blob: 32355d9d0103a827a82f3bfe27493d9a2b47b339 [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/libceph.h>
16#include <linux/ceph/osd_client.h>
17#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OP_FRONT_LEN 4096
23#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Alex Elderf9d25192013-02-15 11:42:29 -060029static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080030static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070031static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040033static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070035static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040037static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080038static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070040
41/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050067 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070070 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070071
Alex Elder60e56f12013-02-15 11:42:29 -060072 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050073 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070075 if (r < 0)
76 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050077 if (*objlen < orig_len) {
78 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060079 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
Alex Elder75d1c942013-03-13 20:50:00 -050083 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070084
Alex Elder3ff5f382013-02-15 22:10:17 -060085 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070086}
87
Alex Elderc54d47b2013-04-03 01:28:57 -050088static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89{
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92}
93
Alex Eldera4ce40a2013-04-05 01:27:12 -050094static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050095 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97{
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500105
Alex Eldera4ce40a2013-04-05 01:27:12 -0500106static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500107 struct ceph_pagelist *pagelist)
108{
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112
113#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500114static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500115 struct bio *bio, size_t bio_length)
116{
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500121#endif /* CONFIG_BLOCK */
122
Ioana Ciornei8a703a32015-10-22 18:06:07 +0300123#define osd_req_op_data(oreq, whch, typ, fld) \
124({ \
125 struct ceph_osd_request *__oreq = (oreq); \
126 unsigned int __whch = (whch); \
127 BUG_ON(__whch >= __oreq->r_num_ops); \
128 &__oreq->r_ops[__whch].typ.fld; \
129})
Alex Elder863c7eb2013-04-15 14:50:36 -0500130
Alex Elder49719772013-02-11 12:33:24 -0600131static struct ceph_osd_data *
132osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
133{
134 BUG_ON(which >= osd_req->r_num_ops);
135
136 return &osd_req->r_ops[which].raw_data_in;
137}
138
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139struct ceph_osd_data *
140osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500141 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500142{
Alex Elder863c7eb2013-04-15 14:50:36 -0500143 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500144}
145EXPORT_SYMBOL(osd_req_op_extent_osd_data);
146
147struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500148osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
149 unsigned int which)
150{
Alex Elder863c7eb2013-04-15 14:50:36 -0500151 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500152}
153EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
154
Alex Elder49719772013-02-11 12:33:24 -0600155void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
156 unsigned int which, struct page **pages,
157 u64 length, u32 alignment,
158 bool pages_from_pool, bool own_pages)
159{
160 struct ceph_osd_data *osd_data;
161
162 osd_data = osd_req_op_raw_data_in(osd_req, which);
163 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
164 pages_from_pool, own_pages);
165}
166EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
167
Alex Eldera4ce40a2013-04-05 01:27:12 -0500168void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500169 unsigned int which, struct page **pages,
170 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500171 bool pages_from_pool, bool own_pages)
172{
173 struct ceph_osd_data *osd_data;
174
Alex Elder863c7eb2013-04-15 14:50:36 -0500175 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
177 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178}
179EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
180
181void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500182 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500183{
184 struct ceph_osd_data *osd_data;
185
Alex Elder863c7eb2013-04-15 14:50:36 -0500186 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500187 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500188}
189EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
190
191#ifdef CONFIG_BLOCK
192void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500193 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500194{
195 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500196
197 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500198 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500199}
200EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
201#endif /* CONFIG_BLOCK */
202
203static void osd_req_op_cls_request_info_pagelist(
204 struct ceph_osd_request *osd_req,
205 unsigned int which, struct ceph_pagelist *pagelist)
206{
207 struct ceph_osd_data *osd_data;
208
Alex Elder863c7eb2013-04-15 14:50:36 -0500209 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500210 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500211}
212
Alex Elder04017e22013-04-05 14:46:02 -0500213void osd_req_op_cls_request_data_pagelist(
214 struct ceph_osd_request *osd_req,
215 unsigned int which, struct ceph_pagelist *pagelist)
216{
217 struct ceph_osd_data *osd_data;
218
Alex Elder863c7eb2013-04-15 14:50:36 -0500219 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500220 ceph_osd_data_pagelist_init(osd_data, pagelist);
221}
222EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
223
Alex Elder6c57b552013-04-19 15:34:49 -0500224void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
225 unsigned int which, struct page **pages, u64 length,
226 u32 alignment, bool pages_from_pool, bool own_pages)
227{
228 struct ceph_osd_data *osd_data;
229
230 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
231 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
232 pages_from_pool, own_pages);
233}
234EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
235
Alex Eldera4ce40a2013-04-05 01:27:12 -0500236void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
237 unsigned int which, struct page **pages, u64 length,
238 u32 alignment, bool pages_from_pool, bool own_pages)
239{
240 struct ceph_osd_data *osd_data;
241
Alex Elder863c7eb2013-04-15 14:50:36 -0500242 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500243 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
244 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500245}
246EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
247
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500248static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
249{
250 switch (osd_data->type) {
251 case CEPH_OSD_DATA_TYPE_NONE:
252 return 0;
253 case CEPH_OSD_DATA_TYPE_PAGES:
254 return osd_data->length;
255 case CEPH_OSD_DATA_TYPE_PAGELIST:
256 return (u64)osd_data->pagelist->length;
257#ifdef CONFIG_BLOCK
258 case CEPH_OSD_DATA_TYPE_BIO:
259 return (u64)osd_data->bio_length;
260#endif /* CONFIG_BLOCK */
261 default:
262 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
263 return 0;
264 }
265}
266
Alex Elderc54d47b2013-04-03 01:28:57 -0500267static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
268{
Alex Elder54764922013-04-05 01:27:12 -0500269 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500270 int num_pages;
271
272 num_pages = calc_pages_for((u64)osd_data->alignment,
273 (u64)osd_data->length);
274 ceph_release_page_vector(osd_data->pages, num_pages);
275 }
Alex Elder54764922013-04-05 01:27:12 -0500276 ceph_osd_data_init(osd_data);
277}
278
279static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
280 unsigned int which)
281{
282 struct ceph_osd_req_op *op;
283
284 BUG_ON(which >= osd_req->r_num_ops);
285 op = &osd_req->r_ops[which];
286
287 switch (op->op) {
288 case CEPH_OSD_OP_READ:
289 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200290 case CEPH_OSD_OP_WRITEFULL:
Alex Elder54764922013-04-05 01:27:12 -0500291 ceph_osd_data_release(&op->extent.osd_data);
292 break;
293 case CEPH_OSD_OP_CALL:
294 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500295 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500296 ceph_osd_data_release(&op->cls.response_data);
297 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800298 case CEPH_OSD_OP_SETXATTR:
299 case CEPH_OSD_OP_CMPXATTR:
300 ceph_osd_data_release(&op->xattr.osd_data);
301 break;
Yan, Zheng66ba6092015-04-27 11:02:35 +0800302 case CEPH_OSD_OP_STAT:
303 ceph_osd_data_release(&op->raw_data_in);
304 break;
Alex Elder54764922013-04-05 01:27:12 -0500305 default:
306 break;
307 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500308}
309
Sage Weilf24e9982009-10-06 11:31:10 -0700310/*
311 * requests
312 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400313static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700314{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400315 struct ceph_osd_request *req = container_of(kref,
316 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500317 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800318
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400319 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
320 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400321 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
322 WARN_ON(!list_empty(&req->r_req_lru_item));
323 WARN_ON(!list_empty(&req->r_osd_item));
324 WARN_ON(!list_empty(&req->r_linger_item));
325 WARN_ON(!list_empty(&req->r_linger_osd_item));
326 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400327
Sage Weil415e49a2009-12-07 13:37:03 -0800328 if (req->r_request)
329 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500330 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500331 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500332 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500333 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600334
Alex Elder54764922013-04-05 01:27:12 -0500335 for (which = 0; which < req->r_num_ops; which++)
336 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600337
Sage Weil415e49a2009-12-07 13:37:03 -0800338 ceph_put_snap_context(req->r_snapc);
339 if (req->r_mempool)
340 mempool_free(req, req->r_osdc->req_mempool);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100341 else if (req->r_num_ops <= CEPH_OSD_SLAB_OPS)
Alex Elder5522ae02013-05-01 12:43:04 -0500342 kmem_cache_free(ceph_osd_request_cache, req);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100343 else
344 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700345}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400346
347void ceph_osdc_get_request(struct ceph_osd_request *req)
348{
349 dout("%s %p (was %d)\n", __func__, req,
350 atomic_read(&req->r_kref.refcount));
351 kref_get(&req->r_kref);
352}
353EXPORT_SYMBOL(ceph_osdc_get_request);
354
355void ceph_osdc_put_request(struct ceph_osd_request *req)
356{
357 dout("%s %p (was %d)\n", __func__, req,
358 atomic_read(&req->r_kref.refcount));
359 kref_put(&req->r_kref, ceph_osdc_release_request);
360}
361EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700362
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700364 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800365 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700366 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600367 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700368{
369 struct ceph_osd_request *req;
370 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800371 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700372
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700373 if (use_mempool) {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100374 BUG_ON(num_ops > CEPH_OSD_SLAB_OPS);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375 req = mempool_alloc(osdc->req_mempool, gfp_flags);
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100376 } else if (num_ops <= CEPH_OSD_SLAB_OPS) {
377 req = kmem_cache_alloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378 } else {
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100379 BUG_ON(num_ops > CEPH_OSD_MAX_OPS);
380 req = kmalloc(sizeof(*req) + num_ops * sizeof(req->r_ops[0]),
381 gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700382 }
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100383 if (unlikely(!req))
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384 return NULL;
385
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100386 /* req only, each op is zeroed in _osd_req_op_init() */
387 memset(req, 0, sizeof(*req));
388
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389 req->r_osdc = osdc;
390 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500391 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700392
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700393 kref_init(&req->r_kref);
394 init_completion(&req->r_completion);
395 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600396 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700398 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400399 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700400 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700401 INIT_LIST_HEAD(&req->r_osd_item);
402
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200403 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200404 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200405
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100406 msg_size = OSD_OPREPLY_FRONT_LEN;
407 if (num_ops > CEPH_OSD_SLAB_OPS) {
408 /* ceph_osd_op and rval */
409 msg_size += (num_ops - CEPH_OSD_SLAB_OPS) *
410 (sizeof(struct ceph_osd_op) + 4);
411 }
412
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700413 /* create reply message */
414 if (use_mempool)
415 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
416 else
Ilya Dryomov3f1af422016-02-09 17:50:15 +0100417 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, msg_size,
418 gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700419 if (!msg) {
420 ceph_osdc_put_request(req);
421 return NULL;
422 }
423 req->r_reply = msg;
424
Ilya Dryomovae458f52016-02-11 13:09:15 +0100425 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
426 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
427 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
428 msg_size += 1 + 8 + 4 + 4; /* pgid */
429 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
430 msg_size += 2 + num_ops * sizeof(struct ceph_osd_op);
431 msg_size += 8; /* snapid */
432 msg_size += 8; /* snap_seq */
433 msg_size += 4 + 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
434 msg_size += 4; /* retry_attempt */
435
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700437 if (use_mempool)
438 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
439 else
Sage Weilb61c2762011-08-09 15:03:46 -0700440 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700441 if (!msg) {
442 ceph_osdc_put_request(req);
443 return NULL;
444 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700445
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700446 memset(msg->front.iov_base, 0, msg->front.iov_len);
447
448 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700449
450 return req;
451}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700452EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700453
Alex Eldera8dd0a32013-03-13 20:50:00 -0500454static bool osd_req_opcode_valid(u16 opcode)
455{
456 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400457#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
458__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
459#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500460 default:
461 return false;
462 }
463}
464
Alex Elder33803f32013-03-13 20:50:00 -0500465/*
466 * This is an osd op init function for opcodes that have no data or
467 * other information associated with them. It also serves as a
468 * common init routine for all the other init functions, below.
469 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500470static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600471_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800472 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500473{
Alex Elderc99d2d42013-04-05 01:27:11 -0500474 struct ceph_osd_req_op *op;
475
476 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500477 BUG_ON(!osd_req_opcode_valid(opcode));
478
Alex Elderc99d2d42013-04-05 01:27:11 -0500479 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500480 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500481 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800482 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500483
484 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500485}
486
Alex Elder49719772013-02-11 12:33:24 -0600487void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800488 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600489{
Yan, Zheng144cba12015-04-27 11:09:54 +0800490 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600491}
492EXPORT_SYMBOL(osd_req_op_init);
493
Alex Elderc99d2d42013-04-05 01:27:11 -0500494void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
495 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500496 u64 offset, u64 length,
497 u64 truncate_size, u32 truncate_seq)
498{
Yan, Zheng144cba12015-04-27 11:09:54 +0800499 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
500 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500501 size_t payload_len = 0;
502
Li Wangad7a60d2013-08-15 11:51:44 +0800503 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200504 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
505 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500506
Alex Elder33803f32013-03-13 20:50:00 -0500507 op->extent.offset = offset;
508 op->extent.length = length;
509 op->extent.truncate_size = truncate_size;
510 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200511 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500512 payload_len += length;
513
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100514 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500515}
516EXPORT_SYMBOL(osd_req_op_extent_init);
517
Alex Elderc99d2d42013-04-05 01:27:11 -0500518void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
519 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500520{
Alex Elderc99d2d42013-04-05 01:27:11 -0500521 struct ceph_osd_req_op *op;
522 u64 previous;
523
524 BUG_ON(which >= osd_req->r_num_ops);
525 op = &osd_req->r_ops[which];
526 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500527
528 if (length == previous)
529 return; /* Nothing to do */
530 BUG_ON(length > previous);
531
532 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100533 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500534}
535EXPORT_SYMBOL(osd_req_op_extent_update);
536
Yan, Zheng2c63f492016-01-07 17:32:54 +0800537void osd_req_op_extent_dup_last(struct ceph_osd_request *osd_req,
538 unsigned int which, u64 offset_inc)
539{
540 struct ceph_osd_req_op *op, *prev_op;
541
542 BUG_ON(which + 1 >= osd_req->r_num_ops);
543
544 prev_op = &osd_req->r_ops[which];
545 op = _osd_req_op_init(osd_req, which + 1, prev_op->op, prev_op->flags);
546 /* dup previous one */
547 op->indata_len = prev_op->indata_len;
548 op->outdata_len = prev_op->outdata_len;
549 op->extent = prev_op->extent;
550 /* adjust offset */
551 op->extent.offset += offset_inc;
552 op->extent.length -= offset_inc;
553
554 if (op->op == CEPH_OSD_OP_WRITE || op->op == CEPH_OSD_OP_WRITEFULL)
555 op->indata_len -= offset_inc;
556}
557EXPORT_SYMBOL(osd_req_op_extent_dup_last);
558
Alex Elderc99d2d42013-04-05 01:27:11 -0500559void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500560 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500561{
Yan, Zheng144cba12015-04-27 11:09:54 +0800562 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
563 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500564 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500565 size_t payload_len = 0;
566 size_t size;
567
568 BUG_ON(opcode != CEPH_OSD_OP_CALL);
569
Alex Elder5f562df2013-04-05 01:27:12 -0500570 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
571 BUG_ON(!pagelist);
572 ceph_pagelist_init(pagelist);
573
Alex Elder33803f32013-03-13 20:50:00 -0500574 op->cls.class_name = class;
575 size = strlen(class);
576 BUG_ON(size > (size_t) U8_MAX);
577 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500578 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500579 payload_len += size;
580
581 op->cls.method_name = method;
582 size = strlen(method);
583 BUG_ON(size > (size_t) U8_MAX);
584 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500585 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500586 payload_len += size;
587
Alex Eldera4ce40a2013-04-05 01:27:12 -0500588 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500589
Alex Elder33803f32013-03-13 20:50:00 -0500590 op->cls.argc = 0; /* currently unused */
591
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100592 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500593}
594EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500595
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800596int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
597 u16 opcode, const char *name, const void *value,
598 size_t size, u8 cmp_op, u8 cmp_mode)
599{
Yan, Zheng144cba12015-04-27 11:09:54 +0800600 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
601 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800602 struct ceph_pagelist *pagelist;
603 size_t payload_len;
604
605 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
606
607 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
608 if (!pagelist)
609 return -ENOMEM;
610
611 ceph_pagelist_init(pagelist);
612
613 payload_len = strlen(name);
614 op->xattr.name_len = payload_len;
615 ceph_pagelist_append(pagelist, name, payload_len);
616
617 op->xattr.value_len = size;
618 ceph_pagelist_append(pagelist, value, size);
619 payload_len += size;
620
621 op->xattr.cmp_op = cmp_op;
622 op->xattr.cmp_mode = cmp_mode;
623
624 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100625 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800626 return 0;
627}
628EXPORT_SYMBOL(osd_req_op_xattr_init);
629
Alex Elderc99d2d42013-04-05 01:27:11 -0500630void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
631 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500632 u64 cookie, u64 version, int flag)
633{
Yan, Zheng144cba12015-04-27 11:09:54 +0800634 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
635 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500636
Alex Elderc99d2d42013-04-05 01:27:11 -0500637 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500638
639 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500640 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500641 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500642 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500643}
644EXPORT_SYMBOL(osd_req_op_watch_init);
645
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200646void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
647 unsigned int which,
648 u64 expected_object_size,
649 u64 expected_write_size)
650{
651 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800652 CEPH_OSD_OP_SETALLOCHINT,
653 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200654
655 op->alloc_hint.expected_object_size = expected_object_size;
656 op->alloc_hint.expected_write_size = expected_write_size;
657
658 /*
659 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
660 * not worth a feature bit. Set FAILOK per-op flag to make
661 * sure older osds don't trip over an unsupported opcode.
662 */
663 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
664}
665EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
666
Alex Elder90af3602013-04-05 14:46:01 -0500667static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500668 struct ceph_osd_data *osd_data)
669{
670 u64 length = ceph_osd_data_length(osd_data);
671
672 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
673 BUG_ON(length > (u64) SIZE_MAX);
674 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500675 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500676 length, osd_data->alignment);
677 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
678 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500679 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500680#ifdef CONFIG_BLOCK
681 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500682 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500683#endif
684 } else {
685 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
686 }
687}
688
Alex Elder175face2013-03-08 13:35:36 -0600689static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500690 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700691{
Alex Elder79528732013-04-03 21:32:51 -0500692 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500693 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500694 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500695 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600696
Alex Elder79528732013-04-03 21:32:51 -0500697 BUG_ON(which >= req->r_num_ops);
698 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500699 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
700 pr_err("unrecognized osd opcode %d\n", src->op);
701
702 return 0;
703 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700704
Alex Elder065a68f2012-04-20 15:49:43 -0500705 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600706 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600707 osd_data = &src->raw_data_in;
708 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600709 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700710 case CEPH_OSD_OP_READ:
711 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200712 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800713 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800714 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200715 if (src->op == CEPH_OSD_OP_WRITE ||
716 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500717 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600718 dst->extent.offset = cpu_to_le64(src->extent.offset);
719 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700720 dst->extent.truncate_size =
721 cpu_to_le64(src->extent.truncate_size);
722 dst->extent.truncate_seq =
723 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500724 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200725 if (src->op == CEPH_OSD_OP_WRITE ||
726 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500727 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500728 else
Alex Elder04017e22013-04-05 14:46:02 -0500729 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700730 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700731 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700732 dst->cls.class_len = src->cls.class_len;
733 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500734 osd_data = &src->cls.request_info;
735 ceph_osdc_msg_data_add(req->r_request, osd_data);
736 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
737 request_data_len = osd_data->pagelist->length;
738
739 osd_data = &src->cls.request_data;
740 data_length = ceph_osd_data_length(osd_data);
741 if (data_length) {
742 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
743 dst->cls.indata_len = cpu_to_le32(data_length);
744 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100745 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500746 request_data_len += data_length;
747 }
748 osd_data = &src->cls.response_data;
749 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700750 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700751 case CEPH_OSD_OP_STARTSYNC:
752 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700753 case CEPH_OSD_OP_NOTIFY_ACK:
754 case CEPH_OSD_OP_WATCH:
755 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
756 dst->watch.ver = cpu_to_le64(src->watch.ver);
757 dst->watch.flag = src->watch.flag;
758 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200759 case CEPH_OSD_OP_SETALLOCHINT:
760 dst->alloc_hint.expected_object_size =
761 cpu_to_le64(src->alloc_hint.expected_object_size);
762 dst->alloc_hint.expected_write_size =
763 cpu_to_le64(src->alloc_hint.expected_write_size);
764 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800765 case CEPH_OSD_OP_SETXATTR:
766 case CEPH_OSD_OP_CMPXATTR:
767 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
768 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
769 dst->xattr.cmp_op = src->xattr.cmp_op;
770 dst->xattr.cmp_mode = src->xattr.cmp_mode;
771 osd_data = &src->xattr.osd_data;
772 ceph_osdc_msg_data_add(req->r_request, osd_data);
773 request_data_len = osd_data->pagelist->length;
774 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800775 case CEPH_OSD_OP_CREATE:
776 case CEPH_OSD_OP_DELETE:
777 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700778 default:
Alex Elder4c464592013-02-15 11:42:30 -0600779 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600780 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600781 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500782
783 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700784 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200785
Alex Eldera8dd0a32013-03-13 20:50:00 -0500786 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200787 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100788 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600789
Alex Elder54d50642013-04-03 01:28:58 -0500790 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700791}
792
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700793/*
Sage Weilf24e9982009-10-06 11:31:10 -0700794 * build new request AND message, calculate layout, and adjust file
795 * extent as needed.
796 *
797 * if the file was recently truncated, we include information about its
798 * old and new size so that the object can be updated appropriately. (we
799 * avoid synchronously deleting truncated objects because it's slow.)
800 *
801 * if @do_sync, include a 'startsync' command so that the osd will flush
802 * data quickly.
803 */
804struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
805 struct ceph_file_layout *layout,
806 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800807 u64 off, u64 *plen,
808 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700809 int opcode, int flags,
810 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700811 u32 truncate_seq,
812 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600813 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700814{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700815 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500816 u64 objnum = 0;
817 u64 objoff = 0;
818 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700819 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700820
Li Wangad7a60d2013-08-15 11:51:44 +0800821 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800822 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
823 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700824
Alex Elderacead002013-03-14 14:09:05 -0500825 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600826 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700827 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700828 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500829
Alex Elderd178a9e2012-11-13 21:11:15 -0600830 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700831
832 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500833 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600834 if (r < 0) {
835 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700836 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600837 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500838
Yan, Zheng864e9192014-11-13 10:47:25 +0800839 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800840 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800841 } else {
842 u32 object_size = le32_to_cpu(layout->fl_object_size);
843 u32 object_base = off - objoff;
844 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
845 if (truncate_size <= object_base) {
846 truncate_size = 0;
847 } else {
848 truncate_size -= object_base;
849 if (truncate_size > object_size)
850 truncate_size = object_size;
851 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800852 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800853 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800854 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500855 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500856
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200857 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700858
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200859 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200860 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200861 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600862
Sage Weilf24e9982009-10-06 11:31:10 -0700863 return req;
864}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700865EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700866
867/*
868 * We keep osd requests in an rbtree, sorted by ->r_tid.
869 */
870static void __insert_request(struct ceph_osd_client *osdc,
871 struct ceph_osd_request *new)
872{
873 struct rb_node **p = &osdc->requests.rb_node;
874 struct rb_node *parent = NULL;
875 struct ceph_osd_request *req = NULL;
876
877 while (*p) {
878 parent = *p;
879 req = rb_entry(parent, struct ceph_osd_request, r_node);
880 if (new->r_tid < req->r_tid)
881 p = &(*p)->rb_left;
882 else if (new->r_tid > req->r_tid)
883 p = &(*p)->rb_right;
884 else
885 BUG();
886 }
887
888 rb_link_node(&new->r_node, parent, p);
889 rb_insert_color(&new->r_node, &osdc->requests);
890}
891
892static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
893 u64 tid)
894{
895 struct ceph_osd_request *req;
896 struct rb_node *n = osdc->requests.rb_node;
897
898 while (n) {
899 req = rb_entry(n, struct ceph_osd_request, r_node);
900 if (tid < req->r_tid)
901 n = n->rb_left;
902 else if (tid > req->r_tid)
903 n = n->rb_right;
904 else
905 return req;
906 }
907 return NULL;
908}
909
910static struct ceph_osd_request *
911__lookup_request_ge(struct ceph_osd_client *osdc,
912 u64 tid)
913{
914 struct ceph_osd_request *req;
915 struct rb_node *n = osdc->requests.rb_node;
916
917 while (n) {
918 req = rb_entry(n, struct ceph_osd_request, r_node);
919 if (tid < req->r_tid) {
920 if (!n->rb_left)
921 return req;
922 n = n->rb_left;
923 } else if (tid > req->r_tid) {
924 n = n->rb_right;
925 } else {
926 return req;
927 }
928 }
929 return NULL;
930}
931
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400932static void __kick_linger_request(struct ceph_osd_request *req)
933{
934 struct ceph_osd_client *osdc = req->r_osdc;
935 struct ceph_osd *osd = req->r_osd;
936
937 /*
938 * Linger requests need to be resent with a new tid to avoid
939 * the dup op detection logic on the OSDs. Achieve this with
940 * a re-register dance instead of open-coding.
941 */
942 ceph_osdc_get_request(req);
943 if (!list_empty(&req->r_linger_item))
944 __unregister_linger_request(osdc, req);
945 else
946 __unregister_request(osdc, req);
947 __register_request(osdc, req);
948 ceph_osdc_put_request(req);
949
950 /*
951 * Unless request has been registered as both normal and
952 * lingering, __unregister{,_linger}_request clears r_osd.
953 * However, here we need to preserve r_osd to make sure we
954 * requeue on the same OSD.
955 */
956 WARN_ON(req->r_osd || !osd);
957 req->r_osd = osd;
958
959 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
960 __enqueue_request(req);
961}
962
Sage Weil6f6c7002011-01-17 20:34:08 -0800963/*
964 * Resubmit requests pending on the given osd.
965 */
966static void __kick_osd_requests(struct ceph_osd_client *osdc,
967 struct ceph_osd *osd)
968{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700969 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500970 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400971 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800972 int err;
973
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400974 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800975 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600976 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800977 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400978
Alex Eldere02493c2013-03-25 18:16:11 -0500979 /*
980 * Build up a list of requests to resend by traversing the
981 * osd's list of requests. Requests for a given object are
982 * sent in tid order, and that is also the order they're
983 * kept on this list. Therefore all requests that are in
984 * flight will be found first, followed by all requests that
985 * have not yet been sent. And to resend requests while
986 * preserving this order we will want to put any sent
987 * requests back on the front of the osd client's unsent
988 * list.
989 *
990 * So we build a separate ordered list of already-sent
991 * requests for the affected osd and splice it onto the
992 * front of the osd client's unsent list. Once we've seen a
993 * request that has not yet been sent we're done. Those
994 * requests are already sitting right where they belong.
995 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800996 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500997 if (!req->r_sent)
998 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400999
1000 if (!req->r_linger) {
1001 dout("%s requeueing %p tid %llu\n", __func__, req,
1002 req->r_tid);
1003 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001004 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001005 } else {
1006 list_move_tail(&req->r_req_lru_item, &resend_linger);
1007 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001008 }
Alex Eldere02493c2013-03-25 18:16:11 -05001009 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001010
Alex Eldere02493c2013-03-25 18:16:11 -05001011 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001012 * Both registered and not yet registered linger requests are
1013 * enqueued with a new tid on the same OSD. We add/move them
1014 * to req_unsent/o_requests at the end to keep things in tid
1015 * order.
Alex Eldere02493c2013-03-25 18:16:11 -05001016 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001017 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001018 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001019 WARN_ON(!list_empty(&req->r_req_lru_item));
1020 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001021 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +04001022
1023 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
1024 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001025}
1026
Sage Weilf24e9982009-10-06 11:31:10 -07001027/*
Sage Weil81b024e2009-10-09 10:29:18 -07001028 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -07001029 */
1030static void osd_reset(struct ceph_connection *con)
1031{
1032 struct ceph_osd *osd = con->private;
1033 struct ceph_osd_client *osdc;
1034
1035 if (!osd)
1036 return;
1037 dout("osd_reset osd%d\n", osd->o_osd);
1038 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001039 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001040 mutex_lock(&osdc->request_mutex);
1041 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001042 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001043 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001044 up_read(&osdc->map_sem);
1045}
1046
1047/*
1048 * Track open sessions with osds.
1049 */
Alex Eldere10006f2012-05-26 23:26:43 -05001050static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001051{
1052 struct ceph_osd *osd;
1053
1054 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1055 if (!osd)
1056 return NULL;
1057
1058 atomic_set(&osd->o_ref, 1);
1059 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001060 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001061 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001062 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001063 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001064 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001065 osd->o_incarnation = 1;
1066
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001067 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001068
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001069 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001070 return osd;
1071}
1072
1073static struct ceph_osd *get_osd(struct ceph_osd *osd)
1074{
1075 if (atomic_inc_not_zero(&osd->o_ref)) {
1076 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1077 atomic_read(&osd->o_ref));
1078 return osd;
1079 } else {
1080 dout("get_osd %p FAIL\n", osd);
1081 return NULL;
1082 }
1083}
1084
1085static void put_osd(struct ceph_osd *osd)
1086{
1087 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1088 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001089 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001090 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1091
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001092 if (osd->o_auth.authorizer)
1093 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001094 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001095 }
Sage Weilf24e9982009-10-06 11:31:10 -07001096}
1097
1098/*
1099 * remove an osd from our map
1100 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001101static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001102{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001103 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001104 WARN_ON(!list_empty(&osd->o_requests));
1105 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001106
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001107 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001108 rb_erase(&osd->o_node, &osdc->osds);
1109 RB_CLEAR_NODE(&osd->o_node);
1110}
1111
1112static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1113{
1114 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1115
1116 if (!RB_EMPTY_NODE(&osd->o_node)) {
1117 ceph_con_close(&osd->o_con);
1118 __remove_osd(osdc, osd);
1119 put_osd(osd);
1120 }
Sage Weilf24e9982009-10-06 11:31:10 -07001121}
1122
Sage Weilaca420b2011-08-31 14:45:53 -07001123static void remove_all_osds(struct ceph_osd_client *osdc)
1124{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001125 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001126 mutex_lock(&osdc->request_mutex);
1127 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1128 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1129 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001130 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001131 }
1132 mutex_unlock(&osdc->request_mutex);
1133}
1134
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001135static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1136 struct ceph_osd *osd)
1137{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001138 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001139 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001140
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001141 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001142 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001143}
1144
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001145static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1146 struct ceph_osd *osd)
1147{
1148 dout("%s %p\n", __func__, osd);
1149
1150 if (list_empty(&osd->o_requests) &&
1151 list_empty(&osd->o_linger_requests))
1152 __move_osd_to_lru(osdc, osd);
1153}
1154
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001155static void __remove_osd_from_lru(struct ceph_osd *osd)
1156{
1157 dout("__remove_osd_from_lru %p\n", osd);
1158 if (!list_empty(&osd->o_osd_lru))
1159 list_del_init(&osd->o_osd_lru);
1160}
1161
Sage Weilaca420b2011-08-31 14:45:53 -07001162static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001163{
1164 struct ceph_osd *osd, *nosd;
1165
1166 dout("__remove_old_osds %p\n", osdc);
1167 mutex_lock(&osdc->request_mutex);
1168 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001169 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001170 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001171 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001172 }
1173 mutex_unlock(&osdc->request_mutex);
1174}
1175
Sage Weilf24e9982009-10-06 11:31:10 -07001176/*
1177 * reset osd connect
1178 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001179static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001180{
Alex Elderc3acb182012-12-07 09:57:58 -06001181 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001182
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001183 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001184 if (list_empty(&osd->o_requests) &&
1185 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001186 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001187 return -ENODEV;
1188 }
1189
1190 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1191 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1192 !ceph_con_opened(&osd->o_con)) {
1193 struct ceph_osd_request *req;
1194
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001195 dout("osd addr hasn't changed and connection never opened, "
1196 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001197 /* touch each r_stamp for handle_timeout()'s benfit */
1198 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1199 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001200
1201 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001202 }
Alex Elderc3acb182012-12-07 09:57:58 -06001203
1204 ceph_con_close(&osd->o_con);
1205 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1206 osd->o_incarnation++;
1207
1208 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001209}
1210
1211static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1212{
1213 struct rb_node **p = &osdc->osds.rb_node;
1214 struct rb_node *parent = NULL;
1215 struct ceph_osd *osd = NULL;
1216
Sage Weilaca420b2011-08-31 14:45:53 -07001217 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001218 while (*p) {
1219 parent = *p;
1220 osd = rb_entry(parent, struct ceph_osd, o_node);
1221 if (new->o_osd < osd->o_osd)
1222 p = &(*p)->rb_left;
1223 else if (new->o_osd > osd->o_osd)
1224 p = &(*p)->rb_right;
1225 else
1226 BUG();
1227 }
1228
1229 rb_link_node(&new->o_node, parent, p);
1230 rb_insert_color(&new->o_node, &osdc->osds);
1231}
1232
1233static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1234{
1235 struct ceph_osd *osd;
1236 struct rb_node *n = osdc->osds.rb_node;
1237
1238 while (n) {
1239 osd = rb_entry(n, struct ceph_osd, o_node);
1240 if (o < osd->o_osd)
1241 n = n->rb_left;
1242 else if (o > osd->o_osd)
1243 n = n->rb_right;
1244 else
1245 return osd;
1246 }
1247 return NULL;
1248}
1249
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001250static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1251{
1252 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001253 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001254}
1255
1256static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1257{
1258 cancel_delayed_work(&osdc->timeout_work);
1259}
Sage Weilf24e9982009-10-06 11:31:10 -07001260
1261/*
1262 * Register request, assign tid. If this is the first request, set up
1263 * the timeout event.
1264 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001265static void __register_request(struct ceph_osd_client *osdc,
1266 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001267{
Sage Weilf24e9982009-10-06 11:31:10 -07001268 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001269 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001270 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001271 __insert_request(osdc, req);
1272 ceph_osdc_get_request(req);
1273 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001274 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001275 dout(" first request, scheduling timeout\n");
1276 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001277 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001278}
1279
Sage Weilf24e9982009-10-06 11:31:10 -07001280/*
1281 * called under osdc->request_mutex
1282 */
1283static void __unregister_request(struct ceph_osd_client *osdc,
1284 struct ceph_osd_request *req)
1285{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001286 if (RB_EMPTY_NODE(&req->r_node)) {
1287 dout("__unregister_request %p tid %lld not registered\n",
1288 req, req->r_tid);
1289 return;
1290 }
1291
Sage Weilf24e9982009-10-06 11:31:10 -07001292 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1293 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001294 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001295 osdc->num_requests--;
1296
Sage Weil0ba64782009-10-08 16:57:16 -07001297 if (req->r_osd) {
1298 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001299 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001300
1301 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001302 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001303 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001304 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001305 }
Sage Weilf24e9982009-10-06 11:31:10 -07001306
Alex Elder7d5f2482012-11-29 08:37:03 -06001307 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001308 ceph_osdc_put_request(req);
1309
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001310 if (osdc->num_requests == 0) {
1311 dout(" no requests, canceling timeout\n");
1312 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001313 }
1314}
1315
1316/*
1317 * Cancel a previously queued request message
1318 */
1319static void __cancel_request(struct ceph_osd_request *req)
1320{
Sage Weil6bc18872010-09-27 10:18:52 -07001321 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001322 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001323 req->r_sent = 0;
1324 }
1325}
1326
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001327static void __register_linger_request(struct ceph_osd_client *osdc,
1328 struct ceph_osd_request *req)
1329{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001330 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1331 WARN_ON(!req->r_linger);
1332
Alex Elder96e4dac2013-05-22 20:54:25 -05001333 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001334 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001335 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001336 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001337 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001338}
1339
1340static void __unregister_linger_request(struct ceph_osd_client *osdc,
1341 struct ceph_osd_request *req)
1342{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001343 WARN_ON(!req->r_linger);
1344
1345 if (list_empty(&req->r_linger_item)) {
1346 dout("%s %p tid %llu not registered\n", __func__, req,
1347 req->r_tid);
1348 return;
1349 }
1350
1351 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001352 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001353
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001354 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001355 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001356 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001357 if (list_empty(&req->r_osd_item))
1358 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001359 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001360 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001361}
1362
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001363void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1364 struct ceph_osd_request *req)
1365{
1366 if (!req->r_linger) {
1367 dout("set_request_linger %p\n", req);
1368 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001369 }
1370}
1371EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1372
Sage Weilf24e9982009-10-06 11:31:10 -07001373/*
Josh Durgind29adb32013-12-02 19:11:48 -08001374 * Returns whether a request should be blocked from being sent
1375 * based on the current osdmap and osd_client settings.
1376 *
1377 * Caller should hold map_sem for read.
1378 */
1379static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1380 struct ceph_osd_request *req)
1381{
1382 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1383 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1384 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1385 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1386 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1387}
1388
1389/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001390 * Calculate mapping of a request to a PG. Takes tiering into account.
1391 */
1392static int __calc_request_pg(struct ceph_osdmap *osdmap,
1393 struct ceph_osd_request *req,
1394 struct ceph_pg *pg_out)
1395{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001396 bool need_check_tiering;
1397
1398 need_check_tiering = false;
1399 if (req->r_target_oloc.pool == -1) {
1400 req->r_target_oloc = req->r_base_oloc; /* struct */
1401 need_check_tiering = true;
1402 }
1403 if (req->r_target_oid.name_len == 0) {
1404 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1405 need_check_tiering = true;
1406 }
1407
1408 if (need_check_tiering &&
1409 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001410 struct ceph_pg_pool_info *pi;
1411
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001412 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001413 if (pi) {
1414 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1415 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001416 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001417 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1418 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001419 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001420 }
1421 /* !pi is caught in ceph_oloc_oid_to_pg() */
1422 }
1423
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001424 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1425 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001426}
1427
Ilya Dryomovf671b582014-09-02 13:40:33 +04001428static void __enqueue_request(struct ceph_osd_request *req)
1429{
1430 struct ceph_osd_client *osdc = req->r_osdc;
1431
1432 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1433 req->r_osd ? req->r_osd->o_osd : -1);
1434
1435 if (req->r_osd) {
1436 __remove_osd_from_lru(req->r_osd);
1437 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1438 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1439 } else {
1440 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1441 }
1442}
1443
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001444/*
Sage Weilf24e9982009-10-06 11:31:10 -07001445 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1446 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001447 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001448 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001449 *
1450 * Return 0 if unchanged, 1 if changed, or negative on error.
1451 *
1452 * Caller should hold map_sem for read and request_mutex.
1453 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001454static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001455 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001456{
Sage Weil5b191d92013-02-23 10:38:16 -08001457 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001458 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001459 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001460 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001461 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001462
Sage Weil6f6c7002011-01-17 20:34:08 -08001463 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001464
1465 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001466 if (err) {
1467 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001468 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001469 }
Sage Weil7740a422010-01-08 15:58:25 -08001470 req->r_pgid = pgid;
1471
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001472 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1473 if (num < 0)
1474 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001475
Josh Durgind29adb32013-12-02 19:11:48 -08001476 was_paused = req->r_paused;
1477 req->r_paused = __req_should_be_paused(osdc, req);
1478 if (was_paused && !req->r_paused)
1479 force_resend = 1;
1480
Sage Weil38d64532011-10-14 13:33:55 -07001481 if ((!force_resend &&
1482 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001483 req->r_sent >= req->r_osd->o_incarnation &&
1484 req->r_num_pg_osds == num &&
1485 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001486 (req->r_osd == NULL && o == -1) ||
1487 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001488 return 0; /* no change */
1489
Sage Weil5b191d92013-02-23 10:38:16 -08001490 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1491 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001492 req->r_osd ? req->r_osd->o_osd : -1);
1493
Sage Weild85b7052010-05-10 10:24:48 -07001494 /* record full pg acting set */
1495 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1496 req->r_num_pg_osds = num;
1497
Sage Weilf24e9982009-10-06 11:31:10 -07001498 if (req->r_osd) {
1499 __cancel_request(req);
1500 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001501 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001502 req->r_osd = NULL;
1503 }
1504
1505 req->r_osd = __lookup_osd(osdc, o);
1506 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001507 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001508 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001509 if (!req->r_osd) {
1510 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001511 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001512 }
Sage Weilf24e9982009-10-06 11:31:10 -07001513
Sage Weil6f6c7002011-01-17 20:34:08 -08001514 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001515 __insert_osd(osdc, req->r_osd);
1516
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001517 ceph_con_open(&req->r_osd->o_con,
1518 CEPH_ENTITY_TYPE_OSD, o,
1519 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001520 }
1521
Ilya Dryomovf671b582014-09-02 13:40:33 +04001522 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001523 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001524
1525out:
Sage Weilf24e9982009-10-06 11:31:10 -07001526 return err;
1527}
1528
1529/*
1530 * caller should hold map_sem (for read) and request_mutex
1531 */
Sage Weil56e925b2012-01-03 12:34:34 -08001532static void __send_request(struct ceph_osd_client *osdc,
1533 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001534{
Sage Weil1b83bef2013-02-25 16:11:12 -08001535 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001536
Sage Weil1b83bef2013-02-25 16:11:12 -08001537 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1538 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1539 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001540
Sage Weil1b83bef2013-02-25 16:11:12 -08001541 /* fill in message content that changes each time we send it */
1542 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1543 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001544 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001545 p = req->r_request_pgid;
1546 ceph_encode_64(&p, req->r_pgid.pool);
1547 ceph_encode_32(&p, req->r_pgid.seed);
1548 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1549 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1550 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001551
Sage Weil3dd72fc2010-03-22 14:42:30 -07001552 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001553 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001554
1555 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001556
Sage Weilf24e9982009-10-06 11:31:10 -07001557 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001558
1559 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001560}
1561
1562/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001563 * Send any requests in the queue (req_unsent).
1564 */
Alex Elderf9d25192013-02-15 11:42:29 -06001565static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001566{
1567 struct ceph_osd_request *req, *tmp;
1568
Alex Elderf9d25192013-02-15 11:42:29 -06001569 dout("__send_queued\n");
1570 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001571 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001572}
1573
1574/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001575 * Caller should hold map_sem for read and request_mutex.
1576 */
1577static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1578 struct ceph_osd_request *req,
1579 bool nofail)
1580{
1581 int rc;
1582
1583 __register_request(osdc, req);
1584 req->r_sent = 0;
1585 req->r_got_reply = 0;
1586 rc = __map_request(osdc, req, 0);
1587 if (rc < 0) {
1588 if (nofail) {
1589 dout("osdc_start_request failed map, "
1590 " will retry %lld\n", req->r_tid);
1591 rc = 0;
1592 } else {
1593 __unregister_request(osdc, req);
1594 }
1595 return rc;
1596 }
1597
1598 if (req->r_osd == NULL) {
1599 dout("send_request %p no up osds in pg\n", req);
1600 ceph_monc_request_next_osdmap(&osdc->client->monc);
1601 } else {
1602 __send_queued(osdc);
1603 }
1604
1605 return 0;
1606}
1607
1608/*
Sage Weilf24e9982009-10-06 11:31:10 -07001609 * Timeout callback, called every N seconds when 1 or more osd
1610 * requests has been active for more than N seconds. When this
1611 * happens, we ping all OSDs with requests who have timed out to
1612 * ensure any communications channel reset is detected. Reset the
1613 * request timeouts another N seconds in the future as we go.
1614 * Reschedule the timeout event another N seconds in future (unless
1615 * there are no open requests).
1616 */
1617static void handle_timeout(struct work_struct *work)
1618{
1619 struct ceph_osd_client *osdc =
1620 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001621 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001622 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001623 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001624 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001625 dout("timeout\n");
1626 down_read(&osdc->map_sem);
1627
1628 ceph_monc_request_next_osdmap(&osdc->client->monc);
1629
1630 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001631
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001632 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001633 * ping osds that are a bit slow. this ensures that if there
1634 * is a break in the TCP connection we will notice, and reopen
1635 * a connection with that osd (from the fault callback).
1636 */
1637 INIT_LIST_HEAD(&slow_osds);
1638 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001639 if (time_before(jiffies,
1640 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001641 break;
1642
1643 osd = req->r_osd;
1644 BUG_ON(!osd);
1645 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001646 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001647 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1648 }
1649 while (!list_empty(&slow_osds)) {
1650 osd = list_entry(slow_osds.next, struct ceph_osd,
1651 o_keepalive_item);
1652 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001653 ceph_con_keepalive(&osd->o_con);
1654 }
1655
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001656 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001657 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001658 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001659 up_read(&osdc->map_sem);
1660}
1661
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001662static void handle_osds_timeout(struct work_struct *work)
1663{
1664 struct ceph_osd_client *osdc =
1665 container_of(work, struct ceph_osd_client,
1666 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001667 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001668
1669 dout("osds timeout\n");
1670 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001671 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001672 up_read(&osdc->map_sem);
1673
1674 schedule_delayed_work(&osdc->osds_timeout_work,
1675 round_jiffies_relative(delay));
1676}
1677
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001678static int ceph_oloc_decode(void **p, void *end,
1679 struct ceph_object_locator *oloc)
1680{
1681 u8 struct_v, struct_cv;
1682 u32 len;
1683 void *struct_end;
1684 int ret = 0;
1685
1686 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1687 struct_v = ceph_decode_8(p);
1688 struct_cv = ceph_decode_8(p);
1689 if (struct_v < 3) {
1690 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1691 struct_v, struct_cv);
1692 goto e_inval;
1693 }
1694 if (struct_cv > 6) {
1695 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1696 struct_v, struct_cv);
1697 goto e_inval;
1698 }
1699 len = ceph_decode_32(p);
1700 ceph_decode_need(p, end, len, e_inval);
1701 struct_end = *p + len;
1702
1703 oloc->pool = ceph_decode_64(p);
1704 *p += 4; /* skip preferred */
1705
1706 len = ceph_decode_32(p);
1707 if (len > 0) {
1708 pr_warn("ceph_object_locator::key is set\n");
1709 goto e_inval;
1710 }
1711
1712 if (struct_v >= 5) {
1713 len = ceph_decode_32(p);
1714 if (len > 0) {
1715 pr_warn("ceph_object_locator::nspace is set\n");
1716 goto e_inval;
1717 }
1718 }
1719
1720 if (struct_v >= 6) {
1721 s64 hash = ceph_decode_64(p);
1722 if (hash != -1) {
1723 pr_warn("ceph_object_locator::hash is set\n");
1724 goto e_inval;
1725 }
1726 }
1727
1728 /* skip the rest */
1729 *p = struct_end;
1730out:
1731 return ret;
1732
1733e_inval:
1734 ret = -EINVAL;
1735 goto out;
1736}
1737
1738static int ceph_redirect_decode(void **p, void *end,
1739 struct ceph_request_redirect *redir)
1740{
1741 u8 struct_v, struct_cv;
1742 u32 len;
1743 void *struct_end;
1744 int ret;
1745
1746 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1747 struct_v = ceph_decode_8(p);
1748 struct_cv = ceph_decode_8(p);
1749 if (struct_cv > 1) {
1750 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1751 struct_v, struct_cv);
1752 goto e_inval;
1753 }
1754 len = ceph_decode_32(p);
1755 ceph_decode_need(p, end, len, e_inval);
1756 struct_end = *p + len;
1757
1758 ret = ceph_oloc_decode(p, end, &redir->oloc);
1759 if (ret)
1760 goto out;
1761
1762 len = ceph_decode_32(p);
1763 if (len > 0) {
1764 pr_warn("ceph_request_redirect::object_name is set\n");
1765 goto e_inval;
1766 }
1767
1768 len = ceph_decode_32(p);
1769 *p += len; /* skip osd_instructions */
1770
1771 /* skip the rest */
1772 *p = struct_end;
1773out:
1774 return ret;
1775
1776e_inval:
1777 ret = -EINVAL;
1778 goto out;
1779}
1780
Sage Weil25845472011-06-03 09:37:09 -07001781static void complete_request(struct ceph_osd_request *req)
1782{
Sage Weil25845472011-06-03 09:37:09 -07001783 complete_all(&req->r_safe_completion); /* fsync waiter */
1784}
1785
Sage Weilf24e9982009-10-06 11:31:10 -07001786/*
1787 * handle osd op reply. either call the callback if it is specified,
1788 * or do the completion to wake up the waiting thread.
1789 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301790static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001791{
Sage Weil1b83bef2013-02-25 16:11:12 -08001792 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001793 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001794 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001795 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001796 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001797 unsigned int numops;
1798 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001799 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001800 s32 retry_attempt;
1801 struct ceph_pg pg;
1802 int err;
1803 u32 reassert_epoch;
1804 u64 reassert_version;
1805 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001806 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001807 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001808 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001809 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001810
Sage Weil6df058c2009-12-22 11:24:33 -08001811 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001812 dout("handle_reply %p tid %llu\n", msg, tid);
1813
1814 p = msg->front.iov_base;
1815 end = p + msg->front.iov_len;
1816
1817 ceph_decode_need(&p, end, 4, bad);
1818 object_len = ceph_decode_32(&p);
1819 ceph_decode_need(&p, end, object_len, bad);
1820 p += object_len;
1821
Alex Elderef4859d2013-04-01 18:58:26 -05001822 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001823 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001824 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001825
1826 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1827 flags = ceph_decode_64(&p);
1828 result = ceph_decode_32(&p);
1829 reassert_epoch = ceph_decode_32(&p);
1830 reassert_version = ceph_decode_64(&p);
1831 osdmap_epoch = ceph_decode_32(&p);
1832
Sage Weilf24e9982009-10-06 11:31:10 -07001833 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001834 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001835 mutex_lock(&osdc->request_mutex);
1836 req = __lookup_request(osdc, tid);
1837 if (req == NULL) {
1838 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001839 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001840 }
1841 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001842
1843 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1844 req, result);
1845
Dan Carpenter18741192013-08-15 08:51:58 +03001846 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001847 numops = ceph_decode_32(&p);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01001848 if (numops > CEPH_OSD_MAX_OPS)
Sage Weil1b83bef2013-02-25 16:11:12 -08001849 goto bad_put;
1850 if (numops != req->r_num_ops)
1851 goto bad_put;
1852 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001853 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001854 for (i = 0; i < numops; i++) {
1855 struct ceph_osd_op *op = p;
1856 int len;
1857
1858 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001859 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001860 dout(" op %d has %d bytes\n", i, len);
1861 payload_len += len;
1862 p += sizeof(*op);
1863 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001864 bytes = le32_to_cpu(msg->hdr.data_len);
1865 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001866 pr_warn("sum of op payload lens %d != data_len %d\n",
1867 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001868 goto bad_put;
1869 }
1870
Dan Carpenter18741192013-08-15 08:51:58 +03001871 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001872 retry_attempt = ceph_decode_32(&p);
1873 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001874 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001875
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001876 if (le16_to_cpu(msg->hdr.version) >= 6) {
1877 p += 8 + 4; /* skip replay_version */
1878 p += 8; /* skip user_version */
1879
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001880 if (le16_to_cpu(msg->hdr.version) >= 7)
1881 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1882 else
1883 decode_redir = 1;
1884 } else {
1885 decode_redir = 0;
1886 }
1887
1888 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001889 err = ceph_redirect_decode(&p, end, &redir);
1890 if (err)
1891 goto bad_put;
1892 } else {
1893 redir.oloc.pool = -1;
1894 }
1895
1896 if (redir.oloc.pool != -1) {
1897 dout("redirect pool %lld\n", redir.oloc.pool);
1898
1899 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001900
1901 req->r_target_oloc = redir.oloc; /* struct */
1902
1903 /*
1904 * Start redirect requests with nofail=true. If
1905 * mapping fails, request will end up on the notarget
1906 * list, waiting for the new osdmap (which can take
1907 * a while), even though the original request mapped
1908 * successfully. In the future we might want to follow
1909 * original request's nofail setting here.
1910 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001911 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001912 BUG_ON(err);
1913
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001914 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001915 }
1916
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001917 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001918 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001919 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001920 dout("handle_reply result %d bytes %d\n", req->r_result,
1921 bytes);
1922 if (req->r_result == 0)
1923 req->r_result = bytes;
1924
1925 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001926 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1927 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001928
1929 req->r_got_reply = 1;
1930 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1931 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001932 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001933 }
1934
1935 dout("handle_reply tid %llu flags %d\n", tid, flags);
1936
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001937 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1938 __register_linger_request(osdc, req);
1939
Sage Weilf24e9982009-10-06 11:31:10 -07001940 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001941 if (result < 0 ||
1942 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001943 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1944 __unregister_request(osdc, req);
1945
1946 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001947 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001948
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001949 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001950 if (req->r_unsafe_callback &&
1951 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1952 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001953 if (req->r_callback)
1954 req->r_callback(req, msg);
1955 else
1956 complete_all(&req->r_completion);
1957 }
Sage Weilf24e9982009-10-06 11:31:10 -07001958
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001959 if (flags & CEPH_OSD_FLAG_ONDISK) {
1960 if (req->r_unsafe_callback && already_completed)
1961 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001962 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001963 }
Sage Weilf24e9982009-10-06 11:31:10 -07001964
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001965out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001966 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001967 ceph_osdc_put_request(req);
1968 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001969out_unlock:
1970 mutex_unlock(&osdc->request_mutex);
1971 up_read(&osdc->map_sem);
1972 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001973
Sage Weil1b83bef2013-02-25 16:11:12 -08001974bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001975 req->r_result = -EIO;
1976 __unregister_request(osdc, req);
1977 if (req->r_callback)
1978 req->r_callback(req, msg);
1979 else
1980 complete_all(&req->r_completion);
1981 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001982 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001983bad_mutex:
1984 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001985 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001986bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001987 pr_err("corrupt osd_op_reply got %d %d\n",
1988 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001989 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001990}
1991
Sage Weil6f6c7002011-01-17 20:34:08 -08001992static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001993{
Sage Weilf24e9982009-10-06 11:31:10 -07001994 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001995
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001996 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001997 for (p = rb_first(&osdc->osds); p; p = n) {
1998 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001999
Sage Weil6f6c7002011-01-17 20:34:08 -08002000 n = rb_next(p);
2001 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
2002 memcmp(&osd->o_con.peer_addr,
2003 ceph_osd_addr(osdc->osdmap,
2004 osd->o_osd),
2005 sizeof(struct ceph_entity_addr)) != 0)
2006 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07002007 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002008}
2009
2010/*
Sage Weil6f6c7002011-01-17 20:34:08 -08002011 * Requeue requests whose mapping to an OSD has changed. If requests map to
2012 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002013 *
Alex Eldere6d50f62012-12-26 14:31:40 -06002014 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002015 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002016static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
2017 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002018{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002019 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08002020 struct rb_node *p;
2021 int needmap = 0;
2022 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002023 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002024
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002025 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
2026 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002027 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07002028 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08002029 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07002030 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06002031
2032 /*
2033 * For linger requests that have not yet been
2034 * registered, move them to the linger list; they'll
2035 * be sent to the osd in the loop below. Unregister
2036 * the request before re-registering it as a linger
2037 * request to ensure the __map_request() below
2038 * will decide it needs to be sent.
2039 */
2040 if (req->r_linger && list_empty(&req->r_linger_item)) {
2041 dout("%p tid %llu restart on osd%d\n",
2042 req, req->r_tid,
2043 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002044 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002045 __unregister_request(osdc, req);
2046 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002047 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002048 continue;
2049 }
2050
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002051 force_resend_req = force_resend ||
2052 (force_resend_writes &&
2053 req->r_flags & CEPH_OSD_FLAG_WRITE);
2054 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002055 if (err < 0)
2056 continue; /* error */
2057 if (req->r_osd == NULL) {
2058 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2059 needmap++; /* request a newer map */
2060 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002061 if (!req->r_linger) {
2062 dout("%p tid %llu requeued on osd%d\n", req,
2063 req->r_tid,
2064 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002065 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002066 }
2067 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002068 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002069
2070 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2071 r_linger_item) {
2072 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2073
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002074 err = __map_request(osdc, req,
2075 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002076 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002077 if (err < 0)
2078 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002079 if (req->r_osd == NULL || err > 0) {
2080 if (req->r_osd == NULL) {
2081 dout("lingering %p tid %llu maps to no osd\n",
2082 req, req->r_tid);
2083 /*
2084 * A homeless lingering request makes
2085 * no sense, as it's job is to keep
2086 * a particular OSD connection open.
2087 * Request a newer map and kick the
2088 * request, knowing that it won't be
2089 * resent until we actually get a map
2090 * that can tell us where to send it.
2091 */
2092 needmap++;
2093 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002094
Ilya Dryomovb0494532015-05-11 17:53:10 +03002095 dout("kicking lingering %p tid %llu osd%d\n", req,
2096 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2097 __register_request(osdc, req);
2098 __unregister_linger_request(osdc, req);
2099 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002100 }
Alex Elder14d2f382013-05-15 16:28:33 -05002101 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002102 mutex_unlock(&osdc->request_mutex);
2103
2104 if (needmap) {
2105 dout("%d requests for down osds, need new map\n", needmap);
2106 ceph_monc_request_next_osdmap(&osdc->client->monc);
2107 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002108}
Sage Weil6f6c7002011-01-17 20:34:08 -08002109
2110
Sage Weilf24e9982009-10-06 11:31:10 -07002111/*
2112 * Process updated osd map.
2113 *
2114 * The message contains any number of incremental and full maps, normally
2115 * indicating some sort of topology change in the cluster. Kick requests
2116 * off to different OSDs as needed.
2117 */
2118void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2119{
2120 void *p, *end, *next;
2121 u32 nr_maps, maplen;
2122 u32 epoch;
2123 struct ceph_osdmap *newmap = NULL, *oldmap;
2124 int err;
2125 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002126 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002127
2128 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2129 p = msg->front.iov_base;
2130 end = p + msg->front.iov_len;
2131
2132 /* verify fsid */
2133 ceph_decode_need(&p, end, sizeof(fsid), bad);
2134 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002135 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2136 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002137
2138 down_write(&osdc->map_sem);
2139
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002140 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2141
Sage Weilf24e9982009-10-06 11:31:10 -07002142 /* incremental maps */
2143 ceph_decode_32_safe(&p, end, nr_maps, bad);
2144 dout(" %d inc maps\n", nr_maps);
2145 while (nr_maps > 0) {
2146 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002147 epoch = ceph_decode_32(&p);
2148 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002149 ceph_decode_need(&p, end, maplen, bad);
2150 next = p + maplen;
2151 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2152 dout("applying incremental map %u len %d\n",
2153 epoch, maplen);
2154 newmap = osdmap_apply_incremental(&p, next,
2155 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002156 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002157 if (IS_ERR(newmap)) {
2158 err = PTR_ERR(newmap);
2159 goto bad;
2160 }
Sage Weil30dc6382009-12-21 14:49:37 -08002161 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002162 if (newmap != osdc->osdmap) {
2163 ceph_osdmap_destroy(osdc->osdmap);
2164 osdc->osdmap = newmap;
2165 }
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, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002170 } else {
2171 dout("ignoring incremental map %u len %d\n",
2172 epoch, maplen);
2173 }
2174 p = next;
2175 nr_maps--;
2176 }
2177 if (newmap)
2178 goto done;
2179
2180 /* full maps */
2181 ceph_decode_32_safe(&p, end, nr_maps, bad);
2182 dout(" %d full maps\n", nr_maps);
2183 while (nr_maps) {
2184 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002185 epoch = ceph_decode_32(&p);
2186 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002187 ceph_decode_need(&p, end, maplen, bad);
2188 if (nr_maps > 1) {
2189 dout("skipping non-latest full map %u len %d\n",
2190 epoch, maplen);
2191 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2192 dout("skipping full map %u len %d, "
2193 "older than our %u\n", epoch, maplen,
2194 osdc->osdmap->epoch);
2195 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002196 int skipped_map = 0;
2197
Sage Weilf24e9982009-10-06 11:31:10 -07002198 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002199 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002200 if (IS_ERR(newmap)) {
2201 err = PTR_ERR(newmap);
2202 goto bad;
2203 }
Sage Weil30dc6382009-12-21 14:49:37 -08002204 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002205 oldmap = osdc->osdmap;
2206 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002207 if (oldmap) {
2208 if (oldmap->epoch + 1 < newmap->epoch)
2209 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002210 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002211 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002212 was_full = was_full ||
2213 ceph_osdmap_flag(osdc->osdmap,
2214 CEPH_OSDMAP_FULL);
2215 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002216 }
2217 p += maplen;
2218 nr_maps--;
2219 }
2220
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002221 if (!osdc->osdmap)
2222 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002223done:
2224 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002225 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2226 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002227
2228 /*
2229 * subscribe to subsequent osdmap updates if full to ensure
2230 * we find out when we are no longer full and stop returning
2231 * ENOSPC.
2232 */
Josh Durgind29adb32013-12-02 19:11:48 -08002233 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2234 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2235 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002236 ceph_monc_request_next_osdmap(&osdc->client->monc);
2237
Alex Elderf9d25192013-02-15 11:42:29 -06002238 mutex_lock(&osdc->request_mutex);
2239 __send_queued(osdc);
2240 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002241 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002242 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002243 return;
2244
2245bad:
2246 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002247 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002248 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002249}
2250
Sage Weilf24e9982009-10-06 11:31:10 -07002251/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002252 * watch/notify callback event infrastructure
2253 *
2254 * These callbacks are used both for watch and notify operations.
2255 */
2256static void __release_event(struct kref *kref)
2257{
2258 struct ceph_osd_event *event =
2259 container_of(kref, struct ceph_osd_event, kref);
2260
2261 dout("__release_event %p\n", event);
2262 kfree(event);
2263}
2264
2265static void get_event(struct ceph_osd_event *event)
2266{
2267 kref_get(&event->kref);
2268}
2269
2270void ceph_osdc_put_event(struct ceph_osd_event *event)
2271{
2272 kref_put(&event->kref, __release_event);
2273}
2274EXPORT_SYMBOL(ceph_osdc_put_event);
2275
2276static void __insert_event(struct ceph_osd_client *osdc,
2277 struct ceph_osd_event *new)
2278{
2279 struct rb_node **p = &osdc->event_tree.rb_node;
2280 struct rb_node *parent = NULL;
2281 struct ceph_osd_event *event = NULL;
2282
2283 while (*p) {
2284 parent = *p;
2285 event = rb_entry(parent, struct ceph_osd_event, node);
2286 if (new->cookie < event->cookie)
2287 p = &(*p)->rb_left;
2288 else if (new->cookie > event->cookie)
2289 p = &(*p)->rb_right;
2290 else
2291 BUG();
2292 }
2293
2294 rb_link_node(&new->node, parent, p);
2295 rb_insert_color(&new->node, &osdc->event_tree);
2296}
2297
2298static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2299 u64 cookie)
2300{
2301 struct rb_node **p = &osdc->event_tree.rb_node;
2302 struct rb_node *parent = NULL;
2303 struct ceph_osd_event *event = NULL;
2304
2305 while (*p) {
2306 parent = *p;
2307 event = rb_entry(parent, struct ceph_osd_event, node);
2308 if (cookie < event->cookie)
2309 p = &(*p)->rb_left;
2310 else if (cookie > event->cookie)
2311 p = &(*p)->rb_right;
2312 else
2313 return event;
2314 }
2315 return NULL;
2316}
2317
2318static void __remove_event(struct ceph_osd_event *event)
2319{
2320 struct ceph_osd_client *osdc = event->osdc;
2321
2322 if (!RB_EMPTY_NODE(&event->node)) {
2323 dout("__remove_event removed %p\n", event);
2324 rb_erase(&event->node, &osdc->event_tree);
2325 ceph_osdc_put_event(event);
2326 } else {
2327 dout("__remove_event didn't remove %p\n", event);
2328 }
2329}
2330
2331int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2332 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002333 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002334{
2335 struct ceph_osd_event *event;
2336
2337 event = kmalloc(sizeof(*event), GFP_NOIO);
2338 if (!event)
2339 return -ENOMEM;
2340
2341 dout("create_event %p\n", event);
2342 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002343 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002344 event->data = data;
2345 event->osdc = osdc;
2346 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002347 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002348 kref_init(&event->kref); /* one ref for us */
2349 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002350
2351 spin_lock(&osdc->event_lock);
2352 event->cookie = ++osdc->event_count;
2353 __insert_event(osdc, event);
2354 spin_unlock(&osdc->event_lock);
2355
2356 *pevent = event;
2357 return 0;
2358}
2359EXPORT_SYMBOL(ceph_osdc_create_event);
2360
2361void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2362{
2363 struct ceph_osd_client *osdc = event->osdc;
2364
2365 dout("cancel_event %p\n", event);
2366 spin_lock(&osdc->event_lock);
2367 __remove_event(event);
2368 spin_unlock(&osdc->event_lock);
2369 ceph_osdc_put_event(event); /* caller's */
2370}
2371EXPORT_SYMBOL(ceph_osdc_cancel_event);
2372
2373
2374static void do_event_work(struct work_struct *work)
2375{
2376 struct ceph_osd_event_work *event_work =
2377 container_of(work, struct ceph_osd_event_work, work);
2378 struct ceph_osd_event *event = event_work->event;
2379 u64 ver = event_work->ver;
2380 u64 notify_id = event_work->notify_id;
2381 u8 opcode = event_work->opcode;
2382
2383 dout("do_event_work completing %p\n", event);
2384 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002385 dout("do_event_work completed %p\n", event);
2386 ceph_osdc_put_event(event);
2387 kfree(event_work);
2388}
2389
2390
2391/*
2392 * Process osd watch notifications
2393 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002394static void handle_watch_notify(struct ceph_osd_client *osdc,
2395 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002396{
2397 void *p, *end;
2398 u8 proto_ver;
2399 u64 cookie, ver, notify_id;
2400 u8 opcode;
2401 struct ceph_osd_event *event;
2402 struct ceph_osd_event_work *event_work;
2403
2404 p = msg->front.iov_base;
2405 end = p + msg->front.iov_len;
2406
2407 ceph_decode_8_safe(&p, end, proto_ver, bad);
2408 ceph_decode_8_safe(&p, end, opcode, bad);
2409 ceph_decode_64_safe(&p, end, cookie, bad);
2410 ceph_decode_64_safe(&p, end, ver, bad);
2411 ceph_decode_64_safe(&p, end, notify_id, bad);
2412
2413 spin_lock(&osdc->event_lock);
2414 event = __find_event(osdc, cookie);
2415 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002416 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002417 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002418 }
2419 spin_unlock(&osdc->event_lock);
2420 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2421 cookie, ver, event);
2422 if (event) {
2423 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002424 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002425 pr_err("couldn't allocate event_work\n");
2426 ceph_osdc_put_event(event);
2427 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002428 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002429 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002430 event_work->event = event;
2431 event_work->ver = ver;
2432 event_work->notify_id = notify_id;
2433 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002434
2435 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002436 }
2437
2438 return;
2439
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002440bad:
2441 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002442}
2443
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002444/*
Alex Eldere65550f2013-04-05 01:27:12 -05002445 * build new request AND message
2446 *
2447 */
2448void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2449 struct ceph_snap_context *snapc, u64 snap_id,
2450 struct timespec *mtime)
2451{
2452 struct ceph_msg *msg = req->r_request;
2453 void *p;
2454 size_t msg_size;
2455 int flags = req->r_flags;
2456 u64 data_len;
2457 unsigned int i;
2458
2459 req->r_snapid = snap_id;
2460 req->r_snapc = ceph_get_snap_context(snapc);
2461
2462 /* encode request */
2463 msg->hdr.version = cpu_to_le16(4);
2464
2465 p = msg->front.iov_base;
2466 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2467 req->r_request_osdmap_epoch = p;
2468 p += 4;
2469 req->r_request_flags = p;
2470 p += 4;
2471 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2472 ceph_encode_timespec(p, mtime);
2473 p += sizeof(struct ceph_timespec);
2474 req->r_request_reassert_version = p;
2475 p += sizeof(struct ceph_eversion); /* will get filled in */
2476
2477 /* oloc */
2478 ceph_encode_8(&p, 4);
2479 ceph_encode_8(&p, 4);
2480 ceph_encode_32(&p, 8 + 4 + 4);
2481 req->r_request_pool = p;
2482 p += 8;
2483 ceph_encode_32(&p, -1); /* preferred */
2484 ceph_encode_32(&p, 0); /* key len */
2485
2486 ceph_encode_8(&p, 1);
2487 req->r_request_pgid = p;
2488 p += 8 + 4;
2489 ceph_encode_32(&p, -1); /* preferred */
2490
2491 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002492 ceph_encode_32(&p, req->r_base_oid.name_len);
2493 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2494 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2495 req->r_base_oid.name, req->r_base_oid.name_len);
2496 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002497
2498 /* ops--can imply data */
2499 ceph_encode_16(&p, (u16)req->r_num_ops);
2500 data_len = 0;
2501 for (i = 0; i < req->r_num_ops; i++) {
2502 data_len += osd_req_encode_op(req, p, i);
2503 p += sizeof(struct ceph_osd_op);
2504 }
2505
2506 /* snaps */
2507 ceph_encode_64(&p, req->r_snapid);
2508 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2509 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2510 if (req->r_snapc) {
2511 for (i = 0; i < snapc->num_snaps; i++) {
2512 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2513 }
2514 }
2515
2516 req->r_request_attempts = p;
2517 p += 4;
2518
2519 /* data */
2520 if (flags & CEPH_OSD_FLAG_WRITE) {
2521 u16 data_off;
2522
2523 /*
2524 * The header "data_off" is a hint to the receiver
2525 * allowing it to align received data into its
2526 * buffers such that there's no need to re-copy
2527 * it before writing it to disk (direct I/O).
2528 */
2529 data_off = (u16) (off & 0xffff);
2530 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2531 }
2532 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2533
2534 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2535 msg_size = p - msg->front.iov_base;
2536 msg->front.iov_len = msg_size;
2537 msg->hdr.front_len = cpu_to_le32(msg_size);
2538
2539 dout("build_request msg_size was %d\n", (int)msg_size);
2540}
2541EXPORT_SYMBOL(ceph_osdc_build_request);
2542
2543/*
Sage Weilf24e9982009-10-06 11:31:10 -07002544 * Register request, send initial attempt.
2545 */
2546int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2547 struct ceph_osd_request *req,
2548 bool nofail)
2549{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002550 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002551
Sage Weilf24e9982009-10-06 11:31:10 -07002552 down_read(&osdc->map_sem);
2553 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002554
2555 rc = __ceph_osdc_start_request(osdc, req, nofail);
2556
Sage Weilf24e9982009-10-06 11:31:10 -07002557 mutex_unlock(&osdc->request_mutex);
2558 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002559
Sage Weilf24e9982009-10-06 11:31:10 -07002560 return rc;
2561}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002562EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002563
2564/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002565 * Unregister a registered request. The request is not completed (i.e.
2566 * no callbacks or wakeups) - higher layers are supposed to know what
2567 * they are canceling.
2568 */
2569void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2570{
2571 struct ceph_osd_client *osdc = req->r_osdc;
2572
2573 mutex_lock(&osdc->request_mutex);
2574 if (req->r_linger)
2575 __unregister_linger_request(osdc, req);
2576 __unregister_request(osdc, req);
2577 mutex_unlock(&osdc->request_mutex);
2578
2579 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2580}
2581EXPORT_SYMBOL(ceph_osdc_cancel_request);
2582
2583/*
Sage Weilf24e9982009-10-06 11:31:10 -07002584 * wait for a request to complete
2585 */
2586int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2587 struct ceph_osd_request *req)
2588{
2589 int rc;
2590
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002591 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2592
Sage Weilf24e9982009-10-06 11:31:10 -07002593 rc = wait_for_completion_interruptible(&req->r_completion);
2594 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002595 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2596 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002597 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002598 return rc;
2599 }
2600
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002601 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2602 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002603 return req->r_result;
2604}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002605EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002606
2607/*
2608 * sync - wait for all in-flight requests to flush. avoid starvation.
2609 */
2610void ceph_osdc_sync(struct ceph_osd_client *osdc)
2611{
2612 struct ceph_osd_request *req;
2613 u64 last_tid, next_tid = 0;
2614
2615 mutex_lock(&osdc->request_mutex);
2616 last_tid = osdc->last_tid;
2617 while (1) {
2618 req = __lookup_request_ge(osdc, next_tid);
2619 if (!req)
2620 break;
2621 if (req->r_tid > last_tid)
2622 break;
2623
2624 next_tid = req->r_tid + 1;
2625 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2626 continue;
2627
2628 ceph_osdc_get_request(req);
2629 mutex_unlock(&osdc->request_mutex);
2630 dout("sync waiting on tid %llu (last is %llu)\n",
2631 req->r_tid, last_tid);
2632 wait_for_completion(&req->r_safe_completion);
2633 mutex_lock(&osdc->request_mutex);
2634 ceph_osdc_put_request(req);
2635 }
2636 mutex_unlock(&osdc->request_mutex);
2637 dout("sync done (thru tid %llu)\n", last_tid);
2638}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002639EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002640
2641/*
Josh Durgindd935f42013-08-28 21:43:09 -07002642 * Call all pending notify callbacks - for use after a watch is
2643 * unregistered, to make sure no more callbacks for it will be invoked
2644 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002645void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002646{
2647 flush_workqueue(osdc->notify_wq);
2648}
2649EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2650
2651
2652/*
Sage Weilf24e9982009-10-06 11:31:10 -07002653 * init, shutdown
2654 */
2655int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2656{
2657 int err;
2658
2659 dout("init\n");
2660 osdc->client = client;
2661 osdc->osdmap = NULL;
2662 init_rwsem(&osdc->map_sem);
2663 init_completion(&osdc->map_waiters);
2664 osdc->last_requested_map = 0;
2665 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002666 osdc->last_tid = 0;
2667 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002668 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002669 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002670 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002671 INIT_LIST_HEAD(&osdc->req_unsent);
2672 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002673 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002674 osdc->num_requests = 0;
2675 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002676 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002677 spin_lock_init(&osdc->event_lock);
2678 osdc->event_tree = RB_ROOT;
2679 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002680
2681 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002682 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002683
Sage Weil5f44f142009-11-18 14:52:18 -08002684 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002685 osdc->req_mempool = mempool_create_slab_pool(10,
2686 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002687 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002688 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002689
Sage Weild50b4092012-07-09 14:22:34 -07002690 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2691 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002692 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002693 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002694 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002695 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002696 OSD_OPREPLY_FRONT_LEN, 10, true,
2697 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002698 if (err < 0)
2699 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002700
Dan Carpenterdbcae082013-08-15 08:58:59 +03002701 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002702 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002703 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002704 goto out_msgpool_reply;
2705
Sage Weilf24e9982009-10-06 11:31:10 -07002706 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002707
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002708out_msgpool_reply:
2709 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002710out_msgpool:
2711 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002712out_mempool:
2713 mempool_destroy(osdc->req_mempool);
2714out:
2715 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002716}
2717
2718void ceph_osdc_stop(struct ceph_osd_client *osdc)
2719{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002720 flush_workqueue(osdc->notify_wq);
2721 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002722 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002723 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002724 if (osdc->osdmap) {
2725 ceph_osdmap_destroy(osdc->osdmap);
2726 osdc->osdmap = NULL;
2727 }
Sage Weilaca420b2011-08-31 14:45:53 -07002728 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002729 mempool_destroy(osdc->req_mempool);
2730 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002731 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002732}
2733
2734/*
2735 * Read some contiguous pages. If we cross a stripe boundary, shorten
2736 * *plen. Return number of bytes read, or error.
2737 */
2738int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2739 struct ceph_vino vino, struct ceph_file_layout *layout,
2740 u64 off, u64 *plen,
2741 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002742 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002743{
2744 struct ceph_osd_request *req;
2745 int rc = 0;
2746
2747 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2748 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002749 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002750 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002751 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002752 false);
Sage Weil68162822012-09-24 21:01:02 -07002753 if (IS_ERR(req))
2754 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002755
2756 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002757
Alex Elder406e2c92013-04-15 14:50:36 -05002758 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002759 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002760
Alex Eldere0c59482013-03-07 15:38:25 -06002761 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002762 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002763
Alex Elder79528732013-04-03 21:32:51 -05002764 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002765
Sage Weilf24e9982009-10-06 11:31:10 -07002766 rc = ceph_osdc_start_request(osdc, req, false);
2767 if (!rc)
2768 rc = ceph_osdc_wait_request(osdc, req);
2769
2770 ceph_osdc_put_request(req);
2771 dout("readpages result %d\n", rc);
2772 return rc;
2773}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002774EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002775
2776/*
2777 * do a synchronous write on N pages
2778 */
2779int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2780 struct ceph_file_layout *layout,
2781 struct ceph_snap_context *snapc,
2782 u64 off, u64 len,
2783 u32 truncate_seq, u64 truncate_size,
2784 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002785 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002786{
2787 struct ceph_osd_request *req;
2788 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002789 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002790
Alex Elderacead002013-03-14 14:09:05 -05002791 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002792 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002793 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002794 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002795 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002796 true);
Sage Weil68162822012-09-24 21:01:02 -07002797 if (IS_ERR(req))
2798 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002799
2800 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002801 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002802 false, false);
2803 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002804
Alex Elder79528732013-04-03 21:32:51 -05002805 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002806
Alex Elder87f979d2013-02-15 11:42:29 -06002807 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002808 if (!rc)
2809 rc = ceph_osdc_wait_request(osdc, req);
2810
2811 ceph_osdc_put_request(req);
2812 if (rc == 0)
2813 rc = len;
2814 dout("writepages result %d\n", rc);
2815 return rc;
2816}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002817EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002818
Alex Elder5522ae02013-05-01 12:43:04 -05002819int ceph_osdc_setup(void)
2820{
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002821 size_t size = sizeof(struct ceph_osd_request) +
2822 CEPH_OSD_SLAB_OPS * sizeof(struct ceph_osd_req_op);
2823
Alex Elder5522ae02013-05-01 12:43:04 -05002824 BUG_ON(ceph_osd_request_cache);
Ilya Dryomov3f1af422016-02-09 17:50:15 +01002825 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request", size,
2826 0, 0, NULL);
Alex Elder5522ae02013-05-01 12:43:04 -05002827
2828 return ceph_osd_request_cache ? 0 : -ENOMEM;
2829}
2830EXPORT_SYMBOL(ceph_osdc_setup);
2831
2832void ceph_osdc_cleanup(void)
2833{
2834 BUG_ON(!ceph_osd_request_cache);
2835 kmem_cache_destroy(ceph_osd_request_cache);
2836 ceph_osd_request_cache = NULL;
2837}
2838EXPORT_SYMBOL(ceph_osdc_cleanup);
2839
Sage Weilf24e9982009-10-06 11:31:10 -07002840/*
2841 * handle incoming message
2842 */
2843static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2844{
2845 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002846 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002847 int type = le16_to_cpu(msg->hdr.type);
2848
2849 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002850 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002851 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002852
2853 switch (type) {
2854 case CEPH_MSG_OSD_MAP:
2855 ceph_osdc_handle_map(osdc, msg);
2856 break;
2857 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302858 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002859 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002860 case CEPH_MSG_WATCH_NOTIFY:
2861 handle_watch_notify(osdc, msg);
2862 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002863
2864 default:
2865 pr_err("received unknown message type %d %s\n", type,
2866 ceph_msg_type_name(type));
2867 }
Sage Weil4a32f932010-06-13 10:27:53 -07002868out:
Sage Weilf24e9982009-10-06 11:31:10 -07002869 ceph_msg_put(msg);
2870}
2871
Sage Weil5b3a4db2010-02-19 21:43:23 -08002872/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002873 * Lookup and return message for incoming reply. Don't try to do
2874 * anything about a larger than preallocated data portion of the
2875 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002876 */
2877static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002878 struct ceph_msg_header *hdr,
2879 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002880{
2881 struct ceph_osd *osd = con->private;
2882 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002883 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002884 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002885 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002886 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002887 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002888
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002889 tid = le64_to_cpu(hdr->tid);
2890 mutex_lock(&osdc->request_mutex);
2891 req = __lookup_request(osdc, tid);
2892 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002893 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2894 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002895 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002896 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002897 goto out;
2898 }
Sage Weilc16e7862010-03-01 13:02:00 -08002899
Alex Elderace6d3a2013-04-01 16:12:14 -05002900 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002901
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002902 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002903 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2904 __func__, osd->o_osd, req->r_tid, front_len,
2905 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002906 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2907 false);
Sage Weila79832f2010-04-01 16:06:19 -07002908 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002909 goto out;
2910 ceph_msg_put(req->r_reply);
2911 req->r_reply = m;
2912 }
Sage Weilc16e7862010-03-01 13:02:00 -08002913
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002914 if (data_len > req->r_reply->data_length) {
2915 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2916 __func__, osd->o_osd, req->r_tid, data_len,
2917 req->r_reply->data_length);
2918 m = NULL;
2919 *skip = 1;
2920 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002921 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002922
2923 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002924 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002925
2926out:
2927 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002928 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002929}
2930
2931static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2932 struct ceph_msg_header *hdr,
2933 int *skip)
2934{
2935 struct ceph_osd *osd = con->private;
2936 int type = le16_to_cpu(hdr->type);
2937 int front = le32_to_cpu(hdr->front_len);
2938
Alex Elder1c20f2d2012-06-04 14:43:32 -05002939 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002940 switch (type) {
2941 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002942 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002943 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002944 case CEPH_MSG_OSD_OPREPLY:
2945 return get_reply(con, hdr, skip);
2946 default:
2947 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2948 osd->o_osd);
2949 *skip = 1;
2950 return NULL;
2951 }
Sage Weilf24e9982009-10-06 11:31:10 -07002952}
2953
2954/*
2955 * Wrappers to refcount containing ceph_osd struct
2956 */
2957static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2958{
2959 struct ceph_osd *osd = con->private;
2960 if (get_osd(osd))
2961 return con;
2962 return NULL;
2963}
2964
2965static void put_osd_con(struct ceph_connection *con)
2966{
2967 struct ceph_osd *osd = con->private;
2968 put_osd(osd);
2969}
2970
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002971/*
2972 * authentication
2973 */
Alex Eldera3530df2012-05-16 15:16:39 -05002974/*
2975 * Note: returned pointer is the address of a structure that's
2976 * managed separately. Caller must *not* attempt to free it.
2977 */
2978static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002979 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002980{
2981 struct ceph_osd *o = con->private;
2982 struct ceph_osd_client *osdc = o->o_osdc;
2983 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002984 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002985
Alex Elder74f18692012-05-16 15:16:39 -05002986 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002987 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002988 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002989 }
Sage Weil27859f92013-03-25 10:26:14 -07002990 if (!auth->authorizer) {
2991 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2992 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002993 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002994 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002995 } else {
2996 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002997 auth);
2998 if (ret)
2999 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003000 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003001 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05003002
Alex Eldera3530df2012-05-16 15:16:39 -05003003 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003004}
3005
3006
3007static int verify_authorizer_reply(struct ceph_connection *con, int len)
3008{
3009 struct ceph_osd *o = con->private;
3010 struct ceph_osd_client *osdc = o->o_osdc;
3011 struct ceph_auth_client *ac = osdc->client->monc.auth;
3012
Sage Weil27859f92013-03-25 10:26:14 -07003013 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003014}
3015
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003016static int invalidate_authorizer(struct ceph_connection *con)
3017{
3018 struct ceph_osd *o = con->private;
3019 struct ceph_osd_client *osdc = o->o_osdc;
3020 struct ceph_auth_client *ac = osdc->client->monc.auth;
3021
Sage Weil27859f92013-03-25 10:26:14 -07003022 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003023 return ceph_monc_validate_auth(&osdc->client->monc);
3024}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003025
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003026static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003027{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003028 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003029 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003030
Yan, Zheng33d07332014-11-04 16:33:37 +08003031 return ceph_auth_sign_message(auth, msg);
3032}
3033
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003034static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003035{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003036 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003037 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003038
Yan, Zheng33d07332014-11-04 16:33:37 +08003039 return ceph_auth_check_message_signature(auth, msg);
3040}
3041
Tobias Klauser9e327892010-05-20 10:40:19 +02003042static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003043 .get = get_osd_con,
3044 .put = put_osd_con,
3045 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003046 .get_authorizer = get_authorizer,
3047 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003048 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003049 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003050 .sign_message = osd_sign_message,
3051 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003052 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003053};