blob: f93d0e893f3f50ff777e426de26374e07035ba24 [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);
341 else
Alex Elder5522ae02013-05-01 12:43:04 -0500342 kmem_cache_free(ceph_osd_request_cache, req);
343
Sage Weilf24e9982009-10-06 11:31:10 -0700344}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400345
346void ceph_osdc_get_request(struct ceph_osd_request *req)
347{
348 dout("%s %p (was %d)\n", __func__, req,
349 atomic_read(&req->r_kref.refcount));
350 kref_get(&req->r_kref);
351}
352EXPORT_SYMBOL(ceph_osdc_get_request);
353
354void ceph_osdc_put_request(struct ceph_osd_request *req)
355{
356 dout("%s %p (was %d)\n", __func__, req,
357 atomic_read(&req->r_kref.refcount));
358 kref_put(&req->r_kref, ceph_osdc_release_request);
359}
360EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700361
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700362struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800364 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600366 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367{
368 struct ceph_osd_request *req;
369 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800370 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700371
Alex Elder79528732013-04-03 21:32:51 -0500372 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
373 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
374
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375 if (use_mempool) {
376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
377 memset(req, 0, sizeof(*req));
378 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700380 }
381 if (req == NULL)
382 return NULL;
383
384 req->r_osdc = osdc;
385 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500386 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700387
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388 kref_init(&req->r_kref);
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600391 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700393 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400394 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700395 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700396 INIT_LIST_HEAD(&req->r_osd_item);
397
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200398 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200399 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200400
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 /* create reply message */
402 if (use_mempool)
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
404 else
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700407 if (!msg) {
408 ceph_osdc_put_request(req);
409 return NULL;
410 }
411 req->r_reply = msg;
412
Ilya Dryomovae458f52016-02-11 13:09:15 +0100413 msg_size = 4 + 4 + 4; /* client_inc, osdmap_epoch, flags */
414 msg_size += 4 + 4 + 4 + 8; /* mtime, reassert_version */
415 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
416 msg_size += 1 + 8 + 4 + 4; /* pgid */
417 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
418 msg_size += 2 + num_ops * sizeof(struct ceph_osd_op);
419 msg_size += 8; /* snapid */
420 msg_size += 8; /* snap_seq */
421 msg_size += 4 + 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
422 msg_size += 4; /* retry_attempt */
423
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700424 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700425 if (use_mempool)
426 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
427 else
Sage Weilb61c2762011-08-09 15:03:46 -0700428 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700429 if (!msg) {
430 ceph_osdc_put_request(req);
431 return NULL;
432 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700433
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700434 memset(msg->front.iov_base, 0, msg->front.iov_len);
435
436 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700437
438 return req;
439}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700440EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700441
Alex Eldera8dd0a32013-03-13 20:50:00 -0500442static bool osd_req_opcode_valid(u16 opcode)
443{
444 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400445#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
446__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
447#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500448 default:
449 return false;
450 }
451}
452
Alex Elder33803f32013-03-13 20:50:00 -0500453/*
454 * This is an osd op init function for opcodes that have no data or
455 * other information associated with them. It also serves as a
456 * common init routine for all the other init functions, below.
457 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500458static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600459_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800460 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500461{
Alex Elderc99d2d42013-04-05 01:27:11 -0500462 struct ceph_osd_req_op *op;
463
464 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500465 BUG_ON(!osd_req_opcode_valid(opcode));
466
Alex Elderc99d2d42013-04-05 01:27:11 -0500467 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500468 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500469 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800470 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500471
472 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500473}
474
Alex Elder49719772013-02-11 12:33:24 -0600475void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800476 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600477{
Yan, Zheng144cba12015-04-27 11:09:54 +0800478 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600479}
480EXPORT_SYMBOL(osd_req_op_init);
481
Alex Elderc99d2d42013-04-05 01:27:11 -0500482void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
483 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500484 u64 offset, u64 length,
485 u64 truncate_size, u32 truncate_seq)
486{
Yan, Zheng144cba12015-04-27 11:09:54 +0800487 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
488 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500489 size_t payload_len = 0;
490
Li Wangad7a60d2013-08-15 11:51:44 +0800491 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200492 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
493 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500494
Alex Elder33803f32013-03-13 20:50:00 -0500495 op->extent.offset = offset;
496 op->extent.length = length;
497 op->extent.truncate_size = truncate_size;
498 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200499 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500500 payload_len += length;
501
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100502 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500503}
504EXPORT_SYMBOL(osd_req_op_extent_init);
505
Alex Elderc99d2d42013-04-05 01:27:11 -0500506void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
507 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500508{
Alex Elderc99d2d42013-04-05 01:27:11 -0500509 struct ceph_osd_req_op *op;
510 u64 previous;
511
512 BUG_ON(which >= osd_req->r_num_ops);
513 op = &osd_req->r_ops[which];
514 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500515
516 if (length == previous)
517 return; /* Nothing to do */
518 BUG_ON(length > previous);
519
520 op->extent.length = length;
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100521 op->indata_len -= previous - length;
Alex Eldere5975c72013-03-14 14:09:05 -0500522}
523EXPORT_SYMBOL(osd_req_op_extent_update);
524
Alex Elderc99d2d42013-04-05 01:27:11 -0500525void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500526 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500527{
Yan, Zheng144cba12015-04-27 11:09:54 +0800528 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
529 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500530 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500531 size_t payload_len = 0;
532 size_t size;
533
534 BUG_ON(opcode != CEPH_OSD_OP_CALL);
535
Alex Elder5f562df2013-04-05 01:27:12 -0500536 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
537 BUG_ON(!pagelist);
538 ceph_pagelist_init(pagelist);
539
Alex Elder33803f32013-03-13 20:50:00 -0500540 op->cls.class_name = class;
541 size = strlen(class);
542 BUG_ON(size > (size_t) U8_MAX);
543 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500544 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500545 payload_len += size;
546
547 op->cls.method_name = method;
548 size = strlen(method);
549 BUG_ON(size > (size_t) U8_MAX);
550 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500551 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500552 payload_len += size;
553
Alex Eldera4ce40a2013-04-05 01:27:12 -0500554 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500555
Alex Elder33803f32013-03-13 20:50:00 -0500556 op->cls.argc = 0; /* currently unused */
557
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100558 op->indata_len = payload_len;
Alex Elder33803f32013-03-13 20:50:00 -0500559}
560EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500561
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800562int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
563 u16 opcode, const char *name, const void *value,
564 size_t size, u8 cmp_op, u8 cmp_mode)
565{
Yan, Zheng144cba12015-04-27 11:09:54 +0800566 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
567 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800568 struct ceph_pagelist *pagelist;
569 size_t payload_len;
570
571 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
572
573 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
574 if (!pagelist)
575 return -ENOMEM;
576
577 ceph_pagelist_init(pagelist);
578
579 payload_len = strlen(name);
580 op->xattr.name_len = payload_len;
581 ceph_pagelist_append(pagelist, name, payload_len);
582
583 op->xattr.value_len = size;
584 ceph_pagelist_append(pagelist, value, size);
585 payload_len += size;
586
587 op->xattr.cmp_op = cmp_op;
588 op->xattr.cmp_mode = cmp_mode;
589
590 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100591 op->indata_len = payload_len;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800592 return 0;
593}
594EXPORT_SYMBOL(osd_req_op_xattr_init);
595
Alex Elderc99d2d42013-04-05 01:27:11 -0500596void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
597 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500598 u64 cookie, u64 version, int flag)
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);
Alex Elder33803f32013-03-13 20:50:00 -0500602
Alex Elderc99d2d42013-04-05 01:27:11 -0500603 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500604
605 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500606 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500607 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500608 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500609}
610EXPORT_SYMBOL(osd_req_op_watch_init);
611
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200612void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
613 unsigned int which,
614 u64 expected_object_size,
615 u64 expected_write_size)
616{
617 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800618 CEPH_OSD_OP_SETALLOCHINT,
619 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200620
621 op->alloc_hint.expected_object_size = expected_object_size;
622 op->alloc_hint.expected_write_size = expected_write_size;
623
624 /*
625 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
626 * not worth a feature bit. Set FAILOK per-op flag to make
627 * sure older osds don't trip over an unsupported opcode.
628 */
629 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
630}
631EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
632
Alex Elder90af3602013-04-05 14:46:01 -0500633static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500634 struct ceph_osd_data *osd_data)
635{
636 u64 length = ceph_osd_data_length(osd_data);
637
638 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
639 BUG_ON(length > (u64) SIZE_MAX);
640 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500641 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500642 length, osd_data->alignment);
643 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
644 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500645 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500646#ifdef CONFIG_BLOCK
647 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500648 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500649#endif
650 } else {
651 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
652 }
653}
654
Alex Elder175face2013-03-08 13:35:36 -0600655static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500656 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700657{
Alex Elder79528732013-04-03 21:32:51 -0500658 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500659 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500660 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500661 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600662
Alex Elder79528732013-04-03 21:32:51 -0500663 BUG_ON(which >= req->r_num_ops);
664 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500665 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
666 pr_err("unrecognized osd opcode %d\n", src->op);
667
668 return 0;
669 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700670
Alex Elder065a68f2012-04-20 15:49:43 -0500671 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600672 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600673 osd_data = &src->raw_data_in;
674 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600675 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700676 case CEPH_OSD_OP_READ:
677 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200678 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800679 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800680 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200681 if (src->op == CEPH_OSD_OP_WRITE ||
682 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500683 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600684 dst->extent.offset = cpu_to_le64(src->extent.offset);
685 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700686 dst->extent.truncate_size =
687 cpu_to_le64(src->extent.truncate_size);
688 dst->extent.truncate_seq =
689 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500690 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200691 if (src->op == CEPH_OSD_OP_WRITE ||
692 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500693 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500694 else
Alex Elder04017e22013-04-05 14:46:02 -0500695 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700696 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700697 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700698 dst->cls.class_len = src->cls.class_len;
699 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500700 osd_data = &src->cls.request_info;
701 ceph_osdc_msg_data_add(req->r_request, osd_data);
702 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
703 request_data_len = osd_data->pagelist->length;
704
705 osd_data = &src->cls.request_data;
706 data_length = ceph_osd_data_length(osd_data);
707 if (data_length) {
708 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
709 dst->cls.indata_len = cpu_to_le32(data_length);
710 ceph_osdc_msg_data_add(req->r_request, osd_data);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100711 src->indata_len += data_length;
Alex Elder04017e22013-04-05 14:46:02 -0500712 request_data_len += data_length;
713 }
714 osd_data = &src->cls.response_data;
715 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700716 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700717 case CEPH_OSD_OP_STARTSYNC:
718 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700719 case CEPH_OSD_OP_NOTIFY_ACK:
720 case CEPH_OSD_OP_WATCH:
721 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
722 dst->watch.ver = cpu_to_le64(src->watch.ver);
723 dst->watch.flag = src->watch.flag;
724 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200725 case CEPH_OSD_OP_SETALLOCHINT:
726 dst->alloc_hint.expected_object_size =
727 cpu_to_le64(src->alloc_hint.expected_object_size);
728 dst->alloc_hint.expected_write_size =
729 cpu_to_le64(src->alloc_hint.expected_write_size);
730 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800731 case CEPH_OSD_OP_SETXATTR:
732 case CEPH_OSD_OP_CMPXATTR:
733 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
734 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
735 dst->xattr.cmp_op = src->xattr.cmp_op;
736 dst->xattr.cmp_mode = src->xattr.cmp_mode;
737 osd_data = &src->xattr.osd_data;
738 ceph_osdc_msg_data_add(req->r_request, osd_data);
739 request_data_len = osd_data->pagelist->length;
740 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800741 case CEPH_OSD_OP_CREATE:
742 case CEPH_OSD_OP_DELETE:
743 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700744 default:
Alex Elder4c464592013-02-15 11:42:30 -0600745 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600746 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600747 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500748
749 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700750 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200751
Alex Eldera8dd0a32013-03-13 20:50:00 -0500752 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200753 dst->flags = cpu_to_le32(src->flags);
Ilya Dryomovde2aa102016-02-08 13:39:46 +0100754 dst->payload_len = cpu_to_le32(src->indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600755
Alex Elder54d50642013-04-03 01:28:58 -0500756 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700757}
758
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700759/*
Sage Weilf24e9982009-10-06 11:31:10 -0700760 * build new request AND message, calculate layout, and adjust file
761 * extent as needed.
762 *
763 * if the file was recently truncated, we include information about its
764 * old and new size so that the object can be updated appropriately. (we
765 * avoid synchronously deleting truncated objects because it's slow.)
766 *
767 * if @do_sync, include a 'startsync' command so that the osd will flush
768 * data quickly.
769 */
770struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
771 struct ceph_file_layout *layout,
772 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800773 u64 off, u64 *plen,
774 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700775 int opcode, int flags,
776 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700777 u32 truncate_seq,
778 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600779 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700780{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700781 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500782 u64 objnum = 0;
783 u64 objoff = 0;
784 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700785 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700786
Li Wangad7a60d2013-08-15 11:51:44 +0800787 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800788 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
789 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700790
Alex Elderacead002013-03-14 14:09:05 -0500791 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600792 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700793 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700794 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500795
Alex Elderd178a9e2012-11-13 21:11:15 -0600796 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700797
798 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500799 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600800 if (r < 0) {
801 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700802 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600803 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500804
Yan, Zheng864e9192014-11-13 10:47:25 +0800805 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800806 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800807 } else {
808 u32 object_size = le32_to_cpu(layout->fl_object_size);
809 u32 object_base = off - objoff;
810 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
811 if (truncate_size <= object_base) {
812 truncate_size = 0;
813 } else {
814 truncate_size -= object_base;
815 if (truncate_size > object_size)
816 truncate_size = object_size;
817 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800818 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800819 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800820 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500821 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500822
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200823 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700824
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200825 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200826 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200827 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600828
Sage Weilf24e9982009-10-06 11:31:10 -0700829 return req;
830}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700831EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700832
833/*
834 * We keep osd requests in an rbtree, sorted by ->r_tid.
835 */
836static void __insert_request(struct ceph_osd_client *osdc,
837 struct ceph_osd_request *new)
838{
839 struct rb_node **p = &osdc->requests.rb_node;
840 struct rb_node *parent = NULL;
841 struct ceph_osd_request *req = NULL;
842
843 while (*p) {
844 parent = *p;
845 req = rb_entry(parent, struct ceph_osd_request, r_node);
846 if (new->r_tid < req->r_tid)
847 p = &(*p)->rb_left;
848 else if (new->r_tid > req->r_tid)
849 p = &(*p)->rb_right;
850 else
851 BUG();
852 }
853
854 rb_link_node(&new->r_node, parent, p);
855 rb_insert_color(&new->r_node, &osdc->requests);
856}
857
858static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
859 u64 tid)
860{
861 struct ceph_osd_request *req;
862 struct rb_node *n = osdc->requests.rb_node;
863
864 while (n) {
865 req = rb_entry(n, struct ceph_osd_request, r_node);
866 if (tid < req->r_tid)
867 n = n->rb_left;
868 else if (tid > req->r_tid)
869 n = n->rb_right;
870 else
871 return req;
872 }
873 return NULL;
874}
875
876static struct ceph_osd_request *
877__lookup_request_ge(struct ceph_osd_client *osdc,
878 u64 tid)
879{
880 struct ceph_osd_request *req;
881 struct rb_node *n = osdc->requests.rb_node;
882
883 while (n) {
884 req = rb_entry(n, struct ceph_osd_request, r_node);
885 if (tid < req->r_tid) {
886 if (!n->rb_left)
887 return req;
888 n = n->rb_left;
889 } else if (tid > req->r_tid) {
890 n = n->rb_right;
891 } else {
892 return req;
893 }
894 }
895 return NULL;
896}
897
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400898static void __kick_linger_request(struct ceph_osd_request *req)
899{
900 struct ceph_osd_client *osdc = req->r_osdc;
901 struct ceph_osd *osd = req->r_osd;
902
903 /*
904 * Linger requests need to be resent with a new tid to avoid
905 * the dup op detection logic on the OSDs. Achieve this with
906 * a re-register dance instead of open-coding.
907 */
908 ceph_osdc_get_request(req);
909 if (!list_empty(&req->r_linger_item))
910 __unregister_linger_request(osdc, req);
911 else
912 __unregister_request(osdc, req);
913 __register_request(osdc, req);
914 ceph_osdc_put_request(req);
915
916 /*
917 * Unless request has been registered as both normal and
918 * lingering, __unregister{,_linger}_request clears r_osd.
919 * However, here we need to preserve r_osd to make sure we
920 * requeue on the same OSD.
921 */
922 WARN_ON(req->r_osd || !osd);
923 req->r_osd = osd;
924
925 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
926 __enqueue_request(req);
927}
928
Sage Weil6f6c7002011-01-17 20:34:08 -0800929/*
930 * Resubmit requests pending on the given osd.
931 */
932static void __kick_osd_requests(struct ceph_osd_client *osdc,
933 struct ceph_osd *osd)
934{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700935 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500936 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400937 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800938 int err;
939
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400940 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800941 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600942 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800943 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400944
Alex Eldere02493c2013-03-25 18:16:11 -0500945 /*
946 * Build up a list of requests to resend by traversing the
947 * osd's list of requests. Requests for a given object are
948 * sent in tid order, and that is also the order they're
949 * kept on this list. Therefore all requests that are in
950 * flight will be found first, followed by all requests that
951 * have not yet been sent. And to resend requests while
952 * preserving this order we will want to put any sent
953 * requests back on the front of the osd client's unsent
954 * list.
955 *
956 * So we build a separate ordered list of already-sent
957 * requests for the affected osd and splice it onto the
958 * front of the osd client's unsent list. Once we've seen a
959 * request that has not yet been sent we're done. Those
960 * requests are already sitting right where they belong.
961 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800962 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500963 if (!req->r_sent)
964 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400965
966 if (!req->r_linger) {
967 dout("%s requeueing %p tid %llu\n", __func__, req,
968 req->r_tid);
969 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700970 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400971 } else {
972 list_move_tail(&req->r_req_lru_item, &resend_linger);
973 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700974 }
Alex Eldere02493c2013-03-25 18:16:11 -0500975 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700976
Alex Eldere02493c2013-03-25 18:16:11 -0500977 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400978 * Both registered and not yet registered linger requests are
979 * enqueued with a new tid on the same OSD. We add/move them
980 * to req_unsent/o_requests at the end to keep things in tid
981 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500982 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700983 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400984 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400985 WARN_ON(!list_empty(&req->r_req_lru_item));
986 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800987 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400988
989 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
990 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800991}
992
Sage Weilf24e9982009-10-06 11:31:10 -0700993/*
Sage Weil81b024e2009-10-09 10:29:18 -0700994 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700995 */
996static void osd_reset(struct ceph_connection *con)
997{
998 struct ceph_osd *osd = con->private;
999 struct ceph_osd_client *osdc;
1000
1001 if (!osd)
1002 return;
1003 dout("osd_reset osd%d\n", osd->o_osd);
1004 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001005 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001006 mutex_lock(&osdc->request_mutex);
1007 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001008 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001009 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 up_read(&osdc->map_sem);
1011}
1012
1013/*
1014 * Track open sessions with osds.
1015 */
Alex Eldere10006f2012-05-26 23:26:43 -05001016static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001017{
1018 struct ceph_osd *osd;
1019
1020 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1021 if (!osd)
1022 return NULL;
1023
1024 atomic_set(&osd->o_ref, 1);
1025 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001026 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001027 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001028 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001029 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001030 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001031 osd->o_incarnation = 1;
1032
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001033 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001034
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001035 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001036 return osd;
1037}
1038
1039static struct ceph_osd *get_osd(struct ceph_osd *osd)
1040{
1041 if (atomic_inc_not_zero(&osd->o_ref)) {
1042 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1043 atomic_read(&osd->o_ref));
1044 return osd;
1045 } else {
1046 dout("get_osd %p FAIL\n", osd);
1047 return NULL;
1048 }
1049}
1050
1051static void put_osd(struct ceph_osd *osd)
1052{
1053 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1054 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001055 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001056 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1057
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001058 if (osd->o_auth.authorizer)
1059 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001060 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001061 }
Sage Weilf24e9982009-10-06 11:31:10 -07001062}
1063
1064/*
1065 * remove an osd from our map
1066 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001067static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001068{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001069 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001070 WARN_ON(!list_empty(&osd->o_requests));
1071 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001072
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001073 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001074 rb_erase(&osd->o_node, &osdc->osds);
1075 RB_CLEAR_NODE(&osd->o_node);
1076}
1077
1078static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1079{
1080 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1081
1082 if (!RB_EMPTY_NODE(&osd->o_node)) {
1083 ceph_con_close(&osd->o_con);
1084 __remove_osd(osdc, osd);
1085 put_osd(osd);
1086 }
Sage Weilf24e9982009-10-06 11:31:10 -07001087}
1088
Sage Weilaca420b2011-08-31 14:45:53 -07001089static void remove_all_osds(struct ceph_osd_client *osdc)
1090{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001091 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001092 mutex_lock(&osdc->request_mutex);
1093 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1094 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1095 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001096 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001097 }
1098 mutex_unlock(&osdc->request_mutex);
1099}
1100
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001101static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1102 struct ceph_osd *osd)
1103{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001104 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001105 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001106
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001107 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001108 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001109}
1110
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001111static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1112 struct ceph_osd *osd)
1113{
1114 dout("%s %p\n", __func__, osd);
1115
1116 if (list_empty(&osd->o_requests) &&
1117 list_empty(&osd->o_linger_requests))
1118 __move_osd_to_lru(osdc, osd);
1119}
1120
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001121static void __remove_osd_from_lru(struct ceph_osd *osd)
1122{
1123 dout("__remove_osd_from_lru %p\n", osd);
1124 if (!list_empty(&osd->o_osd_lru))
1125 list_del_init(&osd->o_osd_lru);
1126}
1127
Sage Weilaca420b2011-08-31 14:45:53 -07001128static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001129{
1130 struct ceph_osd *osd, *nosd;
1131
1132 dout("__remove_old_osds %p\n", osdc);
1133 mutex_lock(&osdc->request_mutex);
1134 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001135 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001136 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001137 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001138 }
1139 mutex_unlock(&osdc->request_mutex);
1140}
1141
Sage Weilf24e9982009-10-06 11:31:10 -07001142/*
1143 * reset osd connect
1144 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001145static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001146{
Alex Elderc3acb182012-12-07 09:57:58 -06001147 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001148
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001149 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001150 if (list_empty(&osd->o_requests) &&
1151 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001152 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001153 return -ENODEV;
1154 }
1155
1156 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1157 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1158 !ceph_con_opened(&osd->o_con)) {
1159 struct ceph_osd_request *req;
1160
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001161 dout("osd addr hasn't changed and connection never opened, "
1162 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001163 /* touch each r_stamp for handle_timeout()'s benfit */
1164 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1165 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001166
1167 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001168 }
Alex Elderc3acb182012-12-07 09:57:58 -06001169
1170 ceph_con_close(&osd->o_con);
1171 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1172 osd->o_incarnation++;
1173
1174 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001175}
1176
1177static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1178{
1179 struct rb_node **p = &osdc->osds.rb_node;
1180 struct rb_node *parent = NULL;
1181 struct ceph_osd *osd = NULL;
1182
Sage Weilaca420b2011-08-31 14:45:53 -07001183 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001184 while (*p) {
1185 parent = *p;
1186 osd = rb_entry(parent, struct ceph_osd, o_node);
1187 if (new->o_osd < osd->o_osd)
1188 p = &(*p)->rb_left;
1189 else if (new->o_osd > osd->o_osd)
1190 p = &(*p)->rb_right;
1191 else
1192 BUG();
1193 }
1194
1195 rb_link_node(&new->o_node, parent, p);
1196 rb_insert_color(&new->o_node, &osdc->osds);
1197}
1198
1199static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1200{
1201 struct ceph_osd *osd;
1202 struct rb_node *n = osdc->osds.rb_node;
1203
1204 while (n) {
1205 osd = rb_entry(n, struct ceph_osd, o_node);
1206 if (o < osd->o_osd)
1207 n = n->rb_left;
1208 else if (o > osd->o_osd)
1209 n = n->rb_right;
1210 else
1211 return osd;
1212 }
1213 return NULL;
1214}
1215
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001216static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1217{
1218 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001219 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001220}
1221
1222static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1223{
1224 cancel_delayed_work(&osdc->timeout_work);
1225}
Sage Weilf24e9982009-10-06 11:31:10 -07001226
1227/*
1228 * Register request, assign tid. If this is the first request, set up
1229 * the timeout event.
1230 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001231static void __register_request(struct ceph_osd_client *osdc,
1232 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001233{
Sage Weilf24e9982009-10-06 11:31:10 -07001234 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001235 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001236 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001237 __insert_request(osdc, req);
1238 ceph_osdc_get_request(req);
1239 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001240 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001241 dout(" first request, scheduling timeout\n");
1242 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001243 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001244}
1245
Sage Weilf24e9982009-10-06 11:31:10 -07001246/*
1247 * called under osdc->request_mutex
1248 */
1249static void __unregister_request(struct ceph_osd_client *osdc,
1250 struct ceph_osd_request *req)
1251{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001252 if (RB_EMPTY_NODE(&req->r_node)) {
1253 dout("__unregister_request %p tid %lld not registered\n",
1254 req, req->r_tid);
1255 return;
1256 }
1257
Sage Weilf24e9982009-10-06 11:31:10 -07001258 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1259 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001260 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001261 osdc->num_requests--;
1262
Sage Weil0ba64782009-10-08 16:57:16 -07001263 if (req->r_osd) {
1264 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001265 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001266
1267 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001268 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001269 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001270 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001271 }
Sage Weilf24e9982009-10-06 11:31:10 -07001272
Alex Elder7d5f2482012-11-29 08:37:03 -06001273 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001274 ceph_osdc_put_request(req);
1275
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001276 if (osdc->num_requests == 0) {
1277 dout(" no requests, canceling timeout\n");
1278 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001279 }
1280}
1281
1282/*
1283 * Cancel a previously queued request message
1284 */
1285static void __cancel_request(struct ceph_osd_request *req)
1286{
Sage Weil6bc18872010-09-27 10:18:52 -07001287 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001288 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001289 req->r_sent = 0;
1290 }
1291}
1292
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001293static void __register_linger_request(struct ceph_osd_client *osdc,
1294 struct ceph_osd_request *req)
1295{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001296 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1297 WARN_ON(!req->r_linger);
1298
Alex Elder96e4dac2013-05-22 20:54:25 -05001299 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001300 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001301 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001302 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001303 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001304}
1305
1306static void __unregister_linger_request(struct ceph_osd_client *osdc,
1307 struct ceph_osd_request *req)
1308{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001309 WARN_ON(!req->r_linger);
1310
1311 if (list_empty(&req->r_linger_item)) {
1312 dout("%s %p tid %llu not registered\n", __func__, req,
1313 req->r_tid);
1314 return;
1315 }
1316
1317 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001318 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001319
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001320 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001321 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001322 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001323 if (list_empty(&req->r_osd_item))
1324 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001325 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001326 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001327}
1328
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001329void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1330 struct ceph_osd_request *req)
1331{
1332 if (!req->r_linger) {
1333 dout("set_request_linger %p\n", req);
1334 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001335 }
1336}
1337EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1338
Sage Weilf24e9982009-10-06 11:31:10 -07001339/*
Josh Durgind29adb32013-12-02 19:11:48 -08001340 * Returns whether a request should be blocked from being sent
1341 * based on the current osdmap and osd_client settings.
1342 *
1343 * Caller should hold map_sem for read.
1344 */
1345static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1346 struct ceph_osd_request *req)
1347{
1348 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1349 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1350 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1351 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1352 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1353}
1354
1355/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001356 * Calculate mapping of a request to a PG. Takes tiering into account.
1357 */
1358static int __calc_request_pg(struct ceph_osdmap *osdmap,
1359 struct ceph_osd_request *req,
1360 struct ceph_pg *pg_out)
1361{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001362 bool need_check_tiering;
1363
1364 need_check_tiering = false;
1365 if (req->r_target_oloc.pool == -1) {
1366 req->r_target_oloc = req->r_base_oloc; /* struct */
1367 need_check_tiering = true;
1368 }
1369 if (req->r_target_oid.name_len == 0) {
1370 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1371 need_check_tiering = true;
1372 }
1373
1374 if (need_check_tiering &&
1375 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001376 struct ceph_pg_pool_info *pi;
1377
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001378 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001379 if (pi) {
1380 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1381 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001382 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001383 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1384 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001385 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001386 }
1387 /* !pi is caught in ceph_oloc_oid_to_pg() */
1388 }
1389
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001390 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1391 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001392}
1393
Ilya Dryomovf671b582014-09-02 13:40:33 +04001394static void __enqueue_request(struct ceph_osd_request *req)
1395{
1396 struct ceph_osd_client *osdc = req->r_osdc;
1397
1398 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1399 req->r_osd ? req->r_osd->o_osd : -1);
1400
1401 if (req->r_osd) {
1402 __remove_osd_from_lru(req->r_osd);
1403 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1404 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1405 } else {
1406 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1407 }
1408}
1409
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001410/*
Sage Weilf24e9982009-10-06 11:31:10 -07001411 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1412 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001413 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001414 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001415 *
1416 * Return 0 if unchanged, 1 if changed, or negative on error.
1417 *
1418 * Caller should hold map_sem for read and request_mutex.
1419 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001420static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001421 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001422{
Sage Weil5b191d92013-02-23 10:38:16 -08001423 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001424 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001425 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001426 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001427 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001428
Sage Weil6f6c7002011-01-17 20:34:08 -08001429 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001430
1431 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001432 if (err) {
1433 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001434 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001435 }
Sage Weil7740a422010-01-08 15:58:25 -08001436 req->r_pgid = pgid;
1437
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001438 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1439 if (num < 0)
1440 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001441
Josh Durgind29adb32013-12-02 19:11:48 -08001442 was_paused = req->r_paused;
1443 req->r_paused = __req_should_be_paused(osdc, req);
1444 if (was_paused && !req->r_paused)
1445 force_resend = 1;
1446
Sage Weil38d64532011-10-14 13:33:55 -07001447 if ((!force_resend &&
1448 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001449 req->r_sent >= req->r_osd->o_incarnation &&
1450 req->r_num_pg_osds == num &&
1451 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001452 (req->r_osd == NULL && o == -1) ||
1453 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001454 return 0; /* no change */
1455
Sage Weil5b191d92013-02-23 10:38:16 -08001456 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1457 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001458 req->r_osd ? req->r_osd->o_osd : -1);
1459
Sage Weild85b7052010-05-10 10:24:48 -07001460 /* record full pg acting set */
1461 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1462 req->r_num_pg_osds = num;
1463
Sage Weilf24e9982009-10-06 11:31:10 -07001464 if (req->r_osd) {
1465 __cancel_request(req);
1466 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001467 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001468 req->r_osd = NULL;
1469 }
1470
1471 req->r_osd = __lookup_osd(osdc, o);
1472 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001473 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001474 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001475 if (!req->r_osd) {
1476 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001477 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001478 }
Sage Weilf24e9982009-10-06 11:31:10 -07001479
Sage Weil6f6c7002011-01-17 20:34:08 -08001480 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001481 __insert_osd(osdc, req->r_osd);
1482
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001483 ceph_con_open(&req->r_osd->o_con,
1484 CEPH_ENTITY_TYPE_OSD, o,
1485 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001486 }
1487
Ilya Dryomovf671b582014-09-02 13:40:33 +04001488 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001489 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001490
1491out:
Sage Weilf24e9982009-10-06 11:31:10 -07001492 return err;
1493}
1494
1495/*
1496 * caller should hold map_sem (for read) and request_mutex
1497 */
Sage Weil56e925b2012-01-03 12:34:34 -08001498static void __send_request(struct ceph_osd_client *osdc,
1499 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001500{
Sage Weil1b83bef2013-02-25 16:11:12 -08001501 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001502
Sage Weil1b83bef2013-02-25 16:11:12 -08001503 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1504 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1505 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001506
Sage Weil1b83bef2013-02-25 16:11:12 -08001507 /* fill in message content that changes each time we send it */
1508 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1509 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001510 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001511 p = req->r_request_pgid;
1512 ceph_encode_64(&p, req->r_pgid.pool);
1513 ceph_encode_32(&p, req->r_pgid.seed);
1514 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1515 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1516 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001517
Sage Weil3dd72fc2010-03-22 14:42:30 -07001518 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001519 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001520
1521 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001522
Sage Weilf24e9982009-10-06 11:31:10 -07001523 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001524
1525 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001526}
1527
1528/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001529 * Send any requests in the queue (req_unsent).
1530 */
Alex Elderf9d25192013-02-15 11:42:29 -06001531static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001532{
1533 struct ceph_osd_request *req, *tmp;
1534
Alex Elderf9d25192013-02-15 11:42:29 -06001535 dout("__send_queued\n");
1536 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001537 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001538}
1539
1540/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001541 * Caller should hold map_sem for read and request_mutex.
1542 */
1543static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1544 struct ceph_osd_request *req,
1545 bool nofail)
1546{
1547 int rc;
1548
1549 __register_request(osdc, req);
1550 req->r_sent = 0;
1551 req->r_got_reply = 0;
1552 rc = __map_request(osdc, req, 0);
1553 if (rc < 0) {
1554 if (nofail) {
1555 dout("osdc_start_request failed map, "
1556 " will retry %lld\n", req->r_tid);
1557 rc = 0;
1558 } else {
1559 __unregister_request(osdc, req);
1560 }
1561 return rc;
1562 }
1563
1564 if (req->r_osd == NULL) {
1565 dout("send_request %p no up osds in pg\n", req);
1566 ceph_monc_request_next_osdmap(&osdc->client->monc);
1567 } else {
1568 __send_queued(osdc);
1569 }
1570
1571 return 0;
1572}
1573
1574/*
Sage Weilf24e9982009-10-06 11:31:10 -07001575 * Timeout callback, called every N seconds when 1 or more osd
1576 * requests has been active for more than N seconds. When this
1577 * happens, we ping all OSDs with requests who have timed out to
1578 * ensure any communications channel reset is detected. Reset the
1579 * request timeouts another N seconds in the future as we go.
1580 * Reschedule the timeout event another N seconds in future (unless
1581 * there are no open requests).
1582 */
1583static void handle_timeout(struct work_struct *work)
1584{
1585 struct ceph_osd_client *osdc =
1586 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001587 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001588 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001589 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001590 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001591 dout("timeout\n");
1592 down_read(&osdc->map_sem);
1593
1594 ceph_monc_request_next_osdmap(&osdc->client->monc);
1595
1596 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001597
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001598 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001599 * ping osds that are a bit slow. this ensures that if there
1600 * is a break in the TCP connection we will notice, and reopen
1601 * a connection with that osd (from the fault callback).
1602 */
1603 INIT_LIST_HEAD(&slow_osds);
1604 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001605 if (time_before(jiffies,
1606 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001607 break;
1608
1609 osd = req->r_osd;
1610 BUG_ON(!osd);
1611 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001612 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001613 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1614 }
1615 while (!list_empty(&slow_osds)) {
1616 osd = list_entry(slow_osds.next, struct ceph_osd,
1617 o_keepalive_item);
1618 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001619 ceph_con_keepalive(&osd->o_con);
1620 }
1621
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001622 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001623 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001624 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001625 up_read(&osdc->map_sem);
1626}
1627
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001628static void handle_osds_timeout(struct work_struct *work)
1629{
1630 struct ceph_osd_client *osdc =
1631 container_of(work, struct ceph_osd_client,
1632 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001633 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001634
1635 dout("osds timeout\n");
1636 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001637 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001638 up_read(&osdc->map_sem);
1639
1640 schedule_delayed_work(&osdc->osds_timeout_work,
1641 round_jiffies_relative(delay));
1642}
1643
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001644static int ceph_oloc_decode(void **p, void *end,
1645 struct ceph_object_locator *oloc)
1646{
1647 u8 struct_v, struct_cv;
1648 u32 len;
1649 void *struct_end;
1650 int ret = 0;
1651
1652 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1653 struct_v = ceph_decode_8(p);
1654 struct_cv = ceph_decode_8(p);
1655 if (struct_v < 3) {
1656 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1657 struct_v, struct_cv);
1658 goto e_inval;
1659 }
1660 if (struct_cv > 6) {
1661 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1662 struct_v, struct_cv);
1663 goto e_inval;
1664 }
1665 len = ceph_decode_32(p);
1666 ceph_decode_need(p, end, len, e_inval);
1667 struct_end = *p + len;
1668
1669 oloc->pool = ceph_decode_64(p);
1670 *p += 4; /* skip preferred */
1671
1672 len = ceph_decode_32(p);
1673 if (len > 0) {
1674 pr_warn("ceph_object_locator::key is set\n");
1675 goto e_inval;
1676 }
1677
1678 if (struct_v >= 5) {
1679 len = ceph_decode_32(p);
1680 if (len > 0) {
1681 pr_warn("ceph_object_locator::nspace is set\n");
1682 goto e_inval;
1683 }
1684 }
1685
1686 if (struct_v >= 6) {
1687 s64 hash = ceph_decode_64(p);
1688 if (hash != -1) {
1689 pr_warn("ceph_object_locator::hash is set\n");
1690 goto e_inval;
1691 }
1692 }
1693
1694 /* skip the rest */
1695 *p = struct_end;
1696out:
1697 return ret;
1698
1699e_inval:
1700 ret = -EINVAL;
1701 goto out;
1702}
1703
1704static int ceph_redirect_decode(void **p, void *end,
1705 struct ceph_request_redirect *redir)
1706{
1707 u8 struct_v, struct_cv;
1708 u32 len;
1709 void *struct_end;
1710 int ret;
1711
1712 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1713 struct_v = ceph_decode_8(p);
1714 struct_cv = ceph_decode_8(p);
1715 if (struct_cv > 1) {
1716 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1717 struct_v, struct_cv);
1718 goto e_inval;
1719 }
1720 len = ceph_decode_32(p);
1721 ceph_decode_need(p, end, len, e_inval);
1722 struct_end = *p + len;
1723
1724 ret = ceph_oloc_decode(p, end, &redir->oloc);
1725 if (ret)
1726 goto out;
1727
1728 len = ceph_decode_32(p);
1729 if (len > 0) {
1730 pr_warn("ceph_request_redirect::object_name is set\n");
1731 goto e_inval;
1732 }
1733
1734 len = ceph_decode_32(p);
1735 *p += len; /* skip osd_instructions */
1736
1737 /* skip the rest */
1738 *p = struct_end;
1739out:
1740 return ret;
1741
1742e_inval:
1743 ret = -EINVAL;
1744 goto out;
1745}
1746
Sage Weil25845472011-06-03 09:37:09 -07001747static void complete_request(struct ceph_osd_request *req)
1748{
Sage Weil25845472011-06-03 09:37:09 -07001749 complete_all(&req->r_safe_completion); /* fsync waiter */
1750}
1751
Sage Weilf24e9982009-10-06 11:31:10 -07001752/*
1753 * handle osd op reply. either call the callback if it is specified,
1754 * or do the completion to wake up the waiting thread.
1755 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301756static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001757{
Sage Weil1b83bef2013-02-25 16:11:12 -08001758 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001759 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001760 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001761 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001762 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001763 unsigned int numops;
1764 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001765 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001766 s32 retry_attempt;
1767 struct ceph_pg pg;
1768 int err;
1769 u32 reassert_epoch;
1770 u64 reassert_version;
1771 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001772 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001773 u32 bytes;
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001774 u8 decode_redir;
Alex Elder79528732013-04-03 21:32:51 -05001775 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001776
Sage Weil6df058c2009-12-22 11:24:33 -08001777 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001778 dout("handle_reply %p tid %llu\n", msg, tid);
1779
1780 p = msg->front.iov_base;
1781 end = p + msg->front.iov_len;
1782
1783 ceph_decode_need(&p, end, 4, bad);
1784 object_len = ceph_decode_32(&p);
1785 ceph_decode_need(&p, end, object_len, bad);
1786 p += object_len;
1787
Alex Elderef4859d2013-04-01 18:58:26 -05001788 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001789 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001790 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001791
1792 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1793 flags = ceph_decode_64(&p);
1794 result = ceph_decode_32(&p);
1795 reassert_epoch = ceph_decode_32(&p);
1796 reassert_version = ceph_decode_64(&p);
1797 osdmap_epoch = ceph_decode_32(&p);
1798
Sage Weilf24e9982009-10-06 11:31:10 -07001799 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001800 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001801 mutex_lock(&osdc->request_mutex);
1802 req = __lookup_request(osdc, tid);
1803 if (req == NULL) {
1804 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001805 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001806 }
1807 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001808
1809 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1810 req, result);
1811
Dan Carpenter18741192013-08-15 08:51:58 +03001812 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001813 numops = ceph_decode_32(&p);
1814 if (numops > CEPH_OSD_MAX_OP)
1815 goto bad_put;
1816 if (numops != req->r_num_ops)
1817 goto bad_put;
1818 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001819 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001820 for (i = 0; i < numops; i++) {
1821 struct ceph_osd_op *op = p;
1822 int len;
1823
1824 len = le32_to_cpu(op->payload_len);
Yan, Zheng7665d852016-01-07 16:48:57 +08001825 req->r_ops[i].outdata_len = len;
Sage Weil1b83bef2013-02-25 16:11:12 -08001826 dout(" op %d has %d bytes\n", i, len);
1827 payload_len += len;
1828 p += sizeof(*op);
1829 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001830 bytes = le32_to_cpu(msg->hdr.data_len);
1831 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001832 pr_warn("sum of op payload lens %d != data_len %d\n",
1833 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001834 goto bad_put;
1835 }
1836
Dan Carpenter18741192013-08-15 08:51:58 +03001837 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001838 retry_attempt = ceph_decode_32(&p);
1839 for (i = 0; i < numops; i++)
Yan, Zheng7665d852016-01-07 16:48:57 +08001840 req->r_ops[i].rval = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001841
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001842 if (le16_to_cpu(msg->hdr.version) >= 6) {
1843 p += 8 + 4; /* skip replay_version */
1844 p += 8; /* skip user_version */
1845
Ilya Dryomovb0b31a82016-02-03 15:25:48 +01001846 if (le16_to_cpu(msg->hdr.version) >= 7)
1847 ceph_decode_8_safe(&p, end, decode_redir, bad_put);
1848 else
1849 decode_redir = 1;
1850 } else {
1851 decode_redir = 0;
1852 }
1853
1854 if (decode_redir) {
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001855 err = ceph_redirect_decode(&p, end, &redir);
1856 if (err)
1857 goto bad_put;
1858 } else {
1859 redir.oloc.pool = -1;
1860 }
1861
1862 if (redir.oloc.pool != -1) {
1863 dout("redirect pool %lld\n", redir.oloc.pool);
1864
1865 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001866
1867 req->r_target_oloc = redir.oloc; /* struct */
1868
1869 /*
1870 * Start redirect requests with nofail=true. If
1871 * mapping fails, request will end up on the notarget
1872 * list, waiting for the new osdmap (which can take
1873 * a while), even though the original request mapped
1874 * successfully. In the future we might want to follow
1875 * original request's nofail setting here.
1876 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001877 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001878 BUG_ON(err);
1879
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001880 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001881 }
1882
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001883 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001884 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001885 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001886 dout("handle_reply result %d bytes %d\n", req->r_result,
1887 bytes);
1888 if (req->r_result == 0)
1889 req->r_result = bytes;
1890
1891 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001892 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1893 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001894
1895 req->r_got_reply = 1;
1896 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1897 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001898 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001899 }
1900
1901 dout("handle_reply tid %llu flags %d\n", tid, flags);
1902
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001903 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1904 __register_linger_request(osdc, req);
1905
Sage Weilf24e9982009-10-06 11:31:10 -07001906 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001907 if (result < 0 ||
1908 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001909 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1910 __unregister_request(osdc, req);
1911
1912 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001913 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001914
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001915 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001916 if (req->r_unsafe_callback &&
1917 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1918 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001919 if (req->r_callback)
1920 req->r_callback(req, msg);
1921 else
1922 complete_all(&req->r_completion);
1923 }
Sage Weilf24e9982009-10-06 11:31:10 -07001924
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001925 if (flags & CEPH_OSD_FLAG_ONDISK) {
1926 if (req->r_unsafe_callback && already_completed)
1927 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001928 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001929 }
Sage Weilf24e9982009-10-06 11:31:10 -07001930
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001931out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001932 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001933 ceph_osdc_put_request(req);
1934 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001935out_unlock:
1936 mutex_unlock(&osdc->request_mutex);
1937 up_read(&osdc->map_sem);
1938 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001939
Sage Weil1b83bef2013-02-25 16:11:12 -08001940bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001941 req->r_result = -EIO;
1942 __unregister_request(osdc, req);
1943 if (req->r_callback)
1944 req->r_callback(req, msg);
1945 else
1946 complete_all(&req->r_completion);
1947 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001948 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001949bad_mutex:
1950 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001951 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001952bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001953 pr_err("corrupt osd_op_reply got %d %d\n",
1954 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001955 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001956}
1957
Sage Weil6f6c7002011-01-17 20:34:08 -08001958static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001959{
Sage Weilf24e9982009-10-06 11:31:10 -07001960 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001961
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001962 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001963 for (p = rb_first(&osdc->osds); p; p = n) {
1964 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001965
Sage Weil6f6c7002011-01-17 20:34:08 -08001966 n = rb_next(p);
1967 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1968 memcmp(&osd->o_con.peer_addr,
1969 ceph_osd_addr(osdc->osdmap,
1970 osd->o_osd),
1971 sizeof(struct ceph_entity_addr)) != 0)
1972 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001973 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001974}
1975
1976/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001977 * Requeue requests whose mapping to an OSD has changed. If requests map to
1978 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001979 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001980 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001981 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001982static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1983 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001984{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001985 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001986 struct rb_node *p;
1987 int needmap = 0;
1988 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001989 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001990
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001991 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1992 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001993 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001994 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001995 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001996 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001997
1998 /*
1999 * For linger requests that have not yet been
2000 * registered, move them to the linger list; they'll
2001 * be sent to the osd in the loop below. Unregister
2002 * the request before re-registering it as a linger
2003 * request to ensure the __map_request() below
2004 * will decide it needs to be sent.
2005 */
2006 if (req->r_linger && list_empty(&req->r_linger_item)) {
2007 dout("%p tid %llu restart on osd%d\n",
2008 req, req->r_tid,
2009 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05002010 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002011 __unregister_request(osdc, req);
2012 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002013 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002014 continue;
2015 }
2016
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002017 force_resend_req = force_resend ||
2018 (force_resend_writes &&
2019 req->r_flags & CEPH_OSD_FLAG_WRITE);
2020 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002021 if (err < 0)
2022 continue; /* error */
2023 if (req->r_osd == NULL) {
2024 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2025 needmap++; /* request a newer map */
2026 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002027 if (!req->r_linger) {
2028 dout("%p tid %llu requeued on osd%d\n", req,
2029 req->r_tid,
2030 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002031 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002032 }
2033 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002034 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002035
2036 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2037 r_linger_item) {
2038 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2039
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002040 err = __map_request(osdc, req,
2041 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002042 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002043 if (err < 0)
2044 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002045 if (req->r_osd == NULL || err > 0) {
2046 if (req->r_osd == NULL) {
2047 dout("lingering %p tid %llu maps to no osd\n",
2048 req, req->r_tid);
2049 /*
2050 * A homeless lingering request makes
2051 * no sense, as it's job is to keep
2052 * a particular OSD connection open.
2053 * Request a newer map and kick the
2054 * request, knowing that it won't be
2055 * resent until we actually get a map
2056 * that can tell us where to send it.
2057 */
2058 needmap++;
2059 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002060
Ilya Dryomovb0494532015-05-11 17:53:10 +03002061 dout("kicking lingering %p tid %llu osd%d\n", req,
2062 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2063 __register_request(osdc, req);
2064 __unregister_linger_request(osdc, req);
2065 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002066 }
Alex Elder14d2f382013-05-15 16:28:33 -05002067 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002068 mutex_unlock(&osdc->request_mutex);
2069
2070 if (needmap) {
2071 dout("%d requests for down osds, need new map\n", needmap);
2072 ceph_monc_request_next_osdmap(&osdc->client->monc);
2073 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002074}
Sage Weil6f6c7002011-01-17 20:34:08 -08002075
2076
Sage Weilf24e9982009-10-06 11:31:10 -07002077/*
2078 * Process updated osd map.
2079 *
2080 * The message contains any number of incremental and full maps, normally
2081 * indicating some sort of topology change in the cluster. Kick requests
2082 * off to different OSDs as needed.
2083 */
2084void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2085{
2086 void *p, *end, *next;
2087 u32 nr_maps, maplen;
2088 u32 epoch;
2089 struct ceph_osdmap *newmap = NULL, *oldmap;
2090 int err;
2091 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002092 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002093
2094 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2095 p = msg->front.iov_base;
2096 end = p + msg->front.iov_len;
2097
2098 /* verify fsid */
2099 ceph_decode_need(&p, end, sizeof(fsid), bad);
2100 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002101 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2102 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002103
2104 down_write(&osdc->map_sem);
2105
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002106 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2107
Sage Weilf24e9982009-10-06 11:31:10 -07002108 /* incremental maps */
2109 ceph_decode_32_safe(&p, end, nr_maps, bad);
2110 dout(" %d inc maps\n", nr_maps);
2111 while (nr_maps > 0) {
2112 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002113 epoch = ceph_decode_32(&p);
2114 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002115 ceph_decode_need(&p, end, maplen, bad);
2116 next = p + maplen;
2117 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2118 dout("applying incremental map %u len %d\n",
2119 epoch, maplen);
2120 newmap = osdmap_apply_incremental(&p, next,
2121 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002122 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002123 if (IS_ERR(newmap)) {
2124 err = PTR_ERR(newmap);
2125 goto bad;
2126 }
Sage Weil30dc6382009-12-21 14:49:37 -08002127 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002128 if (newmap != osdc->osdmap) {
2129 ceph_osdmap_destroy(osdc->osdmap);
2130 osdc->osdmap = newmap;
2131 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002132 was_full = was_full ||
2133 ceph_osdmap_flag(osdc->osdmap,
2134 CEPH_OSDMAP_FULL);
2135 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002136 } else {
2137 dout("ignoring incremental map %u len %d\n",
2138 epoch, maplen);
2139 }
2140 p = next;
2141 nr_maps--;
2142 }
2143 if (newmap)
2144 goto done;
2145
2146 /* full maps */
2147 ceph_decode_32_safe(&p, end, nr_maps, bad);
2148 dout(" %d full maps\n", nr_maps);
2149 while (nr_maps) {
2150 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002151 epoch = ceph_decode_32(&p);
2152 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002153 ceph_decode_need(&p, end, maplen, bad);
2154 if (nr_maps > 1) {
2155 dout("skipping non-latest full map %u len %d\n",
2156 epoch, maplen);
2157 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2158 dout("skipping full map %u len %d, "
2159 "older than our %u\n", epoch, maplen,
2160 osdc->osdmap->epoch);
2161 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002162 int skipped_map = 0;
2163
Sage Weilf24e9982009-10-06 11:31:10 -07002164 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002165 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002166 if (IS_ERR(newmap)) {
2167 err = PTR_ERR(newmap);
2168 goto bad;
2169 }
Sage Weil30dc6382009-12-21 14:49:37 -08002170 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002171 oldmap = osdc->osdmap;
2172 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002173 if (oldmap) {
2174 if (oldmap->epoch + 1 < newmap->epoch)
2175 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002176 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002177 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002178 was_full = was_full ||
2179 ceph_osdmap_flag(osdc->osdmap,
2180 CEPH_OSDMAP_FULL);
2181 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002182 }
2183 p += maplen;
2184 nr_maps--;
2185 }
2186
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002187 if (!osdc->osdmap)
2188 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002189done:
2190 downgrade_write(&osdc->map_sem);
Ilya Dryomov82dcaba2016-01-19 16:19:06 +01002191 ceph_monc_got_map(&osdc->client->monc, CEPH_SUB_OSDMAP,
2192 osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002193
2194 /*
2195 * subscribe to subsequent osdmap updates if full to ensure
2196 * we find out when we are no longer full and stop returning
2197 * ENOSPC.
2198 */
Josh Durgind29adb32013-12-02 19:11:48 -08002199 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2200 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2201 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002202 ceph_monc_request_next_osdmap(&osdc->client->monc);
2203
Alex Elderf9d25192013-02-15 11:42:29 -06002204 mutex_lock(&osdc->request_mutex);
2205 __send_queued(osdc);
2206 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002207 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002208 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002209 return;
2210
2211bad:
2212 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002213 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002214 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002215}
2216
Sage Weilf24e9982009-10-06 11:31:10 -07002217/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002218 * watch/notify callback event infrastructure
2219 *
2220 * These callbacks are used both for watch and notify operations.
2221 */
2222static void __release_event(struct kref *kref)
2223{
2224 struct ceph_osd_event *event =
2225 container_of(kref, struct ceph_osd_event, kref);
2226
2227 dout("__release_event %p\n", event);
2228 kfree(event);
2229}
2230
2231static void get_event(struct ceph_osd_event *event)
2232{
2233 kref_get(&event->kref);
2234}
2235
2236void ceph_osdc_put_event(struct ceph_osd_event *event)
2237{
2238 kref_put(&event->kref, __release_event);
2239}
2240EXPORT_SYMBOL(ceph_osdc_put_event);
2241
2242static void __insert_event(struct ceph_osd_client *osdc,
2243 struct ceph_osd_event *new)
2244{
2245 struct rb_node **p = &osdc->event_tree.rb_node;
2246 struct rb_node *parent = NULL;
2247 struct ceph_osd_event *event = NULL;
2248
2249 while (*p) {
2250 parent = *p;
2251 event = rb_entry(parent, struct ceph_osd_event, node);
2252 if (new->cookie < event->cookie)
2253 p = &(*p)->rb_left;
2254 else if (new->cookie > event->cookie)
2255 p = &(*p)->rb_right;
2256 else
2257 BUG();
2258 }
2259
2260 rb_link_node(&new->node, parent, p);
2261 rb_insert_color(&new->node, &osdc->event_tree);
2262}
2263
2264static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2265 u64 cookie)
2266{
2267 struct rb_node **p = &osdc->event_tree.rb_node;
2268 struct rb_node *parent = NULL;
2269 struct ceph_osd_event *event = NULL;
2270
2271 while (*p) {
2272 parent = *p;
2273 event = rb_entry(parent, struct ceph_osd_event, node);
2274 if (cookie < event->cookie)
2275 p = &(*p)->rb_left;
2276 else if (cookie > event->cookie)
2277 p = &(*p)->rb_right;
2278 else
2279 return event;
2280 }
2281 return NULL;
2282}
2283
2284static void __remove_event(struct ceph_osd_event *event)
2285{
2286 struct ceph_osd_client *osdc = event->osdc;
2287
2288 if (!RB_EMPTY_NODE(&event->node)) {
2289 dout("__remove_event removed %p\n", event);
2290 rb_erase(&event->node, &osdc->event_tree);
2291 ceph_osdc_put_event(event);
2292 } else {
2293 dout("__remove_event didn't remove %p\n", event);
2294 }
2295}
2296
2297int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2298 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002299 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002300{
2301 struct ceph_osd_event *event;
2302
2303 event = kmalloc(sizeof(*event), GFP_NOIO);
2304 if (!event)
2305 return -ENOMEM;
2306
2307 dout("create_event %p\n", event);
2308 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002309 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002310 event->data = data;
2311 event->osdc = osdc;
2312 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002313 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002314 kref_init(&event->kref); /* one ref for us */
2315 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002316
2317 spin_lock(&osdc->event_lock);
2318 event->cookie = ++osdc->event_count;
2319 __insert_event(osdc, event);
2320 spin_unlock(&osdc->event_lock);
2321
2322 *pevent = event;
2323 return 0;
2324}
2325EXPORT_SYMBOL(ceph_osdc_create_event);
2326
2327void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2328{
2329 struct ceph_osd_client *osdc = event->osdc;
2330
2331 dout("cancel_event %p\n", event);
2332 spin_lock(&osdc->event_lock);
2333 __remove_event(event);
2334 spin_unlock(&osdc->event_lock);
2335 ceph_osdc_put_event(event); /* caller's */
2336}
2337EXPORT_SYMBOL(ceph_osdc_cancel_event);
2338
2339
2340static void do_event_work(struct work_struct *work)
2341{
2342 struct ceph_osd_event_work *event_work =
2343 container_of(work, struct ceph_osd_event_work, work);
2344 struct ceph_osd_event *event = event_work->event;
2345 u64 ver = event_work->ver;
2346 u64 notify_id = event_work->notify_id;
2347 u8 opcode = event_work->opcode;
2348
2349 dout("do_event_work completing %p\n", event);
2350 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002351 dout("do_event_work completed %p\n", event);
2352 ceph_osdc_put_event(event);
2353 kfree(event_work);
2354}
2355
2356
2357/*
2358 * Process osd watch notifications
2359 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002360static void handle_watch_notify(struct ceph_osd_client *osdc,
2361 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002362{
2363 void *p, *end;
2364 u8 proto_ver;
2365 u64 cookie, ver, notify_id;
2366 u8 opcode;
2367 struct ceph_osd_event *event;
2368 struct ceph_osd_event_work *event_work;
2369
2370 p = msg->front.iov_base;
2371 end = p + msg->front.iov_len;
2372
2373 ceph_decode_8_safe(&p, end, proto_ver, bad);
2374 ceph_decode_8_safe(&p, end, opcode, bad);
2375 ceph_decode_64_safe(&p, end, cookie, bad);
2376 ceph_decode_64_safe(&p, end, ver, bad);
2377 ceph_decode_64_safe(&p, end, notify_id, bad);
2378
2379 spin_lock(&osdc->event_lock);
2380 event = __find_event(osdc, cookie);
2381 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002382 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002383 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002384 }
2385 spin_unlock(&osdc->event_lock);
2386 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2387 cookie, ver, event);
2388 if (event) {
2389 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002390 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002391 pr_err("couldn't allocate event_work\n");
2392 ceph_osdc_put_event(event);
2393 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002394 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002395 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002396 event_work->event = event;
2397 event_work->ver = ver;
2398 event_work->notify_id = notify_id;
2399 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002400
2401 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002402 }
2403
2404 return;
2405
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002406bad:
2407 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002408}
2409
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002410/*
Alex Eldere65550f2013-04-05 01:27:12 -05002411 * build new request AND message
2412 *
2413 */
2414void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2415 struct ceph_snap_context *snapc, u64 snap_id,
2416 struct timespec *mtime)
2417{
2418 struct ceph_msg *msg = req->r_request;
2419 void *p;
2420 size_t msg_size;
2421 int flags = req->r_flags;
2422 u64 data_len;
2423 unsigned int i;
2424
2425 req->r_snapid = snap_id;
2426 req->r_snapc = ceph_get_snap_context(snapc);
2427
2428 /* encode request */
2429 msg->hdr.version = cpu_to_le16(4);
2430
2431 p = msg->front.iov_base;
2432 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2433 req->r_request_osdmap_epoch = p;
2434 p += 4;
2435 req->r_request_flags = p;
2436 p += 4;
2437 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2438 ceph_encode_timespec(p, mtime);
2439 p += sizeof(struct ceph_timespec);
2440 req->r_request_reassert_version = p;
2441 p += sizeof(struct ceph_eversion); /* will get filled in */
2442
2443 /* oloc */
2444 ceph_encode_8(&p, 4);
2445 ceph_encode_8(&p, 4);
2446 ceph_encode_32(&p, 8 + 4 + 4);
2447 req->r_request_pool = p;
2448 p += 8;
2449 ceph_encode_32(&p, -1); /* preferred */
2450 ceph_encode_32(&p, 0); /* key len */
2451
2452 ceph_encode_8(&p, 1);
2453 req->r_request_pgid = p;
2454 p += 8 + 4;
2455 ceph_encode_32(&p, -1); /* preferred */
2456
2457 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002458 ceph_encode_32(&p, req->r_base_oid.name_len);
2459 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2460 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2461 req->r_base_oid.name, req->r_base_oid.name_len);
2462 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002463
2464 /* ops--can imply data */
2465 ceph_encode_16(&p, (u16)req->r_num_ops);
2466 data_len = 0;
2467 for (i = 0; i < req->r_num_ops; i++) {
2468 data_len += osd_req_encode_op(req, p, i);
2469 p += sizeof(struct ceph_osd_op);
2470 }
2471
2472 /* snaps */
2473 ceph_encode_64(&p, req->r_snapid);
2474 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2475 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2476 if (req->r_snapc) {
2477 for (i = 0; i < snapc->num_snaps; i++) {
2478 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2479 }
2480 }
2481
2482 req->r_request_attempts = p;
2483 p += 4;
2484
2485 /* data */
2486 if (flags & CEPH_OSD_FLAG_WRITE) {
2487 u16 data_off;
2488
2489 /*
2490 * The header "data_off" is a hint to the receiver
2491 * allowing it to align received data into its
2492 * buffers such that there's no need to re-copy
2493 * it before writing it to disk (direct I/O).
2494 */
2495 data_off = (u16) (off & 0xffff);
2496 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2497 }
2498 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2499
2500 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2501 msg_size = p - msg->front.iov_base;
2502 msg->front.iov_len = msg_size;
2503 msg->hdr.front_len = cpu_to_le32(msg_size);
2504
2505 dout("build_request msg_size was %d\n", (int)msg_size);
2506}
2507EXPORT_SYMBOL(ceph_osdc_build_request);
2508
2509/*
Sage Weilf24e9982009-10-06 11:31:10 -07002510 * Register request, send initial attempt.
2511 */
2512int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2513 struct ceph_osd_request *req,
2514 bool nofail)
2515{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002516 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002517
Sage Weilf24e9982009-10-06 11:31:10 -07002518 down_read(&osdc->map_sem);
2519 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002520
2521 rc = __ceph_osdc_start_request(osdc, req, nofail);
2522
Sage Weilf24e9982009-10-06 11:31:10 -07002523 mutex_unlock(&osdc->request_mutex);
2524 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002525
Sage Weilf24e9982009-10-06 11:31:10 -07002526 return rc;
2527}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002528EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002529
2530/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002531 * Unregister a registered request. The request is not completed (i.e.
2532 * no callbacks or wakeups) - higher layers are supposed to know what
2533 * they are canceling.
2534 */
2535void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2536{
2537 struct ceph_osd_client *osdc = req->r_osdc;
2538
2539 mutex_lock(&osdc->request_mutex);
2540 if (req->r_linger)
2541 __unregister_linger_request(osdc, req);
2542 __unregister_request(osdc, req);
2543 mutex_unlock(&osdc->request_mutex);
2544
2545 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2546}
2547EXPORT_SYMBOL(ceph_osdc_cancel_request);
2548
2549/*
Sage Weilf24e9982009-10-06 11:31:10 -07002550 * wait for a request to complete
2551 */
2552int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2553 struct ceph_osd_request *req)
2554{
2555 int rc;
2556
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002557 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2558
Sage Weilf24e9982009-10-06 11:31:10 -07002559 rc = wait_for_completion_interruptible(&req->r_completion);
2560 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002561 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2562 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002563 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002564 return rc;
2565 }
2566
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002567 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2568 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002569 return req->r_result;
2570}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002571EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002572
2573/*
2574 * sync - wait for all in-flight requests to flush. avoid starvation.
2575 */
2576void ceph_osdc_sync(struct ceph_osd_client *osdc)
2577{
2578 struct ceph_osd_request *req;
2579 u64 last_tid, next_tid = 0;
2580
2581 mutex_lock(&osdc->request_mutex);
2582 last_tid = osdc->last_tid;
2583 while (1) {
2584 req = __lookup_request_ge(osdc, next_tid);
2585 if (!req)
2586 break;
2587 if (req->r_tid > last_tid)
2588 break;
2589
2590 next_tid = req->r_tid + 1;
2591 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2592 continue;
2593
2594 ceph_osdc_get_request(req);
2595 mutex_unlock(&osdc->request_mutex);
2596 dout("sync waiting on tid %llu (last is %llu)\n",
2597 req->r_tid, last_tid);
2598 wait_for_completion(&req->r_safe_completion);
2599 mutex_lock(&osdc->request_mutex);
2600 ceph_osdc_put_request(req);
2601 }
2602 mutex_unlock(&osdc->request_mutex);
2603 dout("sync done (thru tid %llu)\n", last_tid);
2604}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002605EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002606
2607/*
Josh Durgindd935f42013-08-28 21:43:09 -07002608 * Call all pending notify callbacks - for use after a watch is
2609 * unregistered, to make sure no more callbacks for it will be invoked
2610 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002611void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002612{
2613 flush_workqueue(osdc->notify_wq);
2614}
2615EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2616
2617
2618/*
Sage Weilf24e9982009-10-06 11:31:10 -07002619 * init, shutdown
2620 */
2621int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2622{
2623 int err;
2624
2625 dout("init\n");
2626 osdc->client = client;
2627 osdc->osdmap = NULL;
2628 init_rwsem(&osdc->map_sem);
2629 init_completion(&osdc->map_waiters);
2630 osdc->last_requested_map = 0;
2631 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002632 osdc->last_tid = 0;
2633 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002634 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002635 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002636 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002637 INIT_LIST_HEAD(&osdc->req_unsent);
2638 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002639 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002640 osdc->num_requests = 0;
2641 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002642 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002643 spin_lock_init(&osdc->event_lock);
2644 osdc->event_tree = RB_ROOT;
2645 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002646
2647 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002648 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002649
Sage Weil5f44f142009-11-18 14:52:18 -08002650 err = -ENOMEM;
Ilya Dryomov9e767ad2016-02-09 17:25:31 +01002651 osdc->req_mempool = mempool_create_slab_pool(10,
2652 ceph_osd_request_cache);
Sage Weilf24e9982009-10-06 11:31:10 -07002653 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002654 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002655
Sage Weild50b4092012-07-09 14:22:34 -07002656 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2657 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002658 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002659 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002660 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002661 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002662 OSD_OPREPLY_FRONT_LEN, 10, true,
2663 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002664 if (err < 0)
2665 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002666
Dan Carpenterdbcae082013-08-15 08:58:59 +03002667 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002668 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002669 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002670 goto out_msgpool_reply;
2671
Sage Weilf24e9982009-10-06 11:31:10 -07002672 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002673
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002674out_msgpool_reply:
2675 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002676out_msgpool:
2677 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002678out_mempool:
2679 mempool_destroy(osdc->req_mempool);
2680out:
2681 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002682}
2683
2684void ceph_osdc_stop(struct ceph_osd_client *osdc)
2685{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002686 flush_workqueue(osdc->notify_wq);
2687 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002688 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002689 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002690 if (osdc->osdmap) {
2691 ceph_osdmap_destroy(osdc->osdmap);
2692 osdc->osdmap = NULL;
2693 }
Sage Weilaca420b2011-08-31 14:45:53 -07002694 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002695 mempool_destroy(osdc->req_mempool);
2696 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002697 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002698}
2699
2700/*
2701 * Read some contiguous pages. If we cross a stripe boundary, shorten
2702 * *plen. Return number of bytes read, or error.
2703 */
2704int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2705 struct ceph_vino vino, struct ceph_file_layout *layout,
2706 u64 off, u64 *plen,
2707 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002708 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002709{
2710 struct ceph_osd_request *req;
2711 int rc = 0;
2712
2713 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2714 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002715 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002716 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002717 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002718 false);
Sage Weil68162822012-09-24 21:01:02 -07002719 if (IS_ERR(req))
2720 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002721
2722 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002723
Alex Elder406e2c92013-04-15 14:50:36 -05002724 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002725 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002726
Alex Eldere0c59482013-03-07 15:38:25 -06002727 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002728 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002729
Alex Elder79528732013-04-03 21:32:51 -05002730 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002731
Sage Weilf24e9982009-10-06 11:31:10 -07002732 rc = ceph_osdc_start_request(osdc, req, false);
2733 if (!rc)
2734 rc = ceph_osdc_wait_request(osdc, req);
2735
2736 ceph_osdc_put_request(req);
2737 dout("readpages result %d\n", rc);
2738 return rc;
2739}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002740EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002741
2742/*
2743 * do a synchronous write on N pages
2744 */
2745int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2746 struct ceph_file_layout *layout,
2747 struct ceph_snap_context *snapc,
2748 u64 off, u64 len,
2749 u32 truncate_seq, u64 truncate_size,
2750 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002751 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002752{
2753 struct ceph_osd_request *req;
2754 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002755 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002756
Alex Elderacead002013-03-14 14:09:05 -05002757 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002758 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002759 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002760 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002761 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002762 true);
Sage Weil68162822012-09-24 21:01:02 -07002763 if (IS_ERR(req))
2764 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002765
2766 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002767 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002768 false, false);
2769 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002770
Alex Elder79528732013-04-03 21:32:51 -05002771 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002772
Alex Elder87f979d2013-02-15 11:42:29 -06002773 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002774 if (!rc)
2775 rc = ceph_osdc_wait_request(osdc, req);
2776
2777 ceph_osdc_put_request(req);
2778 if (rc == 0)
2779 rc = len;
2780 dout("writepages result %d\n", rc);
2781 return rc;
2782}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002783EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002784
Alex Elder5522ae02013-05-01 12:43:04 -05002785int ceph_osdc_setup(void)
2786{
2787 BUG_ON(ceph_osd_request_cache);
2788 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2789 sizeof (struct ceph_osd_request),
2790 __alignof__(struct ceph_osd_request),
2791 0, NULL);
2792
2793 return ceph_osd_request_cache ? 0 : -ENOMEM;
2794}
2795EXPORT_SYMBOL(ceph_osdc_setup);
2796
2797void ceph_osdc_cleanup(void)
2798{
2799 BUG_ON(!ceph_osd_request_cache);
2800 kmem_cache_destroy(ceph_osd_request_cache);
2801 ceph_osd_request_cache = NULL;
2802}
2803EXPORT_SYMBOL(ceph_osdc_cleanup);
2804
Sage Weilf24e9982009-10-06 11:31:10 -07002805/*
2806 * handle incoming message
2807 */
2808static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2809{
2810 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002811 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002812 int type = le16_to_cpu(msg->hdr.type);
2813
2814 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002815 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002816 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002817
2818 switch (type) {
2819 case CEPH_MSG_OSD_MAP:
2820 ceph_osdc_handle_map(osdc, msg);
2821 break;
2822 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302823 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002824 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002825 case CEPH_MSG_WATCH_NOTIFY:
2826 handle_watch_notify(osdc, msg);
2827 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002828
2829 default:
2830 pr_err("received unknown message type %d %s\n", type,
2831 ceph_msg_type_name(type));
2832 }
Sage Weil4a32f932010-06-13 10:27:53 -07002833out:
Sage Weilf24e9982009-10-06 11:31:10 -07002834 ceph_msg_put(msg);
2835}
2836
Sage Weil5b3a4db2010-02-19 21:43:23 -08002837/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002838 * Lookup and return message for incoming reply. Don't try to do
2839 * anything about a larger than preallocated data portion of the
2840 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002841 */
2842static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002843 struct ceph_msg_header *hdr,
2844 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002845{
2846 struct ceph_osd *osd = con->private;
2847 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002848 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002849 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002850 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002851 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002852 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002853
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002854 tid = le64_to_cpu(hdr->tid);
2855 mutex_lock(&osdc->request_mutex);
2856 req = __lookup_request(osdc, tid);
2857 if (!req) {
Ilya Dryomovcd8140c2016-02-19 11:38:57 +01002858 dout("%s osd%d tid %llu unknown, skipping\n", __func__,
2859 osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002860 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002861 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002862 goto out;
2863 }
Sage Weilc16e7862010-03-01 13:02:00 -08002864
Alex Elderace6d3a2013-04-01 16:12:14 -05002865 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002866
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002867 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002868 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2869 __func__, osd->o_osd, req->r_tid, front_len,
2870 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002871 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2872 false);
Sage Weila79832f2010-04-01 16:06:19 -07002873 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002874 goto out;
2875 ceph_msg_put(req->r_reply);
2876 req->r_reply = m;
2877 }
Sage Weilc16e7862010-03-01 13:02:00 -08002878
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002879 if (data_len > req->r_reply->data_length) {
2880 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2881 __func__, osd->o_osd, req->r_tid, data_len,
2882 req->r_reply->data_length);
2883 m = NULL;
2884 *skip = 1;
2885 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002886 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002887
2888 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002889 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002890
2891out:
2892 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002893 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002894}
2895
2896static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2897 struct ceph_msg_header *hdr,
2898 int *skip)
2899{
2900 struct ceph_osd *osd = con->private;
2901 int type = le16_to_cpu(hdr->type);
2902 int front = le32_to_cpu(hdr->front_len);
2903
Alex Elder1c20f2d2012-06-04 14:43:32 -05002904 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002905 switch (type) {
2906 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002907 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002908 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002909 case CEPH_MSG_OSD_OPREPLY:
2910 return get_reply(con, hdr, skip);
2911 default:
2912 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2913 osd->o_osd);
2914 *skip = 1;
2915 return NULL;
2916 }
Sage Weilf24e9982009-10-06 11:31:10 -07002917}
2918
2919/*
2920 * Wrappers to refcount containing ceph_osd struct
2921 */
2922static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2923{
2924 struct ceph_osd *osd = con->private;
2925 if (get_osd(osd))
2926 return con;
2927 return NULL;
2928}
2929
2930static void put_osd_con(struct ceph_connection *con)
2931{
2932 struct ceph_osd *osd = con->private;
2933 put_osd(osd);
2934}
2935
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002936/*
2937 * authentication
2938 */
Alex Eldera3530df2012-05-16 15:16:39 -05002939/*
2940 * Note: returned pointer is the address of a structure that's
2941 * managed separately. Caller must *not* attempt to free it.
2942 */
2943static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002944 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002945{
2946 struct ceph_osd *o = con->private;
2947 struct ceph_osd_client *osdc = o->o_osdc;
2948 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002949 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002950
Alex Elder74f18692012-05-16 15:16:39 -05002951 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002952 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002953 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002954 }
Sage Weil27859f92013-03-25 10:26:14 -07002955 if (!auth->authorizer) {
2956 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2957 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002958 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002959 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002960 } else {
2961 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002962 auth);
2963 if (ret)
2964 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002965 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002966 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002967
Alex Eldera3530df2012-05-16 15:16:39 -05002968 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002969}
2970
2971
2972static int verify_authorizer_reply(struct ceph_connection *con, int len)
2973{
2974 struct ceph_osd *o = con->private;
2975 struct ceph_osd_client *osdc = o->o_osdc;
2976 struct ceph_auth_client *ac = osdc->client->monc.auth;
2977
Sage Weil27859f92013-03-25 10:26:14 -07002978 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002979}
2980
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002981static int invalidate_authorizer(struct ceph_connection *con)
2982{
2983 struct ceph_osd *o = con->private;
2984 struct ceph_osd_client *osdc = o->o_osdc;
2985 struct ceph_auth_client *ac = osdc->client->monc.auth;
2986
Sage Weil27859f92013-03-25 10:26:14 -07002987 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002988 return ceph_monc_validate_auth(&osdc->client->monc);
2989}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002990
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002991static int osd_sign_message(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08002992{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002993 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08002994 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002995
Yan, Zheng33d07332014-11-04 16:33:37 +08002996 return ceph_auth_sign_message(auth, msg);
2997}
2998
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01002999static int osd_check_message_signature(struct ceph_msg *msg)
Yan, Zheng33d07332014-11-04 16:33:37 +08003000{
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003001 struct ceph_osd *o = msg->con->private;
Yan, Zheng33d07332014-11-04 16:33:37 +08003002 struct ceph_auth_handshake *auth = &o->o_auth;
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003003
Yan, Zheng33d07332014-11-04 16:33:37 +08003004 return ceph_auth_check_message_signature(auth, msg);
3005}
3006
Tobias Klauser9e327892010-05-20 10:40:19 +02003007static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07003008 .get = get_osd_con,
3009 .put = put_osd_con,
3010 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003011 .get_authorizer = get_authorizer,
3012 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003013 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003014 .alloc_msg = alloc_msg,
Ilya Dryomov79dbd1b2015-10-26 22:23:56 +01003015 .sign_message = osd_sign_message,
3016 .check_message_signature = osd_check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003017 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003018};