blob: 191bc21cecea7dfd15090219b458423abfcbd469 [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
Sage Weil1b83bef2013-02-25 16:11:12 -0800375 msg_size = 4 + 4 + 8 + 8 + 4+8;
376 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
377 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200378 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800379 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
380 msg_size += 8; /* snapid */
381 msg_size += 8; /* snap_seq */
382 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
383 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384
385 if (use_mempool) {
386 req = mempool_alloc(osdc->req_mempool, gfp_flags);
387 memset(req, 0, sizeof(*req));
388 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500389 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700390 }
391 if (req == NULL)
392 return NULL;
393
394 req->r_osdc = osdc;
395 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500396 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700397
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700398 kref_init(&req->r_kref);
399 init_completion(&req->r_completion);
400 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600401 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700402 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700403 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400404 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700405 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700406 INIT_LIST_HEAD(&req->r_osd_item);
407
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200408 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200409 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200410
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700411 /* create reply message */
412 if (use_mempool)
413 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
414 else
415 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700416 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700417 if (!msg) {
418 ceph_osdc_put_request(req);
419 return NULL;
420 }
421 req->r_reply = msg;
422
423 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700424 if (use_mempool)
425 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
426 else
Sage Weilb61c2762011-08-09 15:03:46 -0700427 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700428 if (!msg) {
429 ceph_osdc_put_request(req);
430 return NULL;
431 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700432
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700433 memset(msg->front.iov_base, 0, msg->front.iov_len);
434
435 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436
437 return req;
438}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700439EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700440
Alex Eldera8dd0a32013-03-13 20:50:00 -0500441static bool osd_req_opcode_valid(u16 opcode)
442{
443 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400444#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
445__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
446#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500447 default:
448 return false;
449 }
450}
451
Alex Elder33803f32013-03-13 20:50:00 -0500452/*
453 * This is an osd op init function for opcodes that have no data or
454 * other information associated with them. It also serves as a
455 * common init routine for all the other init functions, below.
456 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500457static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600458_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800459 u16 opcode, u32 flags)
Alex Elder33803f32013-03-13 20:50:00 -0500460{
Alex Elderc99d2d42013-04-05 01:27:11 -0500461 struct ceph_osd_req_op *op;
462
463 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500464 BUG_ON(!osd_req_opcode_valid(opcode));
465
Alex Elderc99d2d42013-04-05 01:27:11 -0500466 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500467 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500468 op->op = opcode;
Yan, Zheng144cba12015-04-27 11:09:54 +0800469 op->flags = flags;
Alex Elderc99d2d42013-04-05 01:27:11 -0500470
471 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500472}
473
Alex Elder49719772013-02-11 12:33:24 -0600474void osd_req_op_init(struct ceph_osd_request *osd_req,
Yan, Zheng144cba12015-04-27 11:09:54 +0800475 unsigned int which, u16 opcode, u32 flags)
Alex Elder49719772013-02-11 12:33:24 -0600476{
Yan, Zheng144cba12015-04-27 11:09:54 +0800477 (void)_osd_req_op_init(osd_req, which, opcode, flags);
Alex Elder49719772013-02-11 12:33:24 -0600478}
479EXPORT_SYMBOL(osd_req_op_init);
480
Alex Elderc99d2d42013-04-05 01:27:11 -0500481void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
482 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500483 u64 offset, u64 length,
484 u64 truncate_size, u32 truncate_seq)
485{
Yan, Zheng144cba12015-04-27 11:09:54 +0800486 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
487 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500488 size_t payload_len = 0;
489
Li Wangad7a60d2013-08-15 11:51:44 +0800490 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Ilya Dryomove30b7572015-10-07 17:27:17 +0200491 opcode != CEPH_OSD_OP_WRITEFULL && opcode != CEPH_OSD_OP_ZERO &&
492 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500493
Alex Elder33803f32013-03-13 20:50:00 -0500494 op->extent.offset = offset;
495 op->extent.length = length;
496 op->extent.truncate_size = truncate_size;
497 op->extent.truncate_seq = truncate_seq;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200498 if (opcode == CEPH_OSD_OP_WRITE || opcode == CEPH_OSD_OP_WRITEFULL)
Alex Elder33803f32013-03-13 20:50:00 -0500499 payload_len += length;
500
501 op->payload_len = payload_len;
502}
503EXPORT_SYMBOL(osd_req_op_extent_init);
504
Alex Elderc99d2d42013-04-05 01:27:11 -0500505void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
506 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500507{
Alex Elderc99d2d42013-04-05 01:27:11 -0500508 struct ceph_osd_req_op *op;
509 u64 previous;
510
511 BUG_ON(which >= osd_req->r_num_ops);
512 op = &osd_req->r_ops[which];
513 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500514
515 if (length == previous)
516 return; /* Nothing to do */
517 BUG_ON(length > previous);
518
519 op->extent.length = length;
520 op->payload_len -= previous - length;
521}
522EXPORT_SYMBOL(osd_req_op_extent_update);
523
Alex Elderc99d2d42013-04-05 01:27:11 -0500524void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500525 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500526{
Yan, Zheng144cba12015-04-27 11:09:54 +0800527 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
528 opcode, 0);
Alex Elder5f562df2013-04-05 01:27:12 -0500529 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500530 size_t payload_len = 0;
531 size_t size;
532
533 BUG_ON(opcode != CEPH_OSD_OP_CALL);
534
Alex Elder5f562df2013-04-05 01:27:12 -0500535 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
536 BUG_ON(!pagelist);
537 ceph_pagelist_init(pagelist);
538
Alex Elder33803f32013-03-13 20:50:00 -0500539 op->cls.class_name = class;
540 size = strlen(class);
541 BUG_ON(size > (size_t) U8_MAX);
542 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500543 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500544 payload_len += size;
545
546 op->cls.method_name = method;
547 size = strlen(method);
548 BUG_ON(size > (size_t) U8_MAX);
549 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500550 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500551 payload_len += size;
552
Alex Eldera4ce40a2013-04-05 01:27:12 -0500553 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500554
Alex Elder33803f32013-03-13 20:50:00 -0500555 op->cls.argc = 0; /* currently unused */
556
557 op->payload_len = payload_len;
558}
559EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500560
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800561int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
562 u16 opcode, const char *name, const void *value,
563 size_t size, u8 cmp_op, u8 cmp_mode)
564{
Yan, Zheng144cba12015-04-27 11:09:54 +0800565 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
566 opcode, 0);
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800567 struct ceph_pagelist *pagelist;
568 size_t payload_len;
569
570 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
571
572 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
573 if (!pagelist)
574 return -ENOMEM;
575
576 ceph_pagelist_init(pagelist);
577
578 payload_len = strlen(name);
579 op->xattr.name_len = payload_len;
580 ceph_pagelist_append(pagelist, name, payload_len);
581
582 op->xattr.value_len = size;
583 ceph_pagelist_append(pagelist, value, size);
584 payload_len += size;
585
586 op->xattr.cmp_op = cmp_op;
587 op->xattr.cmp_mode = cmp_mode;
588
589 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
590 op->payload_len = payload_len;
591 return 0;
592}
593EXPORT_SYMBOL(osd_req_op_xattr_init);
594
Alex Elderc99d2d42013-04-05 01:27:11 -0500595void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
596 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500597 u64 cookie, u64 version, int flag)
598{
Yan, Zheng144cba12015-04-27 11:09:54 +0800599 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
600 opcode, 0);
Alex Elder33803f32013-03-13 20:50:00 -0500601
Alex Elderc99d2d42013-04-05 01:27:11 -0500602 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500603
604 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500605 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500606 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500607 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500608}
609EXPORT_SYMBOL(osd_req_op_watch_init);
610
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200611void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
612 unsigned int which,
613 u64 expected_object_size,
614 u64 expected_write_size)
615{
616 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
Yan, Zheng144cba12015-04-27 11:09:54 +0800617 CEPH_OSD_OP_SETALLOCHINT,
618 0);
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200619
620 op->alloc_hint.expected_object_size = expected_object_size;
621 op->alloc_hint.expected_write_size = expected_write_size;
622
623 /*
624 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
625 * not worth a feature bit. Set FAILOK per-op flag to make
626 * sure older osds don't trip over an unsupported opcode.
627 */
628 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
629}
630EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
631
Alex Elder90af3602013-04-05 14:46:01 -0500632static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500633 struct ceph_osd_data *osd_data)
634{
635 u64 length = ceph_osd_data_length(osd_data);
636
637 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
638 BUG_ON(length > (u64) SIZE_MAX);
639 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500640 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500641 length, osd_data->alignment);
642 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
643 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500644 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500645#ifdef CONFIG_BLOCK
646 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500647 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500648#endif
649 } else {
650 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
651 }
652}
653
Alex Elder175face2013-03-08 13:35:36 -0600654static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500655 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700656{
Alex Elder79528732013-04-03 21:32:51 -0500657 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500658 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500659 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500660 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600661
Alex Elder79528732013-04-03 21:32:51 -0500662 BUG_ON(which >= req->r_num_ops);
663 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500664 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
665 pr_err("unrecognized osd opcode %d\n", src->op);
666
667 return 0;
668 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700669
Alex Elder065a68f2012-04-20 15:49:43 -0500670 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600671 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600672 osd_data = &src->raw_data_in;
673 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600674 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700675 case CEPH_OSD_OP_READ:
676 case CEPH_OSD_OP_WRITE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200677 case CEPH_OSD_OP_WRITEFULL:
Li Wangad7a60d2013-08-15 11:51:44 +0800678 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800679 case CEPH_OSD_OP_TRUNCATE:
Ilya Dryomove30b7572015-10-07 17:27:17 +0200680 if (src->op == CEPH_OSD_OP_WRITE ||
681 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder54d50642013-04-03 01:28:58 -0500682 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600683 dst->extent.offset = cpu_to_le64(src->extent.offset);
684 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700685 dst->extent.truncate_size =
686 cpu_to_le64(src->extent.truncate_size);
687 dst->extent.truncate_seq =
688 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500689 osd_data = &src->extent.osd_data;
Ilya Dryomove30b7572015-10-07 17:27:17 +0200690 if (src->op == CEPH_OSD_OP_WRITE ||
691 src->op == CEPH_OSD_OP_WRITEFULL)
Alex Elder04017e22013-04-05 14:46:02 -0500692 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500693 else
Alex Elder04017e22013-04-05 14:46:02 -0500694 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700695 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700696 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700697 dst->cls.class_len = src->cls.class_len;
698 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500699 osd_data = &src->cls.request_info;
700 ceph_osdc_msg_data_add(req->r_request, osd_data);
701 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
702 request_data_len = osd_data->pagelist->length;
703
704 osd_data = &src->cls.request_data;
705 data_length = ceph_osd_data_length(osd_data);
706 if (data_length) {
707 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
708 dst->cls.indata_len = cpu_to_le32(data_length);
709 ceph_osdc_msg_data_add(req->r_request, osd_data);
710 src->payload_len += data_length;
711 request_data_len += data_length;
712 }
713 osd_data = &src->cls.response_data;
714 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700715 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700716 case CEPH_OSD_OP_STARTSYNC:
717 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700718 case CEPH_OSD_OP_NOTIFY_ACK:
719 case CEPH_OSD_OP_WATCH:
720 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
721 dst->watch.ver = cpu_to_le64(src->watch.ver);
722 dst->watch.flag = src->watch.flag;
723 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200724 case CEPH_OSD_OP_SETALLOCHINT:
725 dst->alloc_hint.expected_object_size =
726 cpu_to_le64(src->alloc_hint.expected_object_size);
727 dst->alloc_hint.expected_write_size =
728 cpu_to_le64(src->alloc_hint.expected_write_size);
729 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800730 case CEPH_OSD_OP_SETXATTR:
731 case CEPH_OSD_OP_CMPXATTR:
732 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
733 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
734 dst->xattr.cmp_op = src->xattr.cmp_op;
735 dst->xattr.cmp_mode = src->xattr.cmp_mode;
736 osd_data = &src->xattr.osd_data;
737 ceph_osdc_msg_data_add(req->r_request, osd_data);
738 request_data_len = osd_data->pagelist->length;
739 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800740 case CEPH_OSD_OP_CREATE:
741 case CEPH_OSD_OP_DELETE:
742 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700743 default:
Alex Elder4c464592013-02-15 11:42:30 -0600744 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600745 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600746 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500747
748 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700749 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200750
Alex Eldera8dd0a32013-03-13 20:50:00 -0500751 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200752 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700753 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600754
Alex Elder54d50642013-04-03 01:28:58 -0500755 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700756}
757
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700758/*
Sage Weilf24e9982009-10-06 11:31:10 -0700759 * build new request AND message, calculate layout, and adjust file
760 * extent as needed.
761 *
762 * if the file was recently truncated, we include information about its
763 * old and new size so that the object can be updated appropriately. (we
764 * avoid synchronously deleting truncated objects because it's slow.)
765 *
766 * if @do_sync, include a 'startsync' command so that the osd will flush
767 * data quickly.
768 */
769struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
770 struct ceph_file_layout *layout,
771 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800772 u64 off, u64 *plen,
773 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700774 int opcode, int flags,
775 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700776 u32 truncate_seq,
777 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600778 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700779{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700780 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500781 u64 objnum = 0;
782 u64 objoff = 0;
783 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700784 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700785
Li Wangad7a60d2013-08-15 11:51:44 +0800786 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800787 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
788 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700789
Alex Elderacead002013-03-14 14:09:05 -0500790 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600791 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700792 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700793 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500794
Alex Elderd178a9e2012-11-13 21:11:15 -0600795 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700796
797 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500798 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600799 if (r < 0) {
800 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700801 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600802 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500803
Yan, Zheng864e9192014-11-13 10:47:25 +0800804 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng144cba12015-04-27 11:09:54 +0800805 osd_req_op_init(req, which, opcode, 0);
Yan, Zheng864e9192014-11-13 10:47:25 +0800806 } else {
807 u32 object_size = le32_to_cpu(layout->fl_object_size);
808 u32 object_base = off - objoff;
809 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
810 if (truncate_size <= object_base) {
811 truncate_size = 0;
812 } else {
813 truncate_size -= object_base;
814 if (truncate_size > object_size)
815 truncate_size = object_size;
816 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800817 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800818 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800819 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500820 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500821
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200822 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700823
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200824 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200825 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200826 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600827
Sage Weilf24e9982009-10-06 11:31:10 -0700828 return req;
829}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700830EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700831
832/*
833 * We keep osd requests in an rbtree, sorted by ->r_tid.
834 */
835static void __insert_request(struct ceph_osd_client *osdc,
836 struct ceph_osd_request *new)
837{
838 struct rb_node **p = &osdc->requests.rb_node;
839 struct rb_node *parent = NULL;
840 struct ceph_osd_request *req = NULL;
841
842 while (*p) {
843 parent = *p;
844 req = rb_entry(parent, struct ceph_osd_request, r_node);
845 if (new->r_tid < req->r_tid)
846 p = &(*p)->rb_left;
847 else if (new->r_tid > req->r_tid)
848 p = &(*p)->rb_right;
849 else
850 BUG();
851 }
852
853 rb_link_node(&new->r_node, parent, p);
854 rb_insert_color(&new->r_node, &osdc->requests);
855}
856
857static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
858 u64 tid)
859{
860 struct ceph_osd_request *req;
861 struct rb_node *n = osdc->requests.rb_node;
862
863 while (n) {
864 req = rb_entry(n, struct ceph_osd_request, r_node);
865 if (tid < req->r_tid)
866 n = n->rb_left;
867 else if (tid > req->r_tid)
868 n = n->rb_right;
869 else
870 return req;
871 }
872 return NULL;
873}
874
875static struct ceph_osd_request *
876__lookup_request_ge(struct ceph_osd_client *osdc,
877 u64 tid)
878{
879 struct ceph_osd_request *req;
880 struct rb_node *n = osdc->requests.rb_node;
881
882 while (n) {
883 req = rb_entry(n, struct ceph_osd_request, r_node);
884 if (tid < req->r_tid) {
885 if (!n->rb_left)
886 return req;
887 n = n->rb_left;
888 } else if (tid > req->r_tid) {
889 n = n->rb_right;
890 } else {
891 return req;
892 }
893 }
894 return NULL;
895}
896
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400897static void __kick_linger_request(struct ceph_osd_request *req)
898{
899 struct ceph_osd_client *osdc = req->r_osdc;
900 struct ceph_osd *osd = req->r_osd;
901
902 /*
903 * Linger requests need to be resent with a new tid to avoid
904 * the dup op detection logic on the OSDs. Achieve this with
905 * a re-register dance instead of open-coding.
906 */
907 ceph_osdc_get_request(req);
908 if (!list_empty(&req->r_linger_item))
909 __unregister_linger_request(osdc, req);
910 else
911 __unregister_request(osdc, req);
912 __register_request(osdc, req);
913 ceph_osdc_put_request(req);
914
915 /*
916 * Unless request has been registered as both normal and
917 * lingering, __unregister{,_linger}_request clears r_osd.
918 * However, here we need to preserve r_osd to make sure we
919 * requeue on the same OSD.
920 */
921 WARN_ON(req->r_osd || !osd);
922 req->r_osd = osd;
923
924 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
925 __enqueue_request(req);
926}
927
Sage Weil6f6c7002011-01-17 20:34:08 -0800928/*
929 * Resubmit requests pending on the given osd.
930 */
931static void __kick_osd_requests(struct ceph_osd_client *osdc,
932 struct ceph_osd *osd)
933{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700934 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500935 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400936 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800937 int err;
938
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400939 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800940 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600941 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800942 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400943
Alex Eldere02493c2013-03-25 18:16:11 -0500944 /*
945 * Build up a list of requests to resend by traversing the
946 * osd's list of requests. Requests for a given object are
947 * sent in tid order, and that is also the order they're
948 * kept on this list. Therefore all requests that are in
949 * flight will be found first, followed by all requests that
950 * have not yet been sent. And to resend requests while
951 * preserving this order we will want to put any sent
952 * requests back on the front of the osd client's unsent
953 * list.
954 *
955 * So we build a separate ordered list of already-sent
956 * requests for the affected osd and splice it onto the
957 * front of the osd client's unsent list. Once we've seen a
958 * request that has not yet been sent we're done. Those
959 * requests are already sitting right where they belong.
960 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800961 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500962 if (!req->r_sent)
963 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400964
965 if (!req->r_linger) {
966 dout("%s requeueing %p tid %llu\n", __func__, req,
967 req->r_tid);
968 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700969 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400970 } else {
971 list_move_tail(&req->r_req_lru_item, &resend_linger);
972 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700973 }
Alex Eldere02493c2013-03-25 18:16:11 -0500974 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700975
Alex Eldere02493c2013-03-25 18:16:11 -0500976 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400977 * Both registered and not yet registered linger requests are
978 * enqueued with a new tid on the same OSD. We add/move them
979 * to req_unsent/o_requests at the end to keep things in tid
980 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500981 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700982 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400983 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400984 WARN_ON(!list_empty(&req->r_req_lru_item));
985 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800986 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400987
988 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
989 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800990}
991
Sage Weilf24e9982009-10-06 11:31:10 -0700992/*
Sage Weil81b024e2009-10-09 10:29:18 -0700993 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700994 */
995static void osd_reset(struct ceph_connection *con)
996{
997 struct ceph_osd *osd = con->private;
998 struct ceph_osd_client *osdc;
999
1000 if (!osd)
1001 return;
1002 dout("osd_reset osd%d\n", osd->o_osd);
1003 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001004 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -08001005 mutex_lock(&osdc->request_mutex);
1006 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -06001007 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -08001008 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001009 up_read(&osdc->map_sem);
1010}
1011
1012/*
1013 * Track open sessions with osds.
1014 */
Alex Eldere10006f2012-05-26 23:26:43 -05001015static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001016{
1017 struct ceph_osd *osd;
1018
1019 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1020 if (!osd)
1021 return NULL;
1022
1023 atomic_set(&osd->o_ref, 1);
1024 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001025 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001026 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001027 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001028 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001029 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001030 osd->o_incarnation = 1;
1031
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001032 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001033
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001034 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001035 return osd;
1036}
1037
1038static struct ceph_osd *get_osd(struct ceph_osd *osd)
1039{
1040 if (atomic_inc_not_zero(&osd->o_ref)) {
1041 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1042 atomic_read(&osd->o_ref));
1043 return osd;
1044 } else {
1045 dout("get_osd %p FAIL\n", osd);
1046 return NULL;
1047 }
1048}
1049
1050static void put_osd(struct ceph_osd *osd)
1051{
1052 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1053 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001054 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001055 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1056
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001057 if (osd->o_auth.authorizer)
1058 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001059 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001060 }
Sage Weilf24e9982009-10-06 11:31:10 -07001061}
1062
1063/*
1064 * remove an osd from our map
1065 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001066static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001067{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001068 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001069 WARN_ON(!list_empty(&osd->o_requests));
1070 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001071
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001072 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001073 rb_erase(&osd->o_node, &osdc->osds);
1074 RB_CLEAR_NODE(&osd->o_node);
1075}
1076
1077static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1078{
1079 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1080
1081 if (!RB_EMPTY_NODE(&osd->o_node)) {
1082 ceph_con_close(&osd->o_con);
1083 __remove_osd(osdc, osd);
1084 put_osd(osd);
1085 }
Sage Weilf24e9982009-10-06 11:31:10 -07001086}
1087
Sage Weilaca420b2011-08-31 14:45:53 -07001088static void remove_all_osds(struct ceph_osd_client *osdc)
1089{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001090 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001091 mutex_lock(&osdc->request_mutex);
1092 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1093 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1094 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001095 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001096 }
1097 mutex_unlock(&osdc->request_mutex);
1098}
1099
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001100static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1101 struct ceph_osd *osd)
1102{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001103 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001105
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001106 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001107 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001108}
1109
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001110static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1111 struct ceph_osd *osd)
1112{
1113 dout("%s %p\n", __func__, osd);
1114
1115 if (list_empty(&osd->o_requests) &&
1116 list_empty(&osd->o_linger_requests))
1117 __move_osd_to_lru(osdc, osd);
1118}
1119
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001120static void __remove_osd_from_lru(struct ceph_osd *osd)
1121{
1122 dout("__remove_osd_from_lru %p\n", osd);
1123 if (!list_empty(&osd->o_osd_lru))
1124 list_del_init(&osd->o_osd_lru);
1125}
1126
Sage Weilaca420b2011-08-31 14:45:53 -07001127static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001128{
1129 struct ceph_osd *osd, *nosd;
1130
1131 dout("__remove_old_osds %p\n", osdc);
1132 mutex_lock(&osdc->request_mutex);
1133 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001134 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001135 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001136 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001137 }
1138 mutex_unlock(&osdc->request_mutex);
1139}
1140
Sage Weilf24e9982009-10-06 11:31:10 -07001141/*
1142 * reset osd connect
1143 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001144static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001145{
Alex Elderc3acb182012-12-07 09:57:58 -06001146 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001147
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001148 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001149 if (list_empty(&osd->o_requests) &&
1150 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001151 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001152 return -ENODEV;
1153 }
1154
1155 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1156 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1157 !ceph_con_opened(&osd->o_con)) {
1158 struct ceph_osd_request *req;
1159
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001160 dout("osd addr hasn't changed and connection never opened, "
1161 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001162 /* touch each r_stamp for handle_timeout()'s benfit */
1163 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1164 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001165
1166 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001167 }
Alex Elderc3acb182012-12-07 09:57:58 -06001168
1169 ceph_con_close(&osd->o_con);
1170 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1171 osd->o_incarnation++;
1172
1173 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001174}
1175
1176static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1177{
1178 struct rb_node **p = &osdc->osds.rb_node;
1179 struct rb_node *parent = NULL;
1180 struct ceph_osd *osd = NULL;
1181
Sage Weilaca420b2011-08-31 14:45:53 -07001182 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001183 while (*p) {
1184 parent = *p;
1185 osd = rb_entry(parent, struct ceph_osd, o_node);
1186 if (new->o_osd < osd->o_osd)
1187 p = &(*p)->rb_left;
1188 else if (new->o_osd > osd->o_osd)
1189 p = &(*p)->rb_right;
1190 else
1191 BUG();
1192 }
1193
1194 rb_link_node(&new->o_node, parent, p);
1195 rb_insert_color(&new->o_node, &osdc->osds);
1196}
1197
1198static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1199{
1200 struct ceph_osd *osd;
1201 struct rb_node *n = osdc->osds.rb_node;
1202
1203 while (n) {
1204 osd = rb_entry(n, struct ceph_osd, o_node);
1205 if (o < osd->o_osd)
1206 n = n->rb_left;
1207 else if (o > osd->o_osd)
1208 n = n->rb_right;
1209 else
1210 return osd;
1211 }
1212 return NULL;
1213}
1214
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001215static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1216{
1217 schedule_delayed_work(&osdc->timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03001218 osdc->client->options->osd_keepalive_timeout);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001219}
1220
1221static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1222{
1223 cancel_delayed_work(&osdc->timeout_work);
1224}
Sage Weilf24e9982009-10-06 11:31:10 -07001225
1226/*
1227 * Register request, assign tid. If this is the first request, set up
1228 * the timeout event.
1229 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001230static void __register_request(struct ceph_osd_client *osdc,
1231 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001232{
Sage Weilf24e9982009-10-06 11:31:10 -07001233 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001234 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001235 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001236 __insert_request(osdc, req);
1237 ceph_osdc_get_request(req);
1238 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001239 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001240 dout(" first request, scheduling timeout\n");
1241 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001242 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001243}
1244
Sage Weilf24e9982009-10-06 11:31:10 -07001245/*
1246 * called under osdc->request_mutex
1247 */
1248static void __unregister_request(struct ceph_osd_client *osdc,
1249 struct ceph_osd_request *req)
1250{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001251 if (RB_EMPTY_NODE(&req->r_node)) {
1252 dout("__unregister_request %p tid %lld not registered\n",
1253 req, req->r_tid);
1254 return;
1255 }
1256
Sage Weilf24e9982009-10-06 11:31:10 -07001257 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1258 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001259 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001260 osdc->num_requests--;
1261
Sage Weil0ba64782009-10-08 16:57:16 -07001262 if (req->r_osd) {
1263 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001264 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001265
1266 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001267 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001268 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001269 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001270 }
Sage Weilf24e9982009-10-06 11:31:10 -07001271
Alex Elder7d5f2482012-11-29 08:37:03 -06001272 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001273 ceph_osdc_put_request(req);
1274
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001275 if (osdc->num_requests == 0) {
1276 dout(" no requests, canceling timeout\n");
1277 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001278 }
1279}
1280
1281/*
1282 * Cancel a previously queued request message
1283 */
1284static void __cancel_request(struct ceph_osd_request *req)
1285{
Sage Weil6bc18872010-09-27 10:18:52 -07001286 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001287 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001288 req->r_sent = 0;
1289 }
1290}
1291
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001292static void __register_linger_request(struct ceph_osd_client *osdc,
1293 struct ceph_osd_request *req)
1294{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001295 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1296 WARN_ON(!req->r_linger);
1297
Alex Elder96e4dac2013-05-22 20:54:25 -05001298 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001299 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001300 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001301 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001302 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001303}
1304
1305static void __unregister_linger_request(struct ceph_osd_client *osdc,
1306 struct ceph_osd_request *req)
1307{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001308 WARN_ON(!req->r_linger);
1309
1310 if (list_empty(&req->r_linger_item)) {
1311 dout("%s %p tid %llu not registered\n", __func__, req,
1312 req->r_tid);
1313 return;
1314 }
1315
1316 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001317 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001318
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001319 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001320 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001321 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001322 if (list_empty(&req->r_osd_item))
1323 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001324 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001325 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001326}
1327
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001328void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1329 struct ceph_osd_request *req)
1330{
1331 if (!req->r_linger) {
1332 dout("set_request_linger %p\n", req);
1333 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001334 }
1335}
1336EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1337
Sage Weilf24e9982009-10-06 11:31:10 -07001338/*
Josh Durgind29adb32013-12-02 19:11:48 -08001339 * Returns whether a request should be blocked from being sent
1340 * based on the current osdmap and osd_client settings.
1341 *
1342 * Caller should hold map_sem for read.
1343 */
1344static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1345 struct ceph_osd_request *req)
1346{
1347 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1348 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1349 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1350 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1351 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1352}
1353
1354/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001355 * Calculate mapping of a request to a PG. Takes tiering into account.
1356 */
1357static int __calc_request_pg(struct ceph_osdmap *osdmap,
1358 struct ceph_osd_request *req,
1359 struct ceph_pg *pg_out)
1360{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001361 bool need_check_tiering;
1362
1363 need_check_tiering = false;
1364 if (req->r_target_oloc.pool == -1) {
1365 req->r_target_oloc = req->r_base_oloc; /* struct */
1366 need_check_tiering = true;
1367 }
1368 if (req->r_target_oid.name_len == 0) {
1369 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1370 need_check_tiering = true;
1371 }
1372
1373 if (need_check_tiering &&
1374 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001375 struct ceph_pg_pool_info *pi;
1376
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001377 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001378 if (pi) {
1379 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1380 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001381 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001382 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1383 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001384 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001385 }
1386 /* !pi is caught in ceph_oloc_oid_to_pg() */
1387 }
1388
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001389 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1390 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001391}
1392
Ilya Dryomovf671b582014-09-02 13:40:33 +04001393static void __enqueue_request(struct ceph_osd_request *req)
1394{
1395 struct ceph_osd_client *osdc = req->r_osdc;
1396
1397 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1398 req->r_osd ? req->r_osd->o_osd : -1);
1399
1400 if (req->r_osd) {
1401 __remove_osd_from_lru(req->r_osd);
1402 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1403 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1404 } else {
1405 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1406 }
1407}
1408
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001409/*
Sage Weilf24e9982009-10-06 11:31:10 -07001410 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1411 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001412 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001413 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001414 *
1415 * Return 0 if unchanged, 1 if changed, or negative on error.
1416 *
1417 * Caller should hold map_sem for read and request_mutex.
1418 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001419static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001420 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001421{
Sage Weil5b191d92013-02-23 10:38:16 -08001422 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001423 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001424 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001425 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001426 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001427
Sage Weil6f6c7002011-01-17 20:34:08 -08001428 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001429
1430 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001431 if (err) {
1432 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001433 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001434 }
Sage Weil7740a422010-01-08 15:58:25 -08001435 req->r_pgid = pgid;
1436
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001437 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1438 if (num < 0)
1439 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001440
Josh Durgind29adb32013-12-02 19:11:48 -08001441 was_paused = req->r_paused;
1442 req->r_paused = __req_should_be_paused(osdc, req);
1443 if (was_paused && !req->r_paused)
1444 force_resend = 1;
1445
Sage Weil38d64532011-10-14 13:33:55 -07001446 if ((!force_resend &&
1447 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001448 req->r_sent >= req->r_osd->o_incarnation &&
1449 req->r_num_pg_osds == num &&
1450 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001451 (req->r_osd == NULL && o == -1) ||
1452 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001453 return 0; /* no change */
1454
Sage Weil5b191d92013-02-23 10:38:16 -08001455 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1456 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001457 req->r_osd ? req->r_osd->o_osd : -1);
1458
Sage Weild85b7052010-05-10 10:24:48 -07001459 /* record full pg acting set */
1460 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1461 req->r_num_pg_osds = num;
1462
Sage Weilf24e9982009-10-06 11:31:10 -07001463 if (req->r_osd) {
1464 __cancel_request(req);
1465 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001466 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001467 req->r_osd = NULL;
1468 }
1469
1470 req->r_osd = __lookup_osd(osdc, o);
1471 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001472 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001473 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001474 if (!req->r_osd) {
1475 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001476 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001477 }
Sage Weilf24e9982009-10-06 11:31:10 -07001478
Sage Weil6f6c7002011-01-17 20:34:08 -08001479 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001480 __insert_osd(osdc, req->r_osd);
1481
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001482 ceph_con_open(&req->r_osd->o_con,
1483 CEPH_ENTITY_TYPE_OSD, o,
1484 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001485 }
1486
Ilya Dryomovf671b582014-09-02 13:40:33 +04001487 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001488 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001489
1490out:
Sage Weilf24e9982009-10-06 11:31:10 -07001491 return err;
1492}
1493
1494/*
1495 * caller should hold map_sem (for read) and request_mutex
1496 */
Sage Weil56e925b2012-01-03 12:34:34 -08001497static void __send_request(struct ceph_osd_client *osdc,
1498 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001499{
Sage Weil1b83bef2013-02-25 16:11:12 -08001500 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001501
Sage Weil1b83bef2013-02-25 16:11:12 -08001502 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1503 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1504 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001505
Sage Weil1b83bef2013-02-25 16:11:12 -08001506 /* fill in message content that changes each time we send it */
1507 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1508 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001509 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001510 p = req->r_request_pgid;
1511 ceph_encode_64(&p, req->r_pgid.pool);
1512 ceph_encode_32(&p, req->r_pgid.seed);
1513 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1514 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1515 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001516
Sage Weil3dd72fc2010-03-22 14:42:30 -07001517 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001518 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001519
1520 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001521
Sage Weilf24e9982009-10-06 11:31:10 -07001522 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001523
1524 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001525}
1526
1527/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001528 * Send any requests in the queue (req_unsent).
1529 */
Alex Elderf9d25192013-02-15 11:42:29 -06001530static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001531{
1532 struct ceph_osd_request *req, *tmp;
1533
Alex Elderf9d25192013-02-15 11:42:29 -06001534 dout("__send_queued\n");
1535 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001536 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001537}
1538
1539/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001540 * Caller should hold map_sem for read and request_mutex.
1541 */
1542static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1543 struct ceph_osd_request *req,
1544 bool nofail)
1545{
1546 int rc;
1547
1548 __register_request(osdc, req);
1549 req->r_sent = 0;
1550 req->r_got_reply = 0;
1551 rc = __map_request(osdc, req, 0);
1552 if (rc < 0) {
1553 if (nofail) {
1554 dout("osdc_start_request failed map, "
1555 " will retry %lld\n", req->r_tid);
1556 rc = 0;
1557 } else {
1558 __unregister_request(osdc, req);
1559 }
1560 return rc;
1561 }
1562
1563 if (req->r_osd == NULL) {
1564 dout("send_request %p no up osds in pg\n", req);
1565 ceph_monc_request_next_osdmap(&osdc->client->monc);
1566 } else {
1567 __send_queued(osdc);
1568 }
1569
1570 return 0;
1571}
1572
1573/*
Sage Weilf24e9982009-10-06 11:31:10 -07001574 * Timeout callback, called every N seconds when 1 or more osd
1575 * requests has been active for more than N seconds. When this
1576 * happens, we ping all OSDs with requests who have timed out to
1577 * ensure any communications channel reset is detected. Reset the
1578 * request timeouts another N seconds in the future as we go.
1579 * Reschedule the timeout event another N seconds in future (unless
1580 * there are no open requests).
1581 */
1582static void handle_timeout(struct work_struct *work)
1583{
1584 struct ceph_osd_client *osdc =
1585 container_of(work, struct ceph_osd_client, timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001586 struct ceph_options *opts = osdc->client->options;
Sage Weil83aff952012-11-28 12:28:24 -08001587 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001588 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001589 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001590 dout("timeout\n");
1591 down_read(&osdc->map_sem);
1592
1593 ceph_monc_request_next_osdmap(&osdc->client->monc);
1594
1595 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001596
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001597 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001598 * ping osds that are a bit slow. this ensures that if there
1599 * is a break in the TCP connection we will notice, and reopen
1600 * a connection with that osd (from the fault callback).
1601 */
1602 INIT_LIST_HEAD(&slow_osds);
1603 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Ilya Dryomova319bf52015-05-15 12:02:17 +03001604 if (time_before(jiffies,
1605 req->r_stamp + opts->osd_keepalive_timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001606 break;
1607
1608 osd = req->r_osd;
1609 BUG_ON(!osd);
1610 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001611 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001612 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1613 }
1614 while (!list_empty(&slow_osds)) {
1615 osd = list_entry(slow_osds.next, struct ceph_osd,
1616 o_keepalive_item);
1617 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001618 ceph_con_keepalive(&osd->o_con);
1619 }
1620
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001621 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001622 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001623 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001624 up_read(&osdc->map_sem);
1625}
1626
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001627static void handle_osds_timeout(struct work_struct *work)
1628{
1629 struct ceph_osd_client *osdc =
1630 container_of(work, struct ceph_osd_client,
1631 osds_timeout_work.work);
Ilya Dryomova319bf52015-05-15 12:02:17 +03001632 unsigned long delay = osdc->client->options->osd_idle_ttl / 4;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001633
1634 dout("osds timeout\n");
1635 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001636 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001637 up_read(&osdc->map_sem);
1638
1639 schedule_delayed_work(&osdc->osds_timeout_work,
1640 round_jiffies_relative(delay));
1641}
1642
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001643static int ceph_oloc_decode(void **p, void *end,
1644 struct ceph_object_locator *oloc)
1645{
1646 u8 struct_v, struct_cv;
1647 u32 len;
1648 void *struct_end;
1649 int ret = 0;
1650
1651 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1652 struct_v = ceph_decode_8(p);
1653 struct_cv = ceph_decode_8(p);
1654 if (struct_v < 3) {
1655 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1656 struct_v, struct_cv);
1657 goto e_inval;
1658 }
1659 if (struct_cv > 6) {
1660 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1661 struct_v, struct_cv);
1662 goto e_inval;
1663 }
1664 len = ceph_decode_32(p);
1665 ceph_decode_need(p, end, len, e_inval);
1666 struct_end = *p + len;
1667
1668 oloc->pool = ceph_decode_64(p);
1669 *p += 4; /* skip preferred */
1670
1671 len = ceph_decode_32(p);
1672 if (len > 0) {
1673 pr_warn("ceph_object_locator::key is set\n");
1674 goto e_inval;
1675 }
1676
1677 if (struct_v >= 5) {
1678 len = ceph_decode_32(p);
1679 if (len > 0) {
1680 pr_warn("ceph_object_locator::nspace is set\n");
1681 goto e_inval;
1682 }
1683 }
1684
1685 if (struct_v >= 6) {
1686 s64 hash = ceph_decode_64(p);
1687 if (hash != -1) {
1688 pr_warn("ceph_object_locator::hash is set\n");
1689 goto e_inval;
1690 }
1691 }
1692
1693 /* skip the rest */
1694 *p = struct_end;
1695out:
1696 return ret;
1697
1698e_inval:
1699 ret = -EINVAL;
1700 goto out;
1701}
1702
1703static int ceph_redirect_decode(void **p, void *end,
1704 struct ceph_request_redirect *redir)
1705{
1706 u8 struct_v, struct_cv;
1707 u32 len;
1708 void *struct_end;
1709 int ret;
1710
1711 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1712 struct_v = ceph_decode_8(p);
1713 struct_cv = ceph_decode_8(p);
1714 if (struct_cv > 1) {
1715 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1716 struct_v, struct_cv);
1717 goto e_inval;
1718 }
1719 len = ceph_decode_32(p);
1720 ceph_decode_need(p, end, len, e_inval);
1721 struct_end = *p + len;
1722
1723 ret = ceph_oloc_decode(p, end, &redir->oloc);
1724 if (ret)
1725 goto out;
1726
1727 len = ceph_decode_32(p);
1728 if (len > 0) {
1729 pr_warn("ceph_request_redirect::object_name is set\n");
1730 goto e_inval;
1731 }
1732
1733 len = ceph_decode_32(p);
1734 *p += len; /* skip osd_instructions */
1735
1736 /* skip the rest */
1737 *p = struct_end;
1738out:
1739 return ret;
1740
1741e_inval:
1742 ret = -EINVAL;
1743 goto out;
1744}
1745
Sage Weil25845472011-06-03 09:37:09 -07001746static void complete_request(struct ceph_osd_request *req)
1747{
Sage Weil25845472011-06-03 09:37:09 -07001748 complete_all(&req->r_safe_completion); /* fsync waiter */
1749}
1750
Sage Weilf24e9982009-10-06 11:31:10 -07001751/*
1752 * handle osd op reply. either call the callback if it is specified,
1753 * or do the completion to wake up the waiting thread.
1754 */
Shraddha Barke70cf0522015-10-18 13:55:38 +05301755static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg)
Sage Weilf24e9982009-10-06 11:31:10 -07001756{
Sage Weil1b83bef2013-02-25 16:11:12 -08001757 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001758 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001759 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001760 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001761 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001762 unsigned int numops;
1763 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001764 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001765 s32 retry_attempt;
1766 struct ceph_pg pg;
1767 int err;
1768 u32 reassert_epoch;
1769 u64 reassert_version;
1770 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001771 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001772 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001773 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001774
Sage Weil6df058c2009-12-22 11:24:33 -08001775 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001776 dout("handle_reply %p tid %llu\n", msg, tid);
1777
1778 p = msg->front.iov_base;
1779 end = p + msg->front.iov_len;
1780
1781 ceph_decode_need(&p, end, 4, bad);
1782 object_len = ceph_decode_32(&p);
1783 ceph_decode_need(&p, end, object_len, bad);
1784 p += object_len;
1785
Alex Elderef4859d2013-04-01 18:58:26 -05001786 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001787 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001788 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001789
1790 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1791 flags = ceph_decode_64(&p);
1792 result = ceph_decode_32(&p);
1793 reassert_epoch = ceph_decode_32(&p);
1794 reassert_version = ceph_decode_64(&p);
1795 osdmap_epoch = ceph_decode_32(&p);
1796
Sage Weilf24e9982009-10-06 11:31:10 -07001797 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001798 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001799 mutex_lock(&osdc->request_mutex);
1800 req = __lookup_request(osdc, tid);
1801 if (req == NULL) {
1802 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001803 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001804 }
1805 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001806
1807 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1808 req, result);
1809
Dan Carpenter18741192013-08-15 08:51:58 +03001810 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001811 numops = ceph_decode_32(&p);
1812 if (numops > CEPH_OSD_MAX_OP)
1813 goto bad_put;
1814 if (numops != req->r_num_ops)
1815 goto bad_put;
1816 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001817 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001818 for (i = 0; i < numops; i++) {
1819 struct ceph_osd_op *op = p;
1820 int len;
1821
1822 len = le32_to_cpu(op->payload_len);
1823 req->r_reply_op_len[i] = len;
1824 dout(" op %d has %d bytes\n", i, len);
1825 payload_len += len;
1826 p += sizeof(*op);
1827 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001828 bytes = le32_to_cpu(msg->hdr.data_len);
1829 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001830 pr_warn("sum of op payload lens %d != data_len %d\n",
1831 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001832 goto bad_put;
1833 }
1834
Dan Carpenter18741192013-08-15 08:51:58 +03001835 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001836 retry_attempt = ceph_decode_32(&p);
1837 for (i = 0; i < numops; i++)
1838 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001839
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001840 if (le16_to_cpu(msg->hdr.version) >= 6) {
1841 p += 8 + 4; /* skip replay_version */
1842 p += 8; /* skip user_version */
1843
1844 err = ceph_redirect_decode(&p, end, &redir);
1845 if (err)
1846 goto bad_put;
1847 } else {
1848 redir.oloc.pool = -1;
1849 }
1850
1851 if (redir.oloc.pool != -1) {
1852 dout("redirect pool %lld\n", redir.oloc.pool);
1853
1854 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001855
1856 req->r_target_oloc = redir.oloc; /* struct */
1857
1858 /*
1859 * Start redirect requests with nofail=true. If
1860 * mapping fails, request will end up on the notarget
1861 * list, waiting for the new osdmap (which can take
1862 * a while), even though the original request mapped
1863 * successfully. In the future we might want to follow
1864 * original request's nofail setting here.
1865 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001866 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001867 BUG_ON(err);
1868
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001869 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001870 }
1871
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001872 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001873 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001874 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001875 dout("handle_reply result %d bytes %d\n", req->r_result,
1876 bytes);
1877 if (req->r_result == 0)
1878 req->r_result = bytes;
1879
1880 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001881 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1882 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001883
1884 req->r_got_reply = 1;
1885 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1886 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001887 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001888 }
1889
1890 dout("handle_reply tid %llu flags %d\n", tid, flags);
1891
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001892 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1893 __register_linger_request(osdc, req);
1894
Sage Weilf24e9982009-10-06 11:31:10 -07001895 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001896 if (result < 0 ||
1897 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001898 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1899 __unregister_request(osdc, req);
1900
1901 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001902 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001903
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001904 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001905 if (req->r_unsafe_callback &&
1906 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1907 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001908 if (req->r_callback)
1909 req->r_callback(req, msg);
1910 else
1911 complete_all(&req->r_completion);
1912 }
Sage Weilf24e9982009-10-06 11:31:10 -07001913
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001914 if (flags & CEPH_OSD_FLAG_ONDISK) {
1915 if (req->r_unsafe_callback && already_completed)
1916 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001917 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001918 }
Sage Weilf24e9982009-10-06 11:31:10 -07001919
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001920out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001921 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001922 ceph_osdc_put_request(req);
1923 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001924out_unlock:
1925 mutex_unlock(&osdc->request_mutex);
1926 up_read(&osdc->map_sem);
1927 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001928
Sage Weil1b83bef2013-02-25 16:11:12 -08001929bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001930 req->r_result = -EIO;
1931 __unregister_request(osdc, req);
1932 if (req->r_callback)
1933 req->r_callback(req, msg);
1934 else
1935 complete_all(&req->r_completion);
1936 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001937 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001938bad_mutex:
1939 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001940 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001941bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001942 pr_err("corrupt osd_op_reply got %d %d\n",
1943 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001944 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001945}
1946
Sage Weil6f6c7002011-01-17 20:34:08 -08001947static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001948{
Sage Weilf24e9982009-10-06 11:31:10 -07001949 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001950
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001951 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001952 for (p = rb_first(&osdc->osds); p; p = n) {
1953 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001954
Sage Weil6f6c7002011-01-17 20:34:08 -08001955 n = rb_next(p);
1956 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1957 memcmp(&osd->o_con.peer_addr,
1958 ceph_osd_addr(osdc->osdmap,
1959 osd->o_osd),
1960 sizeof(struct ceph_entity_addr)) != 0)
1961 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001962 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001963}
1964
1965/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001966 * Requeue requests whose mapping to an OSD has changed. If requests map to
1967 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001968 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001969 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001970 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001971static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1972 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001973{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001974 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001975 struct rb_node *p;
1976 int needmap = 0;
1977 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001978 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001979
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001980 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1981 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001982 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001983 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001984 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001985 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001986
1987 /*
1988 * For linger requests that have not yet been
1989 * registered, move them to the linger list; they'll
1990 * be sent to the osd in the loop below. Unregister
1991 * the request before re-registering it as a linger
1992 * request to ensure the __map_request() below
1993 * will decide it needs to be sent.
1994 */
1995 if (req->r_linger && list_empty(&req->r_linger_item)) {
1996 dout("%p tid %llu restart on osd%d\n",
1997 req, req->r_tid,
1998 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001999 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002000 __unregister_request(osdc, req);
2001 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05002002 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06002003 continue;
2004 }
2005
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002006 force_resend_req = force_resend ||
2007 (force_resend_writes &&
2008 req->r_flags & CEPH_OSD_FLAG_WRITE);
2009 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08002010 if (err < 0)
2011 continue; /* error */
2012 if (req->r_osd == NULL) {
2013 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2014 needmap++; /* request a newer map */
2015 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002016 if (!req->r_linger) {
2017 dout("%p tid %llu requeued on osd%d\n", req,
2018 req->r_tid,
2019 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002020 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002021 }
2022 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002023 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002024
2025 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2026 r_linger_item) {
2027 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2028
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002029 err = __map_request(osdc, req,
2030 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002031 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002032 if (err < 0)
2033 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002034 if (req->r_osd == NULL || err > 0) {
2035 if (req->r_osd == NULL) {
2036 dout("lingering %p tid %llu maps to no osd\n",
2037 req, req->r_tid);
2038 /*
2039 * A homeless lingering request makes
2040 * no sense, as it's job is to keep
2041 * a particular OSD connection open.
2042 * Request a newer map and kick the
2043 * request, knowing that it won't be
2044 * resent until we actually get a map
2045 * that can tell us where to send it.
2046 */
2047 needmap++;
2048 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002049
Ilya Dryomovb0494532015-05-11 17:53:10 +03002050 dout("kicking lingering %p tid %llu osd%d\n", req,
2051 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2052 __register_request(osdc, req);
2053 __unregister_linger_request(osdc, req);
2054 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002055 }
Alex Elder14d2f382013-05-15 16:28:33 -05002056 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002057 mutex_unlock(&osdc->request_mutex);
2058
2059 if (needmap) {
2060 dout("%d requests for down osds, need new map\n", needmap);
2061 ceph_monc_request_next_osdmap(&osdc->client->monc);
2062 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002063}
Sage Weil6f6c7002011-01-17 20:34:08 -08002064
2065
Sage Weilf24e9982009-10-06 11:31:10 -07002066/*
2067 * Process updated osd map.
2068 *
2069 * The message contains any number of incremental and full maps, normally
2070 * indicating some sort of topology change in the cluster. Kick requests
2071 * off to different OSDs as needed.
2072 */
2073void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2074{
2075 void *p, *end, *next;
2076 u32 nr_maps, maplen;
2077 u32 epoch;
2078 struct ceph_osdmap *newmap = NULL, *oldmap;
2079 int err;
2080 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002081 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002082
2083 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2084 p = msg->front.iov_base;
2085 end = p + msg->front.iov_len;
2086
2087 /* verify fsid */
2088 ceph_decode_need(&p, end, sizeof(fsid), bad);
2089 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002090 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2091 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002092
2093 down_write(&osdc->map_sem);
2094
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002095 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2096
Sage Weilf24e9982009-10-06 11:31:10 -07002097 /* incremental maps */
2098 ceph_decode_32_safe(&p, end, nr_maps, bad);
2099 dout(" %d inc maps\n", nr_maps);
2100 while (nr_maps > 0) {
2101 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002102 epoch = ceph_decode_32(&p);
2103 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002104 ceph_decode_need(&p, end, maplen, bad);
2105 next = p + maplen;
2106 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2107 dout("applying incremental map %u len %d\n",
2108 epoch, maplen);
2109 newmap = osdmap_apply_incremental(&p, next,
2110 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002111 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002112 if (IS_ERR(newmap)) {
2113 err = PTR_ERR(newmap);
2114 goto bad;
2115 }
Sage Weil30dc6382009-12-21 14:49:37 -08002116 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002117 if (newmap != osdc->osdmap) {
2118 ceph_osdmap_destroy(osdc->osdmap);
2119 osdc->osdmap = newmap;
2120 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002121 was_full = was_full ||
2122 ceph_osdmap_flag(osdc->osdmap,
2123 CEPH_OSDMAP_FULL);
2124 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002125 } else {
2126 dout("ignoring incremental map %u len %d\n",
2127 epoch, maplen);
2128 }
2129 p = next;
2130 nr_maps--;
2131 }
2132 if (newmap)
2133 goto done;
2134
2135 /* full maps */
2136 ceph_decode_32_safe(&p, end, nr_maps, bad);
2137 dout(" %d full maps\n", nr_maps);
2138 while (nr_maps) {
2139 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002140 epoch = ceph_decode_32(&p);
2141 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002142 ceph_decode_need(&p, end, maplen, bad);
2143 if (nr_maps > 1) {
2144 dout("skipping non-latest full map %u len %d\n",
2145 epoch, maplen);
2146 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2147 dout("skipping full map %u len %d, "
2148 "older than our %u\n", epoch, maplen,
2149 osdc->osdmap->epoch);
2150 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002151 int skipped_map = 0;
2152
Sage Weilf24e9982009-10-06 11:31:10 -07002153 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002154 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002155 if (IS_ERR(newmap)) {
2156 err = PTR_ERR(newmap);
2157 goto bad;
2158 }
Sage Weil30dc6382009-12-21 14:49:37 -08002159 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002160 oldmap = osdc->osdmap;
2161 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002162 if (oldmap) {
2163 if (oldmap->epoch + 1 < newmap->epoch)
2164 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002165 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002166 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002167 was_full = was_full ||
2168 ceph_osdmap_flag(osdc->osdmap,
2169 CEPH_OSDMAP_FULL);
2170 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002171 }
2172 p += maplen;
2173 nr_maps--;
2174 }
2175
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002176 if (!osdc->osdmap)
2177 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002178done:
2179 downgrade_write(&osdc->map_sem);
2180 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002181
2182 /*
2183 * subscribe to subsequent osdmap updates if full to ensure
2184 * we find out when we are no longer full and stop returning
2185 * ENOSPC.
2186 */
Josh Durgind29adb32013-12-02 19:11:48 -08002187 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2188 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2189 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002190 ceph_monc_request_next_osdmap(&osdc->client->monc);
2191
Alex Elderf9d25192013-02-15 11:42:29 -06002192 mutex_lock(&osdc->request_mutex);
2193 __send_queued(osdc);
2194 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002195 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002196 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002197 return;
2198
2199bad:
2200 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002201 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002202 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002203}
2204
Sage Weilf24e9982009-10-06 11:31:10 -07002205/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002206 * watch/notify callback event infrastructure
2207 *
2208 * These callbacks are used both for watch and notify operations.
2209 */
2210static void __release_event(struct kref *kref)
2211{
2212 struct ceph_osd_event *event =
2213 container_of(kref, struct ceph_osd_event, kref);
2214
2215 dout("__release_event %p\n", event);
2216 kfree(event);
2217}
2218
2219static void get_event(struct ceph_osd_event *event)
2220{
2221 kref_get(&event->kref);
2222}
2223
2224void ceph_osdc_put_event(struct ceph_osd_event *event)
2225{
2226 kref_put(&event->kref, __release_event);
2227}
2228EXPORT_SYMBOL(ceph_osdc_put_event);
2229
2230static void __insert_event(struct ceph_osd_client *osdc,
2231 struct ceph_osd_event *new)
2232{
2233 struct rb_node **p = &osdc->event_tree.rb_node;
2234 struct rb_node *parent = NULL;
2235 struct ceph_osd_event *event = NULL;
2236
2237 while (*p) {
2238 parent = *p;
2239 event = rb_entry(parent, struct ceph_osd_event, node);
2240 if (new->cookie < event->cookie)
2241 p = &(*p)->rb_left;
2242 else if (new->cookie > event->cookie)
2243 p = &(*p)->rb_right;
2244 else
2245 BUG();
2246 }
2247
2248 rb_link_node(&new->node, parent, p);
2249 rb_insert_color(&new->node, &osdc->event_tree);
2250}
2251
2252static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2253 u64 cookie)
2254{
2255 struct rb_node **p = &osdc->event_tree.rb_node;
2256 struct rb_node *parent = NULL;
2257 struct ceph_osd_event *event = NULL;
2258
2259 while (*p) {
2260 parent = *p;
2261 event = rb_entry(parent, struct ceph_osd_event, node);
2262 if (cookie < event->cookie)
2263 p = &(*p)->rb_left;
2264 else if (cookie > event->cookie)
2265 p = &(*p)->rb_right;
2266 else
2267 return event;
2268 }
2269 return NULL;
2270}
2271
2272static void __remove_event(struct ceph_osd_event *event)
2273{
2274 struct ceph_osd_client *osdc = event->osdc;
2275
2276 if (!RB_EMPTY_NODE(&event->node)) {
2277 dout("__remove_event removed %p\n", event);
2278 rb_erase(&event->node, &osdc->event_tree);
2279 ceph_osdc_put_event(event);
2280 } else {
2281 dout("__remove_event didn't remove %p\n", event);
2282 }
2283}
2284
2285int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2286 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002287 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002288{
2289 struct ceph_osd_event *event;
2290
2291 event = kmalloc(sizeof(*event), GFP_NOIO);
2292 if (!event)
2293 return -ENOMEM;
2294
2295 dout("create_event %p\n", event);
2296 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002297 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002298 event->data = data;
2299 event->osdc = osdc;
2300 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002301 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002302 kref_init(&event->kref); /* one ref for us */
2303 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002304
2305 spin_lock(&osdc->event_lock);
2306 event->cookie = ++osdc->event_count;
2307 __insert_event(osdc, event);
2308 spin_unlock(&osdc->event_lock);
2309
2310 *pevent = event;
2311 return 0;
2312}
2313EXPORT_SYMBOL(ceph_osdc_create_event);
2314
2315void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2316{
2317 struct ceph_osd_client *osdc = event->osdc;
2318
2319 dout("cancel_event %p\n", event);
2320 spin_lock(&osdc->event_lock);
2321 __remove_event(event);
2322 spin_unlock(&osdc->event_lock);
2323 ceph_osdc_put_event(event); /* caller's */
2324}
2325EXPORT_SYMBOL(ceph_osdc_cancel_event);
2326
2327
2328static void do_event_work(struct work_struct *work)
2329{
2330 struct ceph_osd_event_work *event_work =
2331 container_of(work, struct ceph_osd_event_work, work);
2332 struct ceph_osd_event *event = event_work->event;
2333 u64 ver = event_work->ver;
2334 u64 notify_id = event_work->notify_id;
2335 u8 opcode = event_work->opcode;
2336
2337 dout("do_event_work completing %p\n", event);
2338 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002339 dout("do_event_work completed %p\n", event);
2340 ceph_osdc_put_event(event);
2341 kfree(event_work);
2342}
2343
2344
2345/*
2346 * Process osd watch notifications
2347 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002348static void handle_watch_notify(struct ceph_osd_client *osdc,
2349 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002350{
2351 void *p, *end;
2352 u8 proto_ver;
2353 u64 cookie, ver, notify_id;
2354 u8 opcode;
2355 struct ceph_osd_event *event;
2356 struct ceph_osd_event_work *event_work;
2357
2358 p = msg->front.iov_base;
2359 end = p + msg->front.iov_len;
2360
2361 ceph_decode_8_safe(&p, end, proto_ver, bad);
2362 ceph_decode_8_safe(&p, end, opcode, bad);
2363 ceph_decode_64_safe(&p, end, cookie, bad);
2364 ceph_decode_64_safe(&p, end, ver, bad);
2365 ceph_decode_64_safe(&p, end, notify_id, bad);
2366
2367 spin_lock(&osdc->event_lock);
2368 event = __find_event(osdc, cookie);
2369 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002370 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002371 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002372 }
2373 spin_unlock(&osdc->event_lock);
2374 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2375 cookie, ver, event);
2376 if (event) {
2377 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002378 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002379 pr_err("couldn't allocate event_work\n");
2380 ceph_osdc_put_event(event);
2381 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002382 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002383 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002384 event_work->event = event;
2385 event_work->ver = ver;
2386 event_work->notify_id = notify_id;
2387 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002388
2389 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002390 }
2391
2392 return;
2393
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002394bad:
2395 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002396}
2397
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002398/*
Alex Eldere65550f2013-04-05 01:27:12 -05002399 * build new request AND message
2400 *
2401 */
2402void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2403 struct ceph_snap_context *snapc, u64 snap_id,
2404 struct timespec *mtime)
2405{
2406 struct ceph_msg *msg = req->r_request;
2407 void *p;
2408 size_t msg_size;
2409 int flags = req->r_flags;
2410 u64 data_len;
2411 unsigned int i;
2412
2413 req->r_snapid = snap_id;
2414 req->r_snapc = ceph_get_snap_context(snapc);
2415
2416 /* encode request */
2417 msg->hdr.version = cpu_to_le16(4);
2418
2419 p = msg->front.iov_base;
2420 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2421 req->r_request_osdmap_epoch = p;
2422 p += 4;
2423 req->r_request_flags = p;
2424 p += 4;
2425 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2426 ceph_encode_timespec(p, mtime);
2427 p += sizeof(struct ceph_timespec);
2428 req->r_request_reassert_version = p;
2429 p += sizeof(struct ceph_eversion); /* will get filled in */
2430
2431 /* oloc */
2432 ceph_encode_8(&p, 4);
2433 ceph_encode_8(&p, 4);
2434 ceph_encode_32(&p, 8 + 4 + 4);
2435 req->r_request_pool = p;
2436 p += 8;
2437 ceph_encode_32(&p, -1); /* preferred */
2438 ceph_encode_32(&p, 0); /* key len */
2439
2440 ceph_encode_8(&p, 1);
2441 req->r_request_pgid = p;
2442 p += 8 + 4;
2443 ceph_encode_32(&p, -1); /* preferred */
2444
2445 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002446 ceph_encode_32(&p, req->r_base_oid.name_len);
2447 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2448 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2449 req->r_base_oid.name, req->r_base_oid.name_len);
2450 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002451
2452 /* ops--can imply data */
2453 ceph_encode_16(&p, (u16)req->r_num_ops);
2454 data_len = 0;
2455 for (i = 0; i < req->r_num_ops; i++) {
2456 data_len += osd_req_encode_op(req, p, i);
2457 p += sizeof(struct ceph_osd_op);
2458 }
2459
2460 /* snaps */
2461 ceph_encode_64(&p, req->r_snapid);
2462 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2463 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2464 if (req->r_snapc) {
2465 for (i = 0; i < snapc->num_snaps; i++) {
2466 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2467 }
2468 }
2469
2470 req->r_request_attempts = p;
2471 p += 4;
2472
2473 /* data */
2474 if (flags & CEPH_OSD_FLAG_WRITE) {
2475 u16 data_off;
2476
2477 /*
2478 * The header "data_off" is a hint to the receiver
2479 * allowing it to align received data into its
2480 * buffers such that there's no need to re-copy
2481 * it before writing it to disk (direct I/O).
2482 */
2483 data_off = (u16) (off & 0xffff);
2484 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2485 }
2486 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2487
2488 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2489 msg_size = p - msg->front.iov_base;
2490 msg->front.iov_len = msg_size;
2491 msg->hdr.front_len = cpu_to_le32(msg_size);
2492
2493 dout("build_request msg_size was %d\n", (int)msg_size);
2494}
2495EXPORT_SYMBOL(ceph_osdc_build_request);
2496
2497/*
Sage Weilf24e9982009-10-06 11:31:10 -07002498 * Register request, send initial attempt.
2499 */
2500int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2501 struct ceph_osd_request *req,
2502 bool nofail)
2503{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002504 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002505
Sage Weilf24e9982009-10-06 11:31:10 -07002506 down_read(&osdc->map_sem);
2507 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002508
2509 rc = __ceph_osdc_start_request(osdc, req, nofail);
2510
Sage Weilf24e9982009-10-06 11:31:10 -07002511 mutex_unlock(&osdc->request_mutex);
2512 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002513
Sage Weilf24e9982009-10-06 11:31:10 -07002514 return rc;
2515}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002516EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002517
2518/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002519 * Unregister a registered request. The request is not completed (i.e.
2520 * no callbacks or wakeups) - higher layers are supposed to know what
2521 * they are canceling.
2522 */
2523void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2524{
2525 struct ceph_osd_client *osdc = req->r_osdc;
2526
2527 mutex_lock(&osdc->request_mutex);
2528 if (req->r_linger)
2529 __unregister_linger_request(osdc, req);
2530 __unregister_request(osdc, req);
2531 mutex_unlock(&osdc->request_mutex);
2532
2533 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2534}
2535EXPORT_SYMBOL(ceph_osdc_cancel_request);
2536
2537/*
Sage Weilf24e9982009-10-06 11:31:10 -07002538 * wait for a request to complete
2539 */
2540int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2541 struct ceph_osd_request *req)
2542{
2543 int rc;
2544
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002545 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2546
Sage Weilf24e9982009-10-06 11:31:10 -07002547 rc = wait_for_completion_interruptible(&req->r_completion);
2548 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002549 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2550 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002551 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002552 return rc;
2553 }
2554
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002555 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2556 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002557 return req->r_result;
2558}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002559EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002560
2561/*
2562 * sync - wait for all in-flight requests to flush. avoid starvation.
2563 */
2564void ceph_osdc_sync(struct ceph_osd_client *osdc)
2565{
2566 struct ceph_osd_request *req;
2567 u64 last_tid, next_tid = 0;
2568
2569 mutex_lock(&osdc->request_mutex);
2570 last_tid = osdc->last_tid;
2571 while (1) {
2572 req = __lookup_request_ge(osdc, next_tid);
2573 if (!req)
2574 break;
2575 if (req->r_tid > last_tid)
2576 break;
2577
2578 next_tid = req->r_tid + 1;
2579 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2580 continue;
2581
2582 ceph_osdc_get_request(req);
2583 mutex_unlock(&osdc->request_mutex);
2584 dout("sync waiting on tid %llu (last is %llu)\n",
2585 req->r_tid, last_tid);
2586 wait_for_completion(&req->r_safe_completion);
2587 mutex_lock(&osdc->request_mutex);
2588 ceph_osdc_put_request(req);
2589 }
2590 mutex_unlock(&osdc->request_mutex);
2591 dout("sync done (thru tid %llu)\n", last_tid);
2592}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002593EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002594
2595/*
Josh Durgindd935f42013-08-28 21:43:09 -07002596 * Call all pending notify callbacks - for use after a watch is
2597 * unregistered, to make sure no more callbacks for it will be invoked
2598 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002599void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002600{
2601 flush_workqueue(osdc->notify_wq);
2602}
2603EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2604
2605
2606/*
Sage Weilf24e9982009-10-06 11:31:10 -07002607 * init, shutdown
2608 */
2609int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2610{
2611 int err;
2612
2613 dout("init\n");
2614 osdc->client = client;
2615 osdc->osdmap = NULL;
2616 init_rwsem(&osdc->map_sem);
2617 init_completion(&osdc->map_waiters);
2618 osdc->last_requested_map = 0;
2619 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002620 osdc->last_tid = 0;
2621 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002622 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002623 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002624 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002625 INIT_LIST_HEAD(&osdc->req_unsent);
2626 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002627 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002628 osdc->num_requests = 0;
2629 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002630 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002631 spin_lock_init(&osdc->event_lock);
2632 osdc->event_tree = RB_ROOT;
2633 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002634
2635 schedule_delayed_work(&osdc->osds_timeout_work,
Ilya Dryomova319bf52015-05-15 12:02:17 +03002636 round_jiffies_relative(osdc->client->options->osd_idle_ttl));
Sage Weilf24e9982009-10-06 11:31:10 -07002637
Sage Weil5f44f142009-11-18 14:52:18 -08002638 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002639 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2640 sizeof(struct ceph_osd_request));
2641 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002642 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002643
Sage Weild50b4092012-07-09 14:22:34 -07002644 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2645 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002646 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002647 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002648 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002649 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002650 OSD_OPREPLY_FRONT_LEN, 10, true,
2651 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002652 if (err < 0)
2653 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002654
Dan Carpenterdbcae082013-08-15 08:58:59 +03002655 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002656 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002657 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002658 goto out_msgpool_reply;
2659
Sage Weilf24e9982009-10-06 11:31:10 -07002660 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002661
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002662out_msgpool_reply:
2663 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002664out_msgpool:
2665 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002666out_mempool:
2667 mempool_destroy(osdc->req_mempool);
2668out:
2669 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002670}
2671
2672void ceph_osdc_stop(struct ceph_osd_client *osdc)
2673{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002674 flush_workqueue(osdc->notify_wq);
2675 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002676 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002677 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002678 if (osdc->osdmap) {
2679 ceph_osdmap_destroy(osdc->osdmap);
2680 osdc->osdmap = NULL;
2681 }
Sage Weilaca420b2011-08-31 14:45:53 -07002682 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002683 mempool_destroy(osdc->req_mempool);
2684 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002685 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002686}
2687
2688/*
2689 * Read some contiguous pages. If we cross a stripe boundary, shorten
2690 * *plen. Return number of bytes read, or error.
2691 */
2692int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2693 struct ceph_vino vino, struct ceph_file_layout *layout,
2694 u64 off, u64 *plen,
2695 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002696 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002697{
2698 struct ceph_osd_request *req;
2699 int rc = 0;
2700
2701 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2702 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002703 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002704 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002705 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002706 false);
Sage Weil68162822012-09-24 21:01:02 -07002707 if (IS_ERR(req))
2708 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002709
2710 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002711
Alex Elder406e2c92013-04-15 14:50:36 -05002712 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002713 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002714
Alex Eldere0c59482013-03-07 15:38:25 -06002715 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002716 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002717
Alex Elder79528732013-04-03 21:32:51 -05002718 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002719
Sage Weilf24e9982009-10-06 11:31:10 -07002720 rc = ceph_osdc_start_request(osdc, req, false);
2721 if (!rc)
2722 rc = ceph_osdc_wait_request(osdc, req);
2723
2724 ceph_osdc_put_request(req);
2725 dout("readpages result %d\n", rc);
2726 return rc;
2727}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002728EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002729
2730/*
2731 * do a synchronous write on N pages
2732 */
2733int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2734 struct ceph_file_layout *layout,
2735 struct ceph_snap_context *snapc,
2736 u64 off, u64 len,
2737 u32 truncate_seq, u64 truncate_size,
2738 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002739 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002740{
2741 struct ceph_osd_request *req;
2742 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002743 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002744
Alex Elderacead002013-03-14 14:09:05 -05002745 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002746 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002747 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002748 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002749 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002750 true);
Sage Weil68162822012-09-24 21:01:02 -07002751 if (IS_ERR(req))
2752 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002753
2754 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002755 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002756 false, false);
2757 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002758
Alex Elder79528732013-04-03 21:32:51 -05002759 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002760
Alex Elder87f979d2013-02-15 11:42:29 -06002761 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002762 if (!rc)
2763 rc = ceph_osdc_wait_request(osdc, req);
2764
2765 ceph_osdc_put_request(req);
2766 if (rc == 0)
2767 rc = len;
2768 dout("writepages result %d\n", rc);
2769 return rc;
2770}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002771EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002772
Alex Elder5522ae02013-05-01 12:43:04 -05002773int ceph_osdc_setup(void)
2774{
2775 BUG_ON(ceph_osd_request_cache);
2776 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2777 sizeof (struct ceph_osd_request),
2778 __alignof__(struct ceph_osd_request),
2779 0, NULL);
2780
2781 return ceph_osd_request_cache ? 0 : -ENOMEM;
2782}
2783EXPORT_SYMBOL(ceph_osdc_setup);
2784
2785void ceph_osdc_cleanup(void)
2786{
2787 BUG_ON(!ceph_osd_request_cache);
2788 kmem_cache_destroy(ceph_osd_request_cache);
2789 ceph_osd_request_cache = NULL;
2790}
2791EXPORT_SYMBOL(ceph_osdc_cleanup);
2792
Sage Weilf24e9982009-10-06 11:31:10 -07002793/*
2794 * handle incoming message
2795 */
2796static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2797{
2798 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002799 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002800 int type = le16_to_cpu(msg->hdr.type);
2801
2802 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002803 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002804 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002805
2806 switch (type) {
2807 case CEPH_MSG_OSD_MAP:
2808 ceph_osdc_handle_map(osdc, msg);
2809 break;
2810 case CEPH_MSG_OSD_OPREPLY:
Shraddha Barke70cf0522015-10-18 13:55:38 +05302811 handle_reply(osdc, msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002812 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002813 case CEPH_MSG_WATCH_NOTIFY:
2814 handle_watch_notify(osdc, msg);
2815 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002816
2817 default:
2818 pr_err("received unknown message type %d %s\n", type,
2819 ceph_msg_type_name(type));
2820 }
Sage Weil4a32f932010-06-13 10:27:53 -07002821out:
Sage Weilf24e9982009-10-06 11:31:10 -07002822 ceph_msg_put(msg);
2823}
2824
Sage Weil5b3a4db2010-02-19 21:43:23 -08002825/*
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002826 * Lookup and return message for incoming reply. Don't try to do
2827 * anything about a larger than preallocated data portion of the
2828 * message at the moment - for now, just skip the message.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002829 */
2830static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002831 struct ceph_msg_header *hdr,
2832 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002833{
2834 struct ceph_osd *osd = con->private;
2835 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002836 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002837 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002838 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002839 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002840 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002841
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002842 tid = le64_to_cpu(hdr->tid);
2843 mutex_lock(&osdc->request_mutex);
2844 req = __lookup_request(osdc, tid);
2845 if (!req) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002846 pr_warn("%s osd%d tid %llu unknown, skipping\n",
2847 __func__, osd->o_osd, tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002848 m = NULL;
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002849 *skip = 1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002850 goto out;
2851 }
Sage Weilc16e7862010-03-01 13:02:00 -08002852
Alex Elderace6d3a2013-04-01 16:12:14 -05002853 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002854 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002855 req->r_reply, req->r_reply->con);
2856 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002857
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002858 if (front_len > req->r_reply->front_alloc_len) {
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002859 pr_warn("%s osd%d tid %llu front %d > preallocated %d\n",
2860 __func__, osd->o_osd, req->r_tid, front_len,
2861 req->r_reply->front_alloc_len);
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002862 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2863 false);
Sage Weila79832f2010-04-01 16:06:19 -07002864 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002865 goto out;
2866 ceph_msg_put(req->r_reply);
2867 req->r_reply = m;
2868 }
Sage Weilc16e7862010-03-01 13:02:00 -08002869
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002870 if (data_len > req->r_reply->data_length) {
2871 pr_warn("%s osd%d tid %llu data %d > preallocated %zu, skipping\n",
2872 __func__, osd->o_osd, req->r_tid, data_len,
2873 req->r_reply->data_length);
2874 m = NULL;
2875 *skip = 1;
2876 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002877 }
Ilya Dryomovd15f9d62015-09-02 11:37:09 +03002878
2879 m = ceph_msg_get(req->r_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002880 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002881
2882out:
2883 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002884 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002885}
2886
2887static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2888 struct ceph_msg_header *hdr,
2889 int *skip)
2890{
2891 struct ceph_osd *osd = con->private;
2892 int type = le16_to_cpu(hdr->type);
2893 int front = le32_to_cpu(hdr->front_len);
2894
Alex Elder1c20f2d2012-06-04 14:43:32 -05002895 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002896 switch (type) {
2897 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002898 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002899 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002900 case CEPH_MSG_OSD_OPREPLY:
2901 return get_reply(con, hdr, skip);
2902 default:
2903 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2904 osd->o_osd);
2905 *skip = 1;
2906 return NULL;
2907 }
Sage Weilf24e9982009-10-06 11:31:10 -07002908}
2909
2910/*
2911 * Wrappers to refcount containing ceph_osd struct
2912 */
2913static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2914{
2915 struct ceph_osd *osd = con->private;
2916 if (get_osd(osd))
2917 return con;
2918 return NULL;
2919}
2920
2921static void put_osd_con(struct ceph_connection *con)
2922{
2923 struct ceph_osd *osd = con->private;
2924 put_osd(osd);
2925}
2926
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002927/*
2928 * authentication
2929 */
Alex Eldera3530df2012-05-16 15:16:39 -05002930/*
2931 * Note: returned pointer is the address of a structure that's
2932 * managed separately. Caller must *not* attempt to free it.
2933 */
2934static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002935 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002936{
2937 struct ceph_osd *o = con->private;
2938 struct ceph_osd_client *osdc = o->o_osdc;
2939 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002940 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002941
Alex Elder74f18692012-05-16 15:16:39 -05002942 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002943 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002944 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002945 }
Sage Weil27859f92013-03-25 10:26:14 -07002946 if (!auth->authorizer) {
2947 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2948 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002949 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002950 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002951 } else {
2952 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002953 auth);
2954 if (ret)
2955 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002956 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002957 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002958
Alex Eldera3530df2012-05-16 15:16:39 -05002959 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002960}
2961
2962
2963static int verify_authorizer_reply(struct ceph_connection *con, int len)
2964{
2965 struct ceph_osd *o = con->private;
2966 struct ceph_osd_client *osdc = o->o_osdc;
2967 struct ceph_auth_client *ac = osdc->client->monc.auth;
2968
Sage Weil27859f92013-03-25 10:26:14 -07002969 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002970}
2971
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002972static int invalidate_authorizer(struct ceph_connection *con)
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 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002979 return ceph_monc_validate_auth(&osdc->client->monc);
2980}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002981
Yan, Zheng33d07332014-11-04 16:33:37 +08002982static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2983{
2984 struct ceph_osd *o = con->private;
2985 struct ceph_auth_handshake *auth = &o->o_auth;
2986 return ceph_auth_sign_message(auth, msg);
2987}
2988
2989static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2990{
2991 struct ceph_osd *o = con->private;
2992 struct ceph_auth_handshake *auth = &o->o_auth;
2993 return ceph_auth_check_message_signature(auth, msg);
2994}
2995
Tobias Klauser9e327892010-05-20 10:40:19 +02002996static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002997 .get = get_osd_con,
2998 .put = put_osd_con,
2999 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003000 .get_authorizer = get_authorizer,
3001 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003002 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003003 .alloc_msg = alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003004 .sign_message = sign_message,
3005 .check_message_signature = check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003006 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003007};