blob: f79ccac6699fb7b171b680261db9000f7fe4c70c [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
Alex Elder863c7eb2013-04-15 14:50:36 -0500123#define osd_req_op_data(oreq, whch, typ, fld) \
124 ({ \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
127 })
128
Alex Elder49719772013-02-11 12:33:24 -0600129static struct ceph_osd_data *
130osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
131{
132 BUG_ON(which >= osd_req->r_num_ops);
133
134 return &osd_req->r_ops[which].raw_data_in;
135}
136
Alex Eldera4ce40a2013-04-05 01:27:12 -0500137struct ceph_osd_data *
138osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500139 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500140{
Alex Elder863c7eb2013-04-15 14:50:36 -0500141 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500142}
143EXPORT_SYMBOL(osd_req_op_extent_osd_data);
144
145struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500146osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
147 unsigned int which)
148{
Alex Elder863c7eb2013-04-15 14:50:36 -0500149 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500150}
151EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
152
Alex Elder49719772013-02-11 12:33:24 -0600153void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
157{
158 struct ceph_osd_data *osd_data;
159
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
163}
164EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
165
Alex Eldera4ce40a2013-04-05 01:27:12 -0500166void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169 bool pages_from_pool, bool own_pages)
170{
171 struct ceph_osd_data *osd_data;
172
Alex Elder863c7eb2013-04-15 14:50:36 -0500173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176}
177EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
178
179void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500180 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500181{
182 struct ceph_osd_data *osd_data;
183
Alex Elder863c7eb2013-04-15 14:50:36 -0500184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500185 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500186}
187EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
188
189#ifdef CONFIG_BLOCK
190void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500191 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500192{
193 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500194
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500197}
198EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199#endif /* CONFIG_BLOCK */
200
201static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
204{
205 struct ceph_osd_data *osd_data;
206
Alex Elder863c7eb2013-04-15 14:50:36 -0500207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500209}
210
Alex Elder04017e22013-04-05 14:46:02 -0500211void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
214{
215 struct ceph_osd_data *osd_data;
216
Alex Elder863c7eb2013-04-15 14:50:36 -0500217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500218 ceph_osd_data_pagelist_init(osd_data, pagelist);
219}
220EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
221
Alex Elder6c57b552013-04-19 15:34:49 -0500222void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
225{
226 struct ceph_osd_data *osd_data;
227
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
231}
232EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
233
Alex Eldera4ce40a2013-04-05 01:27:12 -0500234void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
237{
238 struct ceph_osd_data *osd_data;
239
Alex Elder863c7eb2013-04-15 14:50:36 -0500240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500243}
244EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
245
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500246static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
247{
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
250 return 0;
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
255#ifdef CONFIG_BLOCK
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258#endif /* CONFIG_BLOCK */
259 default:
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
261 return 0;
262 }
263}
264
Alex Elderc54d47b2013-04-03 01:28:57 -0500265static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
266{
Alex Elder54764922013-04-05 01:27:12 -0500267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500268 int num_pages;
269
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
273 }
Alex Elder54764922013-04-05 01:27:12 -0500274 ceph_osd_data_init(osd_data);
275}
276
277static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
278 unsigned int which)
279{
280 struct ceph_osd_req_op *op;
281
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
284
285 switch (op->op) {
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200288 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500289 ceph_osd_data_release(&op->extent.osd_data);
290 break;
291 case CEPH_OSD_OP_CALL:
292 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500293 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500294 ceph_osd_data_release(&op->cls.response_data);
295 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800296 case CEPH_OSD_OP_SETXATTR:
297 case CEPH_OSD_OP_CMPXATTR:
298 ceph_osd_data_release(&op->xattr.osd_data);
299 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800300 case CEPH_OSD_OP_STAT:
301 ceph_osd_data_release(&op->raw_data_in);
302 break;
Alex Elder54764922013-04-05 01:27:12 -0500303 default:
304 break;
305 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500306}
307
Sage Weilf24e9982009-10-06 11:31:10 -0700308/*
309 * requests
310 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400311static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700312{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400313 struct ceph_osd_request *req = container_of(kref,
314 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500315 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800316
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400317 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
318 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400319 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
320 WARN_ON(!list_empty(&req->r_req_lru_item));
321 WARN_ON(!list_empty(&req->r_osd_item));
322 WARN_ON(!list_empty(&req->r_linger_item));
323 WARN_ON(!list_empty(&req->r_linger_osd_item));
324 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400325
Sage Weil415e49a2009-12-07 13:37:03 -0800326 if (req->r_request)
327 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500328 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500329 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500330 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500331 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600332
Alex Elder54764922013-04-05 01:27:12 -0500333 for (which = 0; which < req->r_num_ops; which++)
334 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600335
Sage Weil415e49a2009-12-07 13:37:03 -0800336 ceph_put_snap_context(req->r_snapc);
337 if (req->r_mempool)
338 mempool_free(req, req->r_osdc->req_mempool);
339 else
Alex Elder5522ae02013-05-01 12:43:04 -0500340 kmem_cache_free(ceph_osd_request_cache, req);
341
Sage Weilf24e9982009-10-06 11:31:10 -0700342}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400343
344void ceph_osdc_get_request(struct ceph_osd_request *req)
345{
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_get(&req->r_kref);
349}
350EXPORT_SYMBOL(ceph_osdc_get_request);
351
352void ceph_osdc_put_request(struct ceph_osd_request *req)
353{
354 dout("%s %p (was %d)\n", __func__, req,
355 atomic_read(&req->r_kref.refcount));
356 kref_put(&req->r_kref, ceph_osdc_release_request);
357}
358EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700359
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700360struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800362 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600364 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365{
366 struct ceph_osd_request *req;
367 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800368 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700369
Alex Elder79528732013-04-03 21:32:51 -0500370 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
371 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
372
Sage Weil1b83bef2013-02-25 16:11:12 -0800373 msg_size = 4 + 4 + 8 + 8 + 4+8;
374 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
375 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200376 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800377 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
378 msg_size += 8; /* snapid */
379 msg_size += 8; /* snap_seq */
380 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
381 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700382
383 if (use_mempool) {
384 req = mempool_alloc(osdc->req_mempool, gfp_flags);
385 memset(req, 0, sizeof(*req));
386 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500387 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388 }
389 if (req == NULL)
390 return NULL;
391
392 req->r_osdc = osdc;
393 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500394 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700395
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700396 kref_init(&req->r_kref);
397 init_completion(&req->r_completion);
398 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600399 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700400 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700401 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400402 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700403 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700404 INIT_LIST_HEAD(&req->r_osd_item);
405
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200406 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200407 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200408
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700409 /* create reply message */
410 if (use_mempool)
411 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
412 else
413 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700414 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700415 if (!msg) {
416 ceph_osdc_put_request(req);
417 return NULL;
418 }
419 req->r_reply = msg;
420
421 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700422 if (use_mempool)
423 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
424 else
Sage Weilb61c2762011-08-09 15:03:46 -0700425 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700426 if (!msg) {
427 ceph_osdc_put_request(req);
428 return NULL;
429 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700430
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700431 memset(msg->front.iov_base, 0, msg->front.iov_len);
432
433 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700434
435 return req;
436}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700437EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700438
Alex Eldera8dd0a32013-03-13 20:50:00 -0500439static bool osd_req_opcode_valid(u16 opcode)
440{
441 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400442#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
443__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
444#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500445 default:
446 return false;
447 }
448}
449
Alex Elder33803f32013-03-13 20:50:00 -0500450/*
451 * This is an osd op init function for opcodes that have no data or
452 * other information associated with them. It also serves as a
453 * common init routine for all the other init functions, below.
454 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500455static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600456_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800457 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500458{
Alex Elderc99d2d42013-04-05 01:27:11 -0500459 struct ceph_osd_req_op *op;
460
461 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500462 BUG_ON(!osd_req_opcode_valid(opcode));
463
Alex Elderc99d2d42013-04-05 01:27:11 -0500464 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500465 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500466 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800467 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500468
469 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500470}
471
Alex Elder49719772013-02-11 12:33:24 -0600472void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800473 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600474{
Yan, Zheng144cba12015-04-27 11:09:54 +0800475 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600476}
477EXPORT_SYMBOL(osd_req_op_init);
478
Alex Elderc99d2d42013-04-05 01:27:11 -0500479void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
480 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500481 u64 offset, u64 length,
482 u64 truncate_size, u32 truncate_seq)
483{
Yan, Zheng144cba12015-04-27 11:09:54 +0800484 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
485 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500486 size_t payload_len = 0;
487
Li Wangad7a60d2013-08-15 11:51:44 +0800488 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200489 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
490 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500491
Alex Elder33803f32013-03-13 20:50:00 -0500492 op->extent.offset = offset;
493 op->extent.length = length;
494 op->extent.truncate_size = truncate_size;
495 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200496 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500497 payload_len += length;
498
499 op->payload_len = payload_len;
500}
501EXPORT_SYMBOL(osd_req_op_extent_init);
502
Alex Elderc99d2d42013-04-05 01:27:11 -0500503void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
504 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500505{
Alex Elderc99d2d42013-04-05 01:27:11 -0500506 struct ceph_osd_req_op *op;
507 u64 previous;
508
509 BUG_ON(which >= osd_req->r_num_ops);
510 op = &osd_req->r_ops[which];
511 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500512
513 if (length == previous)
514 return; /* Nothing to do */
515 BUG_ON(length > previous);
516
517 op->extent.length = length;
518 op->payload_len -= previous - length;
519}
520EXPORT_SYMBOL(osd_req_op_extent_update);
521
Alex Elderc99d2d42013-04-05 01:27:11 -0500522void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500523 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500524{
Yan, Zheng144cba12015-04-27 11:09:54 +0800525 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
526 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500527 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500528 size_t payload_len = 0;
529 size_t size;
530
531 BUG_ON(opcode != CEPH_OSD_OP_CALL);
532
Alex Elder5f562df2013-04-05 01:27:12 -0500533 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
534 BUG_ON(!pagelist);
535 ceph_pagelist_init(pagelist);
536
Alex Elder33803f32013-03-13 20:50:00 -0500537 op->cls.class_name = class;
538 size = strlen(class);
539 BUG_ON(size > (size_t) U8_MAX);
540 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500541 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500542 payload_len += size;
543
544 op->cls.method_name = method;
545 size = strlen(method);
546 BUG_ON(size > (size_t) U8_MAX);
547 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500548 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500549 payload_len += size;
550
Alex Eldera4ce40a2013-04-05 01:27:12 -0500551 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500552
Alex Elder33803f32013-03-13 20:50:00 -0500553 op->cls.argc = 0; /* currently unused */
554
555 op->payload_len = payload_len;
556}
557EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500558
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800559int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
560 u16 opcode, const char *name, const void *value,
561 size_t size, u8 cmp_op, u8 cmp_mode)
562{
Yan, Zheng144cba12015-04-27 11:09:54 +0800563 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
564 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800565 struct ceph_pagelist *pagelist;
566 size_t payload_len;
567
568 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
569
570 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
571 if (!pagelist)
572 return -ENOMEM;
573
574 ceph_pagelist_init(pagelist);
575
576 payload_len = strlen(name);
577 op->xattr.name_len = payload_len;
578 ceph_pagelist_append(pagelist, name, payload_len);
579
580 op->xattr.value_len = size;
581 ceph_pagelist_append(pagelist, value, size);
582 payload_len += size;
583
584 op->xattr.cmp_op = cmp_op;
585 op->xattr.cmp_mode = cmp_mode;
586
587 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
588 op->payload_len = payload_len;
589 return 0;
590}
591EXPORT_SYMBOL(osd_req_op_xattr_init);
592
Alex Elderc99d2d42013-04-05 01:27:11 -0500593void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
594 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500595 u64 cookie, u64 version, int flag)
596{
Yan, Zheng144cba12015-04-27 11:09:54 +0800597 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
598 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500599
Alex Elderc99d2d42013-04-05 01:27:11 -0500600 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500601
602 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500603 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500604 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500605 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500606}
607EXPORT_SYMBOL(osd_req_op_watch_init);
608
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200609void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
610 unsigned int which,
611 u64 expected_object_size,
612 u64 expected_write_size)
613{
614 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800615 CEPH_OSD_OP_SETALLOCHINT,
616 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200617
618 op->alloc_hint.expected_object_size = expected_object_size;
619 op->alloc_hint.expected_write_size = expected_write_size;
620
621 /*
622 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
623 * not worth a feature bit. Set FAILOK per-op flag to make
624 * sure older osds don't trip over an unsupported opcode.
625 */
626 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
627}
628EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
629
Alex Elder90af3602013-04-05 14:46:01 -0500630static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500631 struct ceph_osd_data *osd_data)
632{
633 u64 length = ceph_osd_data_length(osd_data);
634
635 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
636 BUG_ON(length > (u64) SIZE_MAX);
637 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500638 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500639 length, osd_data->alignment);
640 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
641 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500642 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500643#ifdef CONFIG_BLOCK
644 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500645 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500646#endif
647 } else {
648 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
649 }
650}
651
Alex Elder175face2013-03-08 13:35:36 -0600652static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500653 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700654{
Alex Elder79528732013-04-03 21:32:51 -0500655 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500656 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500657 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500658 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600659
Alex Elder79528732013-04-03 21:32:51 -0500660 BUG_ON(which >= req->r_num_ops);
661 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500662 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
663 pr_err("unrecognized osd opcode %d\n", src->op);
664
665 return 0;
666 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700667
Alex Elder065a68f2012-04-20 15:49:43 -0500668 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600669 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600670 osd_data = &src->raw_data_in;
671 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600672 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700673 case CEPH_OSD_OP_READ:
674 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200675 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800676 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800677 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200678 if (src->op == CEPH_OSD_OP_WRITE ||
679 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500680 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600681 dst->extent.offset = cpu_to_le64(src->extent.offset);
682 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700683 dst->extent.truncate_size =
684 cpu_to_le64(src->extent.truncate_size);
685 dst->extent.truncate_seq =
686 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500687 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200688 if (src->op == CEPH_OSD_OP_WRITE ||
689 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500690 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500691 else
Alex Elder04017e22013-04-05 14:46:02 -0500692 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700693 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700694 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700695 dst->cls.class_len = src->cls.class_len;
696 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500697 osd_data = &src->cls.request_info;
698 ceph_osdc_msg_data_add(req->r_request, osd_data);
699 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
700 request_data_len = osd_data->pagelist->length;
701
702 osd_data = &src->cls.request_data;
703 data_length = ceph_osd_data_length(osd_data);
704 if (data_length) {
705 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
706 dst->cls.indata_len = cpu_to_le32(data_length);
707 ceph_osdc_msg_data_add(req->r_request, osd_data);
708 src->payload_len += data_length;
709 request_data_len += data_length;
710 }
711 osd_data = &src->cls.response_data;
712 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700713 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700714 case CEPH_OSD_OP_STARTSYNC:
715 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700716 case CEPH_OSD_OP_NOTIFY_ACK:
717 case CEPH_OSD_OP_WATCH:
718 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
719 dst->watch.ver = cpu_to_le64(src->watch.ver);
720 dst->watch.flag = src->watch.flag;
721 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200722 case CEPH_OSD_OP_SETALLOCHINT:
723 dst->alloc_hint.expected_object_size =
724 cpu_to_le64(src->alloc_hint.expected_object_size);
725 dst->alloc_hint.expected_write_size =
726 cpu_to_le64(src->alloc_hint.expected_write_size);
727 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800728 case CEPH_OSD_OP_SETXATTR:
729 case CEPH_OSD_OP_CMPXATTR:
730 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
731 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
732 dst->xattr.cmp_op = src->xattr.cmp_op;
733 dst->xattr.cmp_mode = src->xattr.cmp_mode;
734 osd_data = &src->xattr.osd_data;
735 ceph_osdc_msg_data_add(req->r_request, osd_data);
736 request_data_len = osd_data->pagelist->length;
737 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800738 case CEPH_OSD_OP_CREATE:
739 case CEPH_OSD_OP_DELETE:
740 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700741 default:
Alex Elder4c464592013-02-15 11:42:30 -0600742 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600743 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600744 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500745
746 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700747 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200748
Alex Eldera8dd0a32013-03-13 20:50:00 -0500749 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200750 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700751 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600752
Alex Elder54d50642013-04-03 01:28:58 -0500753 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700754}
755
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700756/*
Sage Weilf24e9982009-10-06 11:31:10 -0700757 * build new request AND message, calculate layout, and adjust file
758 * extent as needed.
759 *
760 * if the file was recently truncated, we include information about its
761 * old and new size so that the object can be updated appropriately. (we
762 * avoid synchronously deleting truncated objects because it's slow.)
763 *
764 * if @do_sync, include a 'startsync' command so that the osd will flush
765 * data quickly.
766 */
767struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
768 struct ceph_file_layout *layout,
769 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800770 u64 off, u64 *plen,
771 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700772 int opcode, int flags,
773 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700774 u32 truncate_seq,
775 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600776 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700777{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700778 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500779 u64 objnum = 0;
780 u64 objoff = 0;
781 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700782 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700783
Li Wangad7a60d2013-08-15 11:51:44 +0800784 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800785 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
786 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700787
Alex Elderacead002013-03-14 14:09:05 -0500788 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600789 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700790 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700791 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500792
Alex Elderd178a9e2012-11-13 21:11:15 -0600793 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700794
795 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500796 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600797 if (r < 0) {
798 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700799 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600800 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500801
Yan, Zheng864e9192014-11-13 10:47:25 +0800802 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800803 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800804 } else {
805 u32 object_size = le32_to_cpu(layout->fl_object_size);
806 u32 object_base = off - objoff;
807 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
808 if (truncate_size <= object_base) {
809 truncate_size = 0;
810 } else {
811 truncate_size -= object_base;
812 if (truncate_size > object_size)
813 truncate_size = object_size;
814 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800815 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800816 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800817 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500818 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500819
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200820 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700821
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200822 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200823 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200824 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600825
Sage Weilf24e9982009-10-06 11:31:10 -0700826 return req;
827}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700828EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700829
830/*
831 * We keep osd requests in an rbtree, sorted by ->r_tid.
832 */
833static void __insert_request(struct ceph_osd_client *osdc,
834 struct ceph_osd_request *new)
835{
836 struct rb_node **p = &osdc->requests.rb_node;
837 struct rb_node *parent = NULL;
838 struct ceph_osd_request *req = NULL;
839
840 while (*p) {
841 parent = *p;
842 req = rb_entry(parent, struct ceph_osd_request, r_node);
843 if (new->r_tid < req->r_tid)
844 p = &(*p)->rb_left;
845 else if (new->r_tid > req->r_tid)
846 p = &(*p)->rb_right;
847 else
848 BUG();
849 }
850
851 rb_link_node(&new->r_node, parent, p);
852 rb_insert_color(&new->r_node, &osdc->requests);
853}
854
855static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
856 u64 tid)
857{
858 struct ceph_osd_request *req;
859 struct rb_node *n = osdc->requests.rb_node;
860
861 while (n) {
862 req = rb_entry(n, struct ceph_osd_request, r_node);
863 if (tid < req->r_tid)
864 n = n->rb_left;
865 else if (tid > req->r_tid)
866 n = n->rb_right;
867 else
868 return req;
869 }
870 return NULL;
871}
872
873static struct ceph_osd_request *
874__lookup_request_ge(struct ceph_osd_client *osdc,
875 u64 tid)
876{
877 struct ceph_osd_request *req;
878 struct rb_node *n = osdc->requests.rb_node;
879
880 while (n) {
881 req = rb_entry(n, struct ceph_osd_request, r_node);
882 if (tid < req->r_tid) {
883 if (!n->rb_left)
884 return req;
885 n = n->rb_left;
886 } else if (tid > req->r_tid) {
887 n = n->rb_right;
888 } else {
889 return req;
890 }
891 }
892 return NULL;
893}
894
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400895static void __kick_linger_request(struct ceph_osd_request *req)
896{
897 struct ceph_osd_client *osdc = req->r_osdc;
898 struct ceph_osd *osd = req->r_osd;
899
900 /*
901 * Linger requests need to be resent with a new tid to avoid
902 * the dup op detection logic on the OSDs. Achieve this with
903 * a re-register dance instead of open-coding.
904 */
905 ceph_osdc_get_request(req);
906 if (!list_empty(&req->r_linger_item))
907 __unregister_linger_request(osdc, req);
908 else
909 __unregister_request(osdc, req);
910 __register_request(osdc, req);
911 ceph_osdc_put_request(req);
912
913 /*
914 * Unless request has been registered as both normal and
915 * lingering, __unregister{,_linger}_request clears r_osd.
916 * However, here we need to preserve r_osd to make sure we
917 * requeue on the same OSD.
918 */
919 WARN_ON(req->r_osd || !osd);
920 req->r_osd = osd;
921
922 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
923 __enqueue_request(req);
924}
925
Sage Weil6f6c7002011-01-17 20:34:08 -0800926/*
927 * Resubmit requests pending on the given osd.
928 */
929static void __kick_osd_requests(struct ceph_osd_client *osdc,
930 struct ceph_osd *osd)
931{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700932 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500933 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400934 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800935 int err;
936
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400937 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800938 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600939 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800940 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400941
Alex Eldere02493c2013-03-25 18:16:11 -0500942 /*
943 * Build up a list of requests to resend by traversing the
944 * osd's list of requests. Requests for a given object are
945 * sent in tid order, and that is also the order they're
946 * kept on this list. Therefore all requests that are in
947 * flight will be found first, followed by all requests that
948 * have not yet been sent. And to resend requests while
949 * preserving this order we will want to put any sent
950 * requests back on the front of the osd client's unsent
951 * list.
952 *
953 * So we build a separate ordered list of already-sent
954 * requests for the affected osd and splice it onto the
955 * front of the osd client's unsent list. Once we've seen a
956 * request that has not yet been sent we're done. Those
957 * requests are already sitting right where they belong.
958 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800959 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500960 if (!req->r_sent)
961 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400962
963 if (!req->r_linger) {
964 dout("%s requeueing %p tid %llu\n", __func__, req,
965 req->r_tid);
966 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700967 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400968 } else {
969 list_move_tail(&req->r_req_lru_item, &resend_linger);
970 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700971 }
Alex Eldere02493c2013-03-25 18:16:11 -0500972 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700973
Alex Eldere02493c2013-03-25 18:16:11 -0500974 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400975 * Both registered and not yet registered linger requests are
976 * enqueued with a new tid on the same OSD. We add/move them
977 * to req_unsent/o_requests at the end to keep things in tid
978 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500979 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700980 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400981 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400982 WARN_ON(!list_empty(&req->r_req_lru_item));
983 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800984 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400985
986 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
987 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800988}
989
Sage Weilf24e9982009-10-06 11:31:10 -0700990/*
Sage Weil81b024e2009-10-09 10:29:18 -0700991 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700992 */
993static void osd_reset(struct ceph_connection *con)
994{
995 struct ceph_osd *osd = con->private;
996 struct ceph_osd_client *osdc;
997
998 if (!osd)
999 return;
1000 dout("osd_reset osd%d\n", osd->o_osd);
1001 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001002 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001003 mutex_lock(&osdc->request_mutex);
1004 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001005 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001006 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001007 up_read(&osdc->map_sem);
1008}
1009
1010/*
1011 * Track open sessions with osds.
1012 */
Alex Eldere10006f2012-05-26 23:26:43 -05001013static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001014{
1015 struct ceph_osd *osd;
1016
1017 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1018 if (!osd)
1019 return NULL;
1020
1021 atomic_set(&osd->o_ref, 1);
1022 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001023 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001024 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001025 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001026 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001027 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001028 osd->o_incarnation = 1;
1029
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001030 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001031
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001032 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001033 return osd;
1034}
1035
1036static struct ceph_osd *get_osd(struct ceph_osd *osd)
1037{
1038 if (atomic_inc_not_zero(&osd->o_ref)) {
1039 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1040 atomic_read(&osd->o_ref));
1041 return osd;
1042 } else {
1043 dout("get_osd %p FAIL\n", osd);
1044 return NULL;
1045 }
1046}
1047
1048static void put_osd(struct ceph_osd *osd)
1049{
1050 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1051 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001052 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001053 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1054
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001055 if (osd->o_auth.authorizer)
1056 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001057 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001058 }
Sage Weilf24e9982009-10-06 11:31:10 -07001059}
1060
1061/*
1062 * remove an osd from our map
1063 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001064static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001065{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001066 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001067 WARN_ON(!list_empty(&osd->o_requests));
1068 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001069
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001070 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001071 rb_erase(&osd->o_node, &osdc->osds);
1072 RB_CLEAR_NODE(&osd->o_node);
1073}
1074
1075static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1076{
1077 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1078
1079 if (!RB_EMPTY_NODE(&osd->o_node)) {
1080 ceph_con_close(&osd->o_con);
1081 __remove_osd(osdc, osd);
1082 put_osd(osd);
1083 }
Sage Weilf24e9982009-10-06 11:31:10 -07001084}
1085
Sage Weilaca420b2011-08-31 14:45:53 -07001086static void remove_all_osds(struct ceph_osd_client *osdc)
1087{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001088 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001089 mutex_lock(&osdc->request_mutex);
1090 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1091 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1092 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001093 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001094 }
1095 mutex_unlock(&osdc->request_mutex);
1096}
1097
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001098static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1099 struct ceph_osd *osd)
1100{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001101 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001102 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001103
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001105 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001106}
1107
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001108static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1109 struct ceph_osd *osd)
1110{
1111 dout("%s %p\n", __func__, osd);
1112
1113 if (list_empty(&osd->o_requests) &&
1114 list_empty(&osd->o_linger_requests))
1115 __move_osd_to_lru(osdc, osd);
1116}
1117
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001118static void __remove_osd_from_lru(struct ceph_osd *osd)
1119{
1120 dout("__remove_osd_from_lru %p\n", osd);
1121 if (!list_empty(&osd->o_osd_lru))
1122 list_del_init(&osd->o_osd_lru);
1123}
1124
Sage Weilaca420b2011-08-31 14:45:53 -07001125static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001126{
1127 struct ceph_osd *osd, *nosd;
1128
1129 dout("__remove_old_osds %p\n", osdc);
1130 mutex_lock(&osdc->request_mutex);
1131 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001132 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001133 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001134 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001135 }
1136 mutex_unlock(&osdc->request_mutex);
1137}
1138
Sage Weilf24e9982009-10-06 11:31:10 -07001139/*
1140 * reset osd connect
1141 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001142static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001143{
Alex Elderc3acb182012-12-07 09:57:58 -06001144 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001145
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001146 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001147 if (list_empty(&osd->o_requests) &&
1148 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001149 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001150 return -ENODEV;
1151 }
1152
1153 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1154 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1155 !ceph_con_opened(&osd->o_con)) {
1156 struct ceph_osd_request *req;
1157
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001158 dout("osd addr hasn't changed and connection never opened, "
1159 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001160 /* touch each r_stamp for handle_timeout()'s benfit */
1161 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1162 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001163
1164 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001165 }
Alex Elderc3acb182012-12-07 09:57:58 -06001166
1167 ceph_con_close(&osd->o_con);
1168 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1169 osd->o_incarnation++;
1170
1171 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001172}
1173
1174static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1175{
1176 struct rb_node **p = &osdc->osds.rb_node;
1177 struct rb_node *parent = NULL;
1178 struct ceph_osd *osd = NULL;
1179
Sage Weilaca420b2011-08-31 14:45:53 -07001180 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001181 while (*p) {
1182 parent = *p;
1183 osd = rb_entry(parent, struct ceph_osd, o_node);
1184 if (new->o_osd < osd->o_osd)
1185 p = &(*p)->rb_left;
1186 else if (new->o_osd > osd->o_osd)
1187 p = &(*p)->rb_right;
1188 else
1189 BUG();
1190 }
1191
1192 rb_link_node(&new->o_node, parent, p);
1193 rb_insert_color(&new->o_node, &osdc->osds);
1194}
1195
1196static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1197{
1198 struct ceph_osd *osd;
1199 struct rb_node *n = osdc->osds.rb_node;
1200
1201 while (n) {
1202 osd = rb_entry(n, struct ceph_osd, o_node);
1203 if (o < osd->o_osd)
1204 n = n->rb_left;
1205 else if (o > osd->o_osd)
1206 n = n->rb_right;
1207 else
1208 return osd;
1209 }
1210 return NULL;
1211}
1212
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001213static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1214{
1215 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001216 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001217}
1218
1219static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1220{
1221 cancel_delayed_work(&osdc->timeout_work);
1222}
Sage Weilf24e9982009-10-06 11:31:10 -07001223
1224/*
1225 * Register request, assign tid. If this is the first request, set up
1226 * the timeout event.
1227 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001228static void __register_request(struct ceph_osd_client *osdc,
1229 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001230{
Sage Weilf24e9982009-10-06 11:31:10 -07001231 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001232 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001233 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001234 __insert_request(osdc, req);
1235 ceph_osdc_get_request(req);
1236 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001237 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001238 dout(" first request, scheduling timeout\n");
1239 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001240 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001241}
1242
Sage Weilf24e9982009-10-06 11:31:10 -07001243/*
1244 * called under osdc->request_mutex
1245 */
1246static void __unregister_request(struct ceph_osd_client *osdc,
1247 struct ceph_osd_request *req)
1248{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001249 if (RB_EMPTY_NODE(&req->r_node)) {
1250 dout("__unregister_request %p tid %lld not registered\n",
1251 req, req->r_tid);
1252 return;
1253 }
1254
Sage Weilf24e9982009-10-06 11:31:10 -07001255 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1256 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001257 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001258 osdc->num_requests--;
1259
Sage Weil0ba64782009-10-08 16:57:16 -07001260 if (req->r_osd) {
1261 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001262 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001263
1264 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001265 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001266 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001267 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001268 }
Sage Weilf24e9982009-10-06 11:31:10 -07001269
Alex Elder7d5f2482012-11-29 08:37:03 -06001270 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001271 ceph_osdc_put_request(req);
1272
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001273 if (osdc->num_requests == 0) {
1274 dout(" no requests, canceling timeout\n");
1275 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001276 }
1277}
1278
1279/*
1280 * Cancel a previously queued request message
1281 */
1282static void __cancel_request(struct ceph_osd_request *req)
1283{
Sage Weil6bc18872010-09-27 10:18:52 -07001284 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001285 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001286 req->r_sent = 0;
1287 }
1288}
1289
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001290static void __register_linger_request(struct ceph_osd_client *osdc,
1291 struct ceph_osd_request *req)
1292{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001293 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1294 WARN_ON(!req->r_linger);
1295
Alex Elder96e4dac2013-05-22 20:54:25 -05001296 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001297 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001298 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001299 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001300 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001301}
1302
1303static void __unregister_linger_request(struct ceph_osd_client *osdc,
1304 struct ceph_osd_request *req)
1305{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001306 WARN_ON(!req->r_linger);
1307
1308 if (list_empty(&req->r_linger_item)) {
1309 dout("%s %p tid %llu not registered\n", __func__, req,
1310 req->r_tid);
1311 return;
1312 }
1313
1314 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001315 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001316
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001317 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001318 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001319 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001320 if (list_empty(&req->r_osd_item))
1321 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001322 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001323 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001324}
1325
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001326void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1327 struct ceph_osd_request *req)
1328{
1329 if (!req->r_linger) {
1330 dout("set_request_linger %p\n", req);
1331 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001332 }
1333}
1334EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1335
Sage Weilf24e9982009-10-06 11:31:10 -07001336/*
Josh Durgind29adb32013-12-02 19:11:48 -08001337 * Returns whether a request should be blocked from being sent
1338 * based on the current osdmap and osd_client settings.
1339 *
1340 * Caller should hold map_sem for read.
1341 */
1342static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1343 struct ceph_osd_request *req)
1344{
1345 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1346 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1347 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1348 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1349 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1350}
1351
1352/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001353 * Calculate mapping of a request to a PG. Takes tiering into account.
1354 */
1355static int __calc_request_pg(struct ceph_osdmap *osdmap,
1356 struct ceph_osd_request *req,
1357 struct ceph_pg *pg_out)
1358{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001359 bool need_check_tiering;
1360
1361 need_check_tiering = false;
1362 if (req->r_target_oloc.pool == -1) {
1363 req->r_target_oloc = req->r_base_oloc; /* struct */
1364 need_check_tiering = true;
1365 }
1366 if (req->r_target_oid.name_len == 0) {
1367 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1368 need_check_tiering = true;
1369 }
1370
1371 if (need_check_tiering &&
1372 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001373 struct ceph_pg_pool_info *pi;
1374
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001375 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001376 if (pi) {
1377 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1378 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001379 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001380 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1381 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001382 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001383 }
1384 /* !pi is caught in ceph_oloc_oid_to_pg() */
1385 }
1386
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001387 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1388 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001389}
1390
Ilya Dryomovf671b582014-09-02 13:40:33 +04001391static void __enqueue_request(struct ceph_osd_request *req)
1392{
1393 struct ceph_osd_client *osdc = req->r_osdc;
1394
1395 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1396 req->r_osd ? req->r_osd->o_osd : -1);
1397
1398 if (req->r_osd) {
1399 __remove_osd_from_lru(req->r_osd);
1400 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1401 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1402 } else {
1403 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1404 }
1405}
1406
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001407/*
Sage Weilf24e9982009-10-06 11:31:10 -07001408 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1409 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001410 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001411 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001412 *
1413 * Return 0 if unchanged, 1 if changed, or negative on error.
1414 *
1415 * Caller should hold map_sem for read and request_mutex.
1416 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001417static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001418 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001419{
Sage Weil5b191d92013-02-23 10:38:16 -08001420 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001421 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001422 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001423 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001424 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001425
Sage Weil6f6c7002011-01-17 20:34:08 -08001426 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001427
1428 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001429 if (err) {
1430 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001431 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001432 }
Sage Weil7740a422010-01-08 15:58:25 -08001433 req->r_pgid = pgid;
1434
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001435 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1436 if (num < 0)
1437 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001438
Josh Durgind29adb32013-12-02 19:11:48 -08001439 was_paused = req->r_paused;
1440 req->r_paused = __req_should_be_paused(osdc, req);
1441 if (was_paused && !req->r_paused)
1442 force_resend = 1;
1443
Sage Weil38d64532011-10-14 13:33:55 -07001444 if ((!force_resend &&
1445 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001446 req->r_sent >= req->r_osd->o_incarnation &&
1447 req->r_num_pg_osds == num &&
1448 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001449 (req->r_osd == NULL && o == -1) ||
1450 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001451 return 0; /* no change */
1452
Sage Weil5b191d92013-02-23 10:38:16 -08001453 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1454 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001455 req->r_osd ? req->r_osd->o_osd : -1);
1456
Sage Weild85b7052010-05-10 10:24:48 -07001457 /* record full pg acting set */
1458 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1459 req->r_num_pg_osds = num;
1460
Sage Weilf24e9982009-10-06 11:31:10 -07001461 if (req->r_osd) {
1462 __cancel_request(req);
1463 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001464 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001465 req->r_osd = NULL;
1466 }
1467
1468 req->r_osd = __lookup_osd(osdc, o);
1469 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001470 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001471 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001472 if (!req->r_osd) {
1473 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001474 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001475 }
Sage Weilf24e9982009-10-06 11:31:10 -07001476
Sage Weil6f6c7002011-01-17 20:34:08 -08001477 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001478 __insert_osd(osdc, req->r_osd);
1479
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001480 ceph_con_open(&req->r_osd->o_con,
1481 CEPH_ENTITY_TYPE_OSD, o,
1482 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001483 }
1484
Ilya Dryomovf671b582014-09-02 13:40:33 +04001485 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001486 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001487
1488out:
Sage Weilf24e9982009-10-06 11:31:10 -07001489 return err;
1490}
1491
1492/*
1493 * caller should hold map_sem (for read) and request_mutex
1494 */
Sage Weil56e925b2012-01-03 12:34:34 -08001495static void __send_request(struct ceph_osd_client *osdc,
1496 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001497{
Sage Weil1b83bef2013-02-25 16:11:12 -08001498 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001499
Sage Weil1b83bef2013-02-25 16:11:12 -08001500 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1501 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1502 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001503
Sage Weil1b83bef2013-02-25 16:11:12 -08001504 /* fill in message content that changes each time we send it */
1505 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1506 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001507 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001508 p = req->r_request_pgid;
1509 ceph_encode_64(&p, req->r_pgid.pool);
1510 ceph_encode_32(&p, req->r_pgid.seed);
1511 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1512 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1513 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001514
Sage Weil3dd72fc2010-03-22 14:42:30 -07001515 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001516 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001517
1518 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001519
Sage Weilf24e9982009-10-06 11:31:10 -07001520 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001521
1522 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001523}
1524
1525/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001526 * Send any requests in the queue (req_unsent).
1527 */
Alex Elderf9d25192013-02-15 11:42:29 -06001528static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001529{
1530 struct ceph_osd_request *req, *tmp;
1531
Alex Elderf9d25192013-02-15 11:42:29 -06001532 dout("__send_queued\n");
1533 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001534 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001535}
1536
1537/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001538 * Caller should hold map_sem for read and request_mutex.
1539 */
1540static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1541 struct ceph_osd_request *req,
1542 bool nofail)
1543{
1544 int rc;
1545
1546 __register_request(osdc, req);
1547 req->r_sent = 0;
1548 req->r_got_reply = 0;
1549 rc = __map_request(osdc, req, 0);
1550 if (rc < 0) {
1551 if (nofail) {
1552 dout("osdc_start_request failed map, "
1553 " will retry %lld\n", req->r_tid);
1554 rc = 0;
1555 } else {
1556 __unregister_request(osdc, req);
1557 }
1558 return rc;
1559 }
1560
1561 if (req->r_osd == NULL) {
1562 dout("send_request %p no up osds in pg\n", req);
1563 ceph_monc_request_next_osdmap(&osdc->client->monc);
1564 } else {
1565 __send_queued(osdc);
1566 }
1567
1568 return 0;
1569}
1570
1571/*
Sage Weilf24e9982009-10-06 11:31:10 -07001572 * Timeout callback, called every N seconds when 1 or more osd
1573 * requests has been active for more than N seconds. When this
1574 * happens, we ping all OSDs with requests who have timed out to
1575 * ensure any communications channel reset is detected. Reset the
1576 * request timeouts another N seconds in the future as we go.
1577 * Reschedule the timeout event another N seconds in future (unless
1578 * there are no open requests).
1579 */
1580static void handle_timeout(struct work_struct *work)
1581{
1582 struct ceph_osd_client *osdc =
1583 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001584 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001585 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001586 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001587 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001588 dout("timeout\n");
1589 down_read(&osdc->map_sem);
1590
1591 ceph_monc_request_next_osdmap(&osdc->client->monc);
1592
1593 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001594
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001595 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001596 * ping osds that are a bit slow. this ensures that if there
1597 * is a break in the TCP connection we will notice, and reopen
1598 * a connection with that osd (from the fault callback).
1599 */
1600 INIT_LIST_HEAD(&slow_osds);
1601 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001602 if (time_before(jiffies,
1603 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001604 break;
1605
1606 osd = req->r_osd;
1607 BUG_ON(!osd);
1608 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001609 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001610 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1611 }
1612 while (!list_empty(&slow_osds)) {
1613 osd = list_entry(slow_osds.next, struct ceph_osd,
1614 o_keepalive_item);
1615 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001616 ceph_con_keepalive(&osd->o_con);
1617 }
1618
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001619 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001620 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001621 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001622 up_read(&osdc->map_sem);
1623}
1624
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001625static void handle_osds_timeout(struct work_struct *work)
1626{
1627 struct ceph_osd_client *osdc =
1628 container_of(work, struct ceph_osd_client,
1629 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001630 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001631
1632 dout("osds timeout\n");
1633 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001634 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001635 up_read(&osdc->map_sem);
1636
1637 schedule_delayed_work(&osdc->osds_timeout_work,
1638 round_jiffies_relative(delay));
1639}
1640
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001641static int ceph_oloc_decode(void **p, void *end,
1642 struct ceph_object_locator *oloc)
1643{
1644 u8 struct_v, struct_cv;
1645 u32 len;
1646 void *struct_end;
1647 int ret = 0;
1648
1649 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1650 struct_v = ceph_decode_8(p);
1651 struct_cv = ceph_decode_8(p);
1652 if (struct_v < 3) {
1653 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1654 struct_v, struct_cv);
1655 goto e_inval;
1656 }
1657 if (struct_cv > 6) {
1658 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1659 struct_v, struct_cv);
1660 goto e_inval;
1661 }
1662 len = ceph_decode_32(p);
1663 ceph_decode_need(p, end, len, e_inval);
1664 struct_end = *p + len;
1665
1666 oloc->pool = ceph_decode_64(p);
1667 *p += 4; /* skip preferred */
1668
1669 len = ceph_decode_32(p);
1670 if (len > 0) {
1671 pr_warn("ceph_object_locator::key is set\n");
1672 goto e_inval;
1673 }
1674
1675 if (struct_v >= 5) {
1676 len = ceph_decode_32(p);
1677 if (len > 0) {
1678 pr_warn("ceph_object_locator::nspace is set\n");
1679 goto e_inval;
1680 }
1681 }
1682
1683 if (struct_v >= 6) {
1684 s64 hash = ceph_decode_64(p);
1685 if (hash != -1) {
1686 pr_warn("ceph_object_locator::hash is set\n");
1687 goto e_inval;
1688 }
1689 }
1690
1691 /* skip the rest */
1692 *p = struct_end;
1693out:
1694 return ret;
1695
1696e_inval:
1697 ret = -EINVAL;
1698 goto out;
1699}
1700
1701static int ceph_redirect_decode(void **p, void *end,
1702 struct ceph_request_redirect *redir)
1703{
1704 u8 struct_v, struct_cv;
1705 u32 len;
1706 void *struct_end;
1707 int ret;
1708
1709 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1710 struct_v = ceph_decode_8(p);
1711 struct_cv = ceph_decode_8(p);
1712 if (struct_cv > 1) {
1713 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1714 struct_v, struct_cv);
1715 goto e_inval;
1716 }
1717 len = ceph_decode_32(p);
1718 ceph_decode_need(p, end, len, e_inval);
1719 struct_end = *p + len;
1720
1721 ret = ceph_oloc_decode(p, end, &redir->oloc);
1722 if (ret)
1723 goto out;
1724
1725 len = ceph_decode_32(p);
1726 if (len > 0) {
1727 pr_warn("ceph_request_redirect::object_name is set\n");
1728 goto e_inval;
1729 }
1730
1731 len = ceph_decode_32(p);
1732 *p += len; /* skip osd_instructions */
1733
1734 /* skip the rest */
1735 *p = struct_end;
1736out:
1737 return ret;
1738
1739e_inval:
1740 ret = -EINVAL;
1741 goto out;
1742}
1743
Sage Weil25845472011-06-03 09:37:09 -07001744static void complete_request(struct ceph_osd_request *req)
1745{
Sage Weil25845472011-06-03 09:37:09 -07001746 complete_all(&req->r_safe_completion); /* fsync waiter */
1747}
1748
Sage Weilf24e9982009-10-06 11:31:10 -07001749/*
1750 * handle osd op reply. either call the callback if it is specified,
1751 * or do the completion to wake up the waiting thread.
1752 */
Sage Weil350b1c32009-12-22 10:45:45 -08001753static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1754 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001755{
Sage Weil1b83bef2013-02-25 16:11:12 -08001756 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001757 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001758 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001759 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001760 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001761 unsigned int numops;
1762 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001763 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001764 s32 retry_attempt;
1765 struct ceph_pg pg;
1766 int err;
1767 u32 reassert_epoch;
1768 u64 reassert_version;
1769 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001770 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001771 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001772 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001773
Sage Weil6df058c2009-12-22 11:24:33 -08001774 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001775 dout("handle_reply %p tid %llu\n", msg, tid);
1776
1777 p = msg->front.iov_base;
1778 end = p + msg->front.iov_len;
1779
1780 ceph_decode_need(&p, end, 4, bad);
1781 object_len = ceph_decode_32(&p);
1782 ceph_decode_need(&p, end, object_len, bad);
1783 p += object_len;
1784
Alex Elderef4859d2013-04-01 18:58:26 -05001785 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001786 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001787 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001788
1789 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1790 flags = ceph_decode_64(&p);
1791 result = ceph_decode_32(&p);
1792 reassert_epoch = ceph_decode_32(&p);
1793 reassert_version = ceph_decode_64(&p);
1794 osdmap_epoch = ceph_decode_32(&p);
1795
Sage Weilf24e9982009-10-06 11:31:10 -07001796 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001797 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001798 mutex_lock(&osdc->request_mutex);
1799 req = __lookup_request(osdc, tid);
1800 if (req == NULL) {
1801 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001802 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001803 }
1804 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001805
1806 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1807 req, result);
1808
Dan Carpenter18741192013-08-15 08:51:58 +03001809 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001810 numops = ceph_decode_32(&p);
1811 if (numops > CEPH_OSD_MAX_OP)
1812 goto bad_put;
1813 if (numops != req->r_num_ops)
1814 goto bad_put;
1815 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001816 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001817 for (i = 0; i < numops; i++) {
1818 struct ceph_osd_op *op = p;
1819 int len;
1820
1821 len = le32_to_cpu(op->payload_len);
1822 req->r_reply_op_len[i] = len;
1823 dout(" op %d has %d bytes\n", i, len);
1824 payload_len += len;
1825 p += sizeof(*op);
1826 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001827 bytes = le32_to_cpu(msg->hdr.data_len);
1828 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001829 pr_warn("sum of op payload lens %d != data_len %d\n",
1830 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001831 goto bad_put;
1832 }
1833
Dan Carpenter18741192013-08-15 08:51:58 +03001834 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001835 retry_attempt = ceph_decode_32(&p);
1836 for (i = 0; i < numops; i++)
1837 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001838
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001839 if (le16_to_cpu(msg->hdr.version) >= 6) {
1840 p += 8 + 4; /* skip replay_version */
1841 p += 8; /* skip user_version */
1842
1843 err = ceph_redirect_decode(&p, end, &redir);
1844 if (err)
1845 goto bad_put;
1846 } else {
1847 redir.oloc.pool = -1;
1848 }
1849
1850 if (redir.oloc.pool != -1) {
1851 dout("redirect pool %lld\n", redir.oloc.pool);
1852
1853 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001854
1855 req->r_target_oloc = redir.oloc; /* struct */
1856
1857 /*
1858 * Start redirect requests with nofail=true. If
1859 * mapping fails, request will end up on the notarget
1860 * list, waiting for the new osdmap (which can take
1861 * a while), even though the original request mapped
1862 * successfully. In the future we might want to follow
1863 * original request's nofail setting here.
1864 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001865 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001866 BUG_ON(err);
1867
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001868 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001869 }
1870
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001871 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001872 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001873 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001874 dout("handle_reply result %d bytes %d\n", req->r_result,
1875 bytes);
1876 if (req->r_result == 0)
1877 req->r_result = bytes;
1878
1879 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001880 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1881 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001882
1883 req->r_got_reply = 1;
1884 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1885 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001886 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001887 }
1888
1889 dout("handle_reply tid %llu flags %d\n", tid, flags);
1890
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001891 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1892 __register_linger_request(osdc, req);
1893
Sage Weilf24e9982009-10-06 11:31:10 -07001894 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001895 if (result < 0 ||
1896 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001897 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1898 __unregister_request(osdc, req);
1899
1900 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001901 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001902
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001903 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001904 if (req->r_unsafe_callback &&
1905 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1906 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001907 if (req->r_callback)
1908 req->r_callback(req, msg);
1909 else
1910 complete_all(&req->r_completion);
1911 }
Sage Weilf24e9982009-10-06 11:31:10 -07001912
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001913 if (flags & CEPH_OSD_FLAG_ONDISK) {
1914 if (req->r_unsafe_callback && already_completed)
1915 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001916 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001917 }
Sage Weilf24e9982009-10-06 11:31:10 -07001918
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001919out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001920 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001921 ceph_osdc_put_request(req);
1922 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001923out_unlock:
1924 mutex_unlock(&osdc->request_mutex);
1925 up_read(&osdc->map_sem);
1926 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001927
Sage Weil1b83bef2013-02-25 16:11:12 -08001928bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001929 req->r_result = -EIO;
1930 __unregister_request(osdc, req);
1931 if (req->r_callback)
1932 req->r_callback(req, msg);
1933 else
1934 complete_all(&req->r_completion);
1935 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001936 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001937bad_mutex:
1938 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001939 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001940bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001941 pr_err("corrupt osd_op_reply got %d %d\n",
1942 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001943 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001944}
1945
Sage Weil6f6c7002011-01-17 20:34:08 -08001946static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001947{
Sage Weilf24e9982009-10-06 11:31:10 -07001948 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001949
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001950 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001951 for (p = rb_first(&osdc->osds); p; p = n) {
1952 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001953
Sage Weil6f6c7002011-01-17 20:34:08 -08001954 n = rb_next(p);
1955 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1956 memcmp(&osd->o_con.peer_addr,
1957 ceph_osd_addr(osdc->osdmap,
1958 osd->o_osd),
1959 sizeof(struct ceph_entity_addr)) != 0)
1960 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001961 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001962}
1963
1964/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001965 * Requeue requests whose mapping to an OSD has changed. If requests map to
1966 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001967 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001968 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001969 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001970static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1971 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001972{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001973 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001974 struct rb_node *p;
1975 int needmap = 0;
1976 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001977 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001978
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001979 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1980 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001981 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001982 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001983 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001984 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001985
1986 /*
1987 * For linger requests that have not yet been
1988 * registered, move them to the linger list; they'll
1989 * be sent to the osd in the loop below. Unregister
1990 * the request before re-registering it as a linger
1991 * request to ensure the __map_request() below
1992 * will decide it needs to be sent.
1993 */
1994 if (req->r_linger && list_empty(&req->r_linger_item)) {
1995 dout("%p tid %llu restart on osd%d\n",
1996 req, req->r_tid,
1997 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001998 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001999 __unregister_request(osdc, req);
2000 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002001 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002002 continue;
2003 }
2004
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002005 force_resend_req = force_resend ||
2006 (force_resend_writes &&
2007 req->r_flags & CEPH_OSD_FLAG_WRITE);
2008 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002009 if (err < 0)
2010 continue; /* error */
2011 if (req->r_osd == NULL) {
2012 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2013 needmap++; /* request a newer map */
2014 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002015 if (!req->r_linger) {
2016 dout("%p tid %llu requeued on osd%d\n", req,
2017 req->r_tid,
2018 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002019 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002020 }
2021 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002022 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002023
2024 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2025 r_linger_item) {
2026 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2027
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002028 err = __map_request(osdc, req,
2029 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002030 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002031 if (err < 0)
2032 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002033 if (req->r_osd == NULL || err > 0) {
2034 if (req->r_osd == NULL) {
2035 dout("lingering %p tid %llu maps to no osd\n",
2036 req, req->r_tid);
2037 /*
2038 * A homeless lingering request makes
2039 * no sense, as it's job is to keep
2040 * a particular OSD connection open.
2041 * Request a newer map and kick the
2042 * request, knowing that it won't be
2043 * resent until we actually get a map
2044 * that can tell us where to send it.
2045 */
2046 needmap++;
2047 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002048
Ilya Dryomovb0494532015-05-11 17:53:10 +03002049 dout("kicking lingering %p tid %llu osd%d\n", req,
2050 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2051 __register_request(osdc, req);
2052 __unregister_linger_request(osdc, req);
2053 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002054 }
Alex Elder14d2f382013-05-15 16:28:33 -05002055 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002056 mutex_unlock(&osdc->request_mutex);
2057
2058 if (needmap) {
2059 dout("%d requests for down osds, need new map\n", needmap);
2060 ceph_monc_request_next_osdmap(&osdc->client->monc);
2061 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002062}
Sage Weil6f6c7002011-01-17 20:34:08 -08002063
2064
Sage Weilf24e9982009-10-06 11:31:10 -07002065/*
2066 * Process updated osd map.
2067 *
2068 * The message contains any number of incremental and full maps, normally
2069 * indicating some sort of topology change in the cluster. Kick requests
2070 * off to different OSDs as needed.
2071 */
2072void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2073{
2074 void *p, *end, *next;
2075 u32 nr_maps, maplen;
2076 u32 epoch;
2077 struct ceph_osdmap *newmap = NULL, *oldmap;
2078 int err;
2079 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002080 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002081
2082 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2083 p = msg->front.iov_base;
2084 end = p + msg->front.iov_len;
2085
2086 /* verify fsid */
2087 ceph_decode_need(&p, end, sizeof(fsid), bad);
2088 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002089 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2090 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002091
2092 down_write(&osdc->map_sem);
2093
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002094 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2095
Sage Weilf24e9982009-10-06 11:31:10 -07002096 /* incremental maps */
2097 ceph_decode_32_safe(&p, end, nr_maps, bad);
2098 dout(" %d inc maps\n", nr_maps);
2099 while (nr_maps > 0) {
2100 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002101 epoch = ceph_decode_32(&p);
2102 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002103 ceph_decode_need(&p, end, maplen, bad);
2104 next = p + maplen;
2105 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2106 dout("applying incremental map %u len %d\n",
2107 epoch, maplen);
2108 newmap = osdmap_apply_incremental(&p, next,
2109 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002110 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002111 if (IS_ERR(newmap)) {
2112 err = PTR_ERR(newmap);
2113 goto bad;
2114 }
Sage Weil30dc6382009-12-21 14:49:37 -08002115 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002116 if (newmap != osdc->osdmap) {
2117 ceph_osdmap_destroy(osdc->osdmap);
2118 osdc->osdmap = newmap;
2119 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002120 was_full = was_full ||
2121 ceph_osdmap_flag(osdc->osdmap,
2122 CEPH_OSDMAP_FULL);
2123 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002124 } else {
2125 dout("ignoring incremental map %u len %d\n",
2126 epoch, maplen);
2127 }
2128 p = next;
2129 nr_maps--;
2130 }
2131 if (newmap)
2132 goto done;
2133
2134 /* full maps */
2135 ceph_decode_32_safe(&p, end, nr_maps, bad);
2136 dout(" %d full maps\n", nr_maps);
2137 while (nr_maps) {
2138 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002139 epoch = ceph_decode_32(&p);
2140 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002141 ceph_decode_need(&p, end, maplen, bad);
2142 if (nr_maps > 1) {
2143 dout("skipping non-latest full map %u len %d\n",
2144 epoch, maplen);
2145 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2146 dout("skipping full map %u len %d, "
2147 "older than our %u\n", epoch, maplen,
2148 osdc->osdmap->epoch);
2149 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002150 int skipped_map = 0;
2151
Sage Weilf24e9982009-10-06 11:31:10 -07002152 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002153 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002154 if (IS_ERR(newmap)) {
2155 err = PTR_ERR(newmap);
2156 goto bad;
2157 }
Sage Weil30dc6382009-12-21 14:49:37 -08002158 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002159 oldmap = osdc->osdmap;
2160 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002161 if (oldmap) {
2162 if (oldmap->epoch + 1 < newmap->epoch)
2163 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002164 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002165 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002166 was_full = was_full ||
2167 ceph_osdmap_flag(osdc->osdmap,
2168 CEPH_OSDMAP_FULL);
2169 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002170 }
2171 p += maplen;
2172 nr_maps--;
2173 }
2174
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002175 if (!osdc->osdmap)
2176 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002177done:
2178 downgrade_write(&osdc->map_sem);
2179 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002180
2181 /*
2182 * subscribe to subsequent osdmap updates if full to ensure
2183 * we find out when we are no longer full and stop returning
2184 * ENOSPC.
2185 */
Josh Durgind29adb32013-12-02 19:11:48 -08002186 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2187 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2188 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002189 ceph_monc_request_next_osdmap(&osdc->client->monc);
2190
Alex Elderf9d25192013-02-15 11:42:29 -06002191 mutex_lock(&osdc->request_mutex);
2192 __send_queued(osdc);
2193 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002194 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002195 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002196 return;
2197
2198bad:
2199 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002200 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002201 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002202}
2203
Sage Weilf24e9982009-10-06 11:31:10 -07002204/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002205 * watch/notify callback event infrastructure
2206 *
2207 * These callbacks are used both for watch and notify operations.
2208 */
2209static void __release_event(struct kref *kref)
2210{
2211 struct ceph_osd_event *event =
2212 container_of(kref, struct ceph_osd_event, kref);
2213
2214 dout("__release_event %p\n", event);
2215 kfree(event);
2216}
2217
2218static void get_event(struct ceph_osd_event *event)
2219{
2220 kref_get(&event->kref);
2221}
2222
2223void ceph_osdc_put_event(struct ceph_osd_event *event)
2224{
2225 kref_put(&event->kref, __release_event);
2226}
2227EXPORT_SYMBOL(ceph_osdc_put_event);
2228
2229static void __insert_event(struct ceph_osd_client *osdc,
2230 struct ceph_osd_event *new)
2231{
2232 struct rb_node **p = &osdc->event_tree.rb_node;
2233 struct rb_node *parent = NULL;
2234 struct ceph_osd_event *event = NULL;
2235
2236 while (*p) {
2237 parent = *p;
2238 event = rb_entry(parent, struct ceph_osd_event, node);
2239 if (new->cookie < event->cookie)
2240 p = &(*p)->rb_left;
2241 else if (new->cookie > event->cookie)
2242 p = &(*p)->rb_right;
2243 else
2244 BUG();
2245 }
2246
2247 rb_link_node(&new->node, parent, p);
2248 rb_insert_color(&new->node, &osdc->event_tree);
2249}
2250
2251static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2252 u64 cookie)
2253{
2254 struct rb_node **p = &osdc->event_tree.rb_node;
2255 struct rb_node *parent = NULL;
2256 struct ceph_osd_event *event = NULL;
2257
2258 while (*p) {
2259 parent = *p;
2260 event = rb_entry(parent, struct ceph_osd_event, node);
2261 if (cookie < event->cookie)
2262 p = &(*p)->rb_left;
2263 else if (cookie > event->cookie)
2264 p = &(*p)->rb_right;
2265 else
2266 return event;
2267 }
2268 return NULL;
2269}
2270
2271static void __remove_event(struct ceph_osd_event *event)
2272{
2273 struct ceph_osd_client *osdc = event->osdc;
2274
2275 if (!RB_EMPTY_NODE(&event->node)) {
2276 dout("__remove_event removed %p\n", event);
2277 rb_erase(&event->node, &osdc->event_tree);
2278 ceph_osdc_put_event(event);
2279 } else {
2280 dout("__remove_event didn't remove %p\n", event);
2281 }
2282}
2283
2284int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2285 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002286 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002287{
2288 struct ceph_osd_event *event;
2289
2290 event = kmalloc(sizeof(*event), GFP_NOIO);
2291 if (!event)
2292 return -ENOMEM;
2293
2294 dout("create_event %p\n", event);
2295 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002296 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002297 event->data = data;
2298 event->osdc = osdc;
2299 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002300 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002301 kref_init(&event->kref); /* one ref for us */
2302 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002303
2304 spin_lock(&osdc->event_lock);
2305 event->cookie = ++osdc->event_count;
2306 __insert_event(osdc, event);
2307 spin_unlock(&osdc->event_lock);
2308
2309 *pevent = event;
2310 return 0;
2311}
2312EXPORT_SYMBOL(ceph_osdc_create_event);
2313
2314void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2315{
2316 struct ceph_osd_client *osdc = event->osdc;
2317
2318 dout("cancel_event %p\n", event);
2319 spin_lock(&osdc->event_lock);
2320 __remove_event(event);
2321 spin_unlock(&osdc->event_lock);
2322 ceph_osdc_put_event(event); /* caller's */
2323}
2324EXPORT_SYMBOL(ceph_osdc_cancel_event);
2325
2326
2327static void do_event_work(struct work_struct *work)
2328{
2329 struct ceph_osd_event_work *event_work =
2330 container_of(work, struct ceph_osd_event_work, work);
2331 struct ceph_osd_event *event = event_work->event;
2332 u64 ver = event_work->ver;
2333 u64 notify_id = event_work->notify_id;
2334 u8 opcode = event_work->opcode;
2335
2336 dout("do_event_work completing %p\n", event);
2337 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002338 dout("do_event_work completed %p\n", event);
2339 ceph_osdc_put_event(event);
2340 kfree(event_work);
2341}
2342
2343
2344/*
2345 * Process osd watch notifications
2346 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002347static void handle_watch_notify(struct ceph_osd_client *osdc,
2348 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002349{
2350 void *p, *end;
2351 u8 proto_ver;
2352 u64 cookie, ver, notify_id;
2353 u8 opcode;
2354 struct ceph_osd_event *event;
2355 struct ceph_osd_event_work *event_work;
2356
2357 p = msg->front.iov_base;
2358 end = p + msg->front.iov_len;
2359
2360 ceph_decode_8_safe(&p, end, proto_ver, bad);
2361 ceph_decode_8_safe(&p, end, opcode, bad);
2362 ceph_decode_64_safe(&p, end, cookie, bad);
2363 ceph_decode_64_safe(&p, end, ver, bad);
2364 ceph_decode_64_safe(&p, end, notify_id, bad);
2365
2366 spin_lock(&osdc->event_lock);
2367 event = __find_event(osdc, cookie);
2368 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002369 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002370 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002371 }
2372 spin_unlock(&osdc->event_lock);
2373 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2374 cookie, ver, event);
2375 if (event) {
2376 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002377 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002378 pr_err("couldn't allocate event_work\n");
2379 ceph_osdc_put_event(event);
2380 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002381 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002382 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002383 event_work->event = event;
2384 event_work->ver = ver;
2385 event_work->notify_id = notify_id;
2386 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002387
2388 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002389 }
2390
2391 return;
2392
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002393bad:
2394 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002395}
2396
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002397/*
Alex Eldere65550f2013-04-05 01:27:12 -05002398 * build new request AND message
2399 *
2400 */
2401void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2402 struct ceph_snap_context *snapc, u64 snap_id,
2403 struct timespec *mtime)
2404{
2405 struct ceph_msg *msg = req->r_request;
2406 void *p;
2407 size_t msg_size;
2408 int flags = req->r_flags;
2409 u64 data_len;
2410 unsigned int i;
2411
2412 req->r_snapid = snap_id;
2413 req->r_snapc = ceph_get_snap_context(snapc);
2414
2415 /* encode request */
2416 msg->hdr.version = cpu_to_le16(4);
2417
2418 p = msg->front.iov_base;
2419 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2420 req->r_request_osdmap_epoch = p;
2421 p += 4;
2422 req->r_request_flags = p;
2423 p += 4;
2424 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2425 ceph_encode_timespec(p, mtime);
2426 p += sizeof(struct ceph_timespec);
2427 req->r_request_reassert_version = p;
2428 p += sizeof(struct ceph_eversion); /* will get filled in */
2429
2430 /* oloc */
2431 ceph_encode_8(&p, 4);
2432 ceph_encode_8(&p, 4);
2433 ceph_encode_32(&p, 8 + 4 + 4);
2434 req->r_request_pool = p;
2435 p += 8;
2436 ceph_encode_32(&p, -1); /* preferred */
2437 ceph_encode_32(&p, 0); /* key len */
2438
2439 ceph_encode_8(&p, 1);
2440 req->r_request_pgid = p;
2441 p += 8 + 4;
2442 ceph_encode_32(&p, -1); /* preferred */
2443
2444 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002445 ceph_encode_32(&p, req->r_base_oid.name_len);
2446 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2447 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2448 req->r_base_oid.name, req->r_base_oid.name_len);
2449 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002450
2451 /* ops--can imply data */
2452 ceph_encode_16(&p, (u16)req->r_num_ops);
2453 data_len = 0;
2454 for (i = 0; i < req->r_num_ops; i++) {
2455 data_len += osd_req_encode_op(req, p, i);
2456 p += sizeof(struct ceph_osd_op);
2457 }
2458
2459 /* snaps */
2460 ceph_encode_64(&p, req->r_snapid);
2461 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2462 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2463 if (req->r_snapc) {
2464 for (i = 0; i < snapc->num_snaps; i++) {
2465 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2466 }
2467 }
2468
2469 req->r_request_attempts = p;
2470 p += 4;
2471
2472 /* data */
2473 if (flags & CEPH_OSD_FLAG_WRITE) {
2474 u16 data_off;
2475
2476 /*
2477 * The header "data_off" is a hint to the receiver
2478 * allowing it to align received data into its
2479 * buffers such that there's no need to re-copy
2480 * it before writing it to disk (direct I/O).
2481 */
2482 data_off = (u16) (off & 0xffff);
2483 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2484 }
2485 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2486
2487 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2488 msg_size = p - msg->front.iov_base;
2489 msg->front.iov_len = msg_size;
2490 msg->hdr.front_len = cpu_to_le32(msg_size);
2491
2492 dout("build_request msg_size was %d\n", (int)msg_size);
2493}
2494EXPORT_SYMBOL(ceph_osdc_build_request);
2495
2496/*
Sage Weilf24e9982009-10-06 11:31:10 -07002497 * Register request, send initial attempt.
2498 */
2499int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2500 struct ceph_osd_request *req,
2501 bool nofail)
2502{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002503 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002504
Sage Weilf24e9982009-10-06 11:31:10 -07002505 down_read(&osdc->map_sem);
2506 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002507
2508 rc = __ceph_osdc_start_request(osdc, req, nofail);
2509
Sage Weilf24e9982009-10-06 11:31:10 -07002510 mutex_unlock(&osdc->request_mutex);
2511 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002512
Sage Weilf24e9982009-10-06 11:31:10 -07002513 return rc;
2514}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002515EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002516
2517/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002518 * Unregister a registered request. The request is not completed (i.e.
2519 * no callbacks or wakeups) - higher layers are supposed to know what
2520 * they are canceling.
2521 */
2522void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2523{
2524 struct ceph_osd_client *osdc = req->r_osdc;
2525
2526 mutex_lock(&osdc->request_mutex);
2527 if (req->r_linger)
2528 __unregister_linger_request(osdc, req);
2529 __unregister_request(osdc, req);
2530 mutex_unlock(&osdc->request_mutex);
2531
2532 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2533}
2534EXPORT_SYMBOL(ceph_osdc_cancel_request);
2535
2536/*
Sage Weilf24e9982009-10-06 11:31:10 -07002537 * wait for a request to complete
2538 */
2539int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2540 struct ceph_osd_request *req)
2541{
2542 int rc;
2543
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002544 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2545
Sage Weilf24e9982009-10-06 11:31:10 -07002546 rc = wait_for_completion_interruptible(&req->r_completion);
2547 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002548 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2549 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002550 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002551 return rc;
2552 }
2553
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002554 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2555 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002556 return req->r_result;
2557}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002558EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002559
2560/*
2561 * sync - wait for all in-flight requests to flush. avoid starvation.
2562 */
2563void ceph_osdc_sync(struct ceph_osd_client *osdc)
2564{
2565 struct ceph_osd_request *req;
2566 u64 last_tid, next_tid = 0;
2567
2568 mutex_lock(&osdc->request_mutex);
2569 last_tid = osdc->last_tid;
2570 while (1) {
2571 req = __lookup_request_ge(osdc, next_tid);
2572 if (!req)
2573 break;
2574 if (req->r_tid > last_tid)
2575 break;
2576
2577 next_tid = req->r_tid + 1;
2578 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2579 continue;
2580
2581 ceph_osdc_get_request(req);
2582 mutex_unlock(&osdc->request_mutex);
2583 dout("sync waiting on tid %llu (last is %llu)\n",
2584 req->r_tid, last_tid);
2585 wait_for_completion(&req->r_safe_completion);
2586 mutex_lock(&osdc->request_mutex);
2587 ceph_osdc_put_request(req);
2588 }
2589 mutex_unlock(&osdc->request_mutex);
2590 dout("sync done (thru tid %llu)\n", last_tid);
2591}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002592EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002593
2594/*
Josh Durgindd935f42013-08-28 21:43:09 -07002595 * Call all pending notify callbacks - for use after a watch is
2596 * unregistered, to make sure no more callbacks for it will be invoked
2597 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002598void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002599{
2600 flush_workqueue(osdc->notify_wq);
2601}
2602EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2603
2604
2605/*
Sage Weilf24e9982009-10-06 11:31:10 -07002606 * init, shutdown
2607 */
2608int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2609{
2610 int err;
2611
2612 dout("init\n");
2613 osdc->client = client;
2614 osdc->osdmap = NULL;
2615 init_rwsem(&osdc->map_sem);
2616 init_completion(&osdc->map_waiters);
2617 osdc->last_requested_map = 0;
2618 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002619 osdc->last_tid = 0;
2620 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002621 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002622 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002623 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002624 INIT_LIST_HEAD(&osdc->req_unsent);
2625 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002626 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002627 osdc->num_requests = 0;
2628 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002629 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002630 spin_lock_init(&osdc->event_lock);
2631 osdc->event_tree = RB_ROOT;
2632 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002633
2634 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002635 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002636
Sage Weil5f44f142009-11-18 14:52:18 -08002637 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002638 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2639 sizeof(struct ceph_osd_request));
2640 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002641 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002642
Sage Weild50b4092012-07-09 14:22:34 -07002643 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2644 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002645 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002646 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002647 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002648 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002649 OSD_OPREPLY_FRONT_LEN, 10, true,
2650 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002651 if (err < 0)
2652 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002653
Dan Carpenterdbcae082013-08-15 08:58:59 +03002654 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002655 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002656 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002657 goto out_msgpool_reply;
2658
Sage Weilf24e9982009-10-06 11:31:10 -07002659 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002660
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002661out_msgpool_reply:
2662 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002663out_msgpool:
2664 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002665out_mempool:
2666 mempool_destroy(osdc->req_mempool);
2667out:
2668 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002669}
2670
2671void ceph_osdc_stop(struct ceph_osd_client *osdc)
2672{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002673 flush_workqueue(osdc->notify_wq);
2674 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002675 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002676 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002677 if (osdc->osdmap) {
2678 ceph_osdmap_destroy(osdc->osdmap);
2679 osdc->osdmap = NULL;
2680 }
Sage Weilaca420b2011-08-31 14:45:53 -07002681 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002682 mempool_destroy(osdc->req_mempool);
2683 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002684 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002685}
2686
2687/*
2688 * Read some contiguous pages. If we cross a stripe boundary, shorten
2689 * *plen. Return number of bytes read, or error.
2690 */
2691int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2692 struct ceph_vino vino, struct ceph_file_layout *layout,
2693 u64 off, u64 *plen,
2694 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002695 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002696{
2697 struct ceph_osd_request *req;
2698 int rc = 0;
2699
2700 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2701 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002702 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002703 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002704 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002705 false);
Sage Weil68162822012-09-24 21:01:02 -07002706 if (IS_ERR(req))
2707 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002708
2709 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002710
Alex Elder406e2c92013-04-15 14:50:36 -05002711 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002712 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002713
Alex Eldere0c59482013-03-07 15:38:25 -06002714 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002715 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002716
Alex Elder79528732013-04-03 21:32:51 -05002717 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002718
Sage Weilf24e9982009-10-06 11:31:10 -07002719 rc = ceph_osdc_start_request(osdc, req, false);
2720 if (!rc)
2721 rc = ceph_osdc_wait_request(osdc, req);
2722
2723 ceph_osdc_put_request(req);
2724 dout("readpages result %d\n", rc);
2725 return rc;
2726}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002727EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002728
2729/*
2730 * do a synchronous write on N pages
2731 */
2732int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2733 struct ceph_file_layout *layout,
2734 struct ceph_snap_context *snapc,
2735 u64 off, u64 len,
2736 u32 truncate_seq, u64 truncate_size,
2737 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002738 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002739{
2740 struct ceph_osd_request *req;
2741 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002742 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002743
Alex Elderacead002013-03-14 14:09:05 -05002744 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002745 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002746 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002747 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002748 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002749 true);
Sage Weil68162822012-09-24 21:01:02 -07002750 if (IS_ERR(req))
2751 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002752
2753 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002754 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002755 false, false);
2756 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002757
Alex Elder79528732013-04-03 21:32:51 -05002758 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002759
Alex Elder87f979d2013-02-15 11:42:29 -06002760 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002761 if (!rc)
2762 rc = ceph_osdc_wait_request(osdc, req);
2763
2764 ceph_osdc_put_request(req);
2765 if (rc == 0)
2766 rc = len;
2767 dout("writepages result %d\n", rc);
2768 return rc;
2769}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002770EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002771
Alex Elder5522ae02013-05-01 12:43:04 -05002772int ceph_osdc_setup(void)
2773{
2774 BUG_ON(ceph_osd_request_cache);
2775 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2776 sizeof (struct ceph_osd_request),
2777 __alignof__(struct ceph_osd_request),
2778 0, NULL);
2779
2780 return ceph_osd_request_cache ? 0 : -ENOMEM;
2781}
2782EXPORT_SYMBOL(ceph_osdc_setup);
2783
2784void ceph_osdc_cleanup(void)
2785{
2786 BUG_ON(!ceph_osd_request_cache);
2787 kmem_cache_destroy(ceph_osd_request_cache);
2788 ceph_osd_request_cache = NULL;
2789}
2790EXPORT_SYMBOL(ceph_osdc_cleanup);
2791
Sage Weilf24e9982009-10-06 11:31:10 -07002792/*
2793 * handle incoming message
2794 */
2795static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2796{
2797 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002798 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002799 int type = le16_to_cpu(msg->hdr.type);
2800
2801 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002802 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002803 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002804
2805 switch (type) {
2806 case CEPH_MSG_OSD_MAP:
2807 ceph_osdc_handle_map(osdc, msg);
2808 break;
2809 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002810 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002811 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002812 case CEPH_MSG_WATCH_NOTIFY:
2813 handle_watch_notify(osdc, msg);
2814 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002815
2816 default:
2817 pr_err("received unknown message type %d %s\n", type,
2818 ceph_msg_type_name(type));
2819 }
Sage Weil4a32f932010-06-13 10:27:53 -07002820out:
Sage Weilf24e9982009-10-06 11:31:10 -07002821 ceph_msg_put(msg);
2822}
2823
Sage Weil5b3a4db2010-02-19 21:43:23 -08002824/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002825 * Lookup and return message for incoming reply. Don't try to do
2826 * anything about a larger than preallocated data portion of the
2827 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002828 */
2829static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002830 struct ceph_msg_header *hdr,
2831 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002832{
2833 struct ceph_osd *osd = con->private;
2834 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002835 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002836 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002837 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002838 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002839 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002840
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002841 tid = le64_to_cpu(hdr->tid);
2842 mutex_lock(&osdc->request_mutex);
2843 req = __lookup_request(osdc, tid);
2844 if (!req) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002845 pr_warn("%s osd%d tid %llu unknown, skipping\n",
2846 __func__, osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002847 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002848 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002849 goto out;
2850 }
Sage Weilc16e7862010-03-01 13:02:00 -08002851
Alex Elderace6d3a2013-04-01 16:12:14 -05002852 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002853 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002854 req->r_reply, req->r_reply->con);
2855 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002856
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002857 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002858 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2859 __func__, osd->o_osd, req->r_tid, front_len,
2860 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002861 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2862 false);
Sage Weila79832f2010-04-01 16:06:19 -07002863 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002864 goto out;
2865 ceph_msg_put(req->r_reply);
2866 req->r_reply = m;
2867 }
Sage Weilc16e7862010-03-01 13:02:00 -08002868
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002869 if (data_len > req->r_reply->data_length) {
2870 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2871 __func__, osd->o_osd, req->r_tid, data_len,
2872 req->r_reply->data_length);
2873 m = NULL;
2874 *skip = 1;
2875 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002876 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002877
2878 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002879 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002880
2881out:
2882 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002883 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002884}
2885
2886static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2887 struct ceph_msg_header *hdr,
2888 int *skip)
2889{
2890 struct ceph_osd *osd = con->private;
2891 int type = le16_to_cpu(hdr->type);
2892 int front = le32_to_cpu(hdr->front_len);
2893
Alex Elder1c20f2d2012-06-04 14:43:32 -05002894 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002895 switch (type) {
2896 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002897 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002898 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002899 case CEPH_MSG_OSD_OPREPLY:
2900 return get_reply(con, hdr, skip);
2901 default:
2902 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2903 osd->o_osd);
2904 *skip = 1;
2905 return NULL;
2906 }
Sage Weilf24e9982009-10-06 11:31:10 -07002907}
2908
2909/*
2910 * Wrappers to refcount containing ceph_osd struct
2911 */
2912static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2913{
2914 struct ceph_osd *osd = con->private;
2915 if (get_osd(osd))
2916 return con;
2917 return NULL;
2918}
2919
2920static void put_osd_con(struct ceph_connection *con)
2921{
2922 struct ceph_osd *osd = con->private;
2923 put_osd(osd);
2924}
2925
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002926/*
2927 * authentication
2928 */
Alex Eldera3530df2012-05-16 15:16:39 -05002929/*
2930 * Note: returned pointer is the address of a structure that's
2931 * managed separately. Caller must *not* attempt to free it.
2932 */
2933static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002934 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002935{
2936 struct ceph_osd *o = con->private;
2937 struct ceph_osd_client *osdc = o->o_osdc;
2938 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002939 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002940
Alex Elder74f18692012-05-16 15:16:39 -05002941 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002942 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002943 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002944 }
Sage Weil27859f92013-03-25 10:26:14 -07002945 if (!auth->authorizer) {
2946 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2947 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002948 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002949 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002950 } else {
2951 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002952 auth);
2953 if (ret)
2954 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002955 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002956 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002957
Alex Eldera3530df2012-05-16 15:16:39 -05002958 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002959}
2960
2961
2962static int verify_authorizer_reply(struct ceph_connection *con, int len)
2963{
2964 struct ceph_osd *o = con->private;
2965 struct ceph_osd_client *osdc = o->o_osdc;
2966 struct ceph_auth_client *ac = osdc->client->monc.auth;
2967
Sage Weil27859f92013-03-25 10:26:14 -07002968 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002969}
2970
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002971static int invalidate_authorizer(struct ceph_connection *con)
2972{
2973 struct ceph_osd *o = con->private;
2974 struct ceph_osd_client *osdc = o->o_osdc;
2975 struct ceph_auth_client *ac = osdc->client->monc.auth;
2976
Sage Weil27859f92013-03-25 10:26:14 -07002977 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002978 return ceph_monc_validate_auth(&osdc->client->monc);
2979}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002980
Yan, Zheng33d07332014-11-04 16:33:37 +08002981static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2982{
2983 struct ceph_osd *o = con->private;
2984 struct ceph_auth_handshake *auth = &o->o_auth;
2985 return ceph_auth_sign_message(auth, msg);
2986}
2987
2988static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2989{
2990 struct ceph_osd *o = con->private;
2991 struct ceph_auth_handshake *auth = &o->o_auth;
2992 return ceph_auth_check_message_signature(auth, msg);
2993}
2994
Tobias Klauser9e327892010-05-20 10:40:19 +02002995static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002996 .get = get_osd_con,
2997 .put = put_osd_con,
2998 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002999 .get_authorizer = get_authorizer,
3000 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003001 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003002 .alloc_msg = alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003003 .sign_message = sign_message,
3004 .check_message_signature = check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003005 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003006};