blob: 75abaa87abaca130edac954af38197551cd9c5ed [file] [log] [blame]
Alex Eldera4ce40a2013-04-05 01:27:12 -05001
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07003
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07004#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07005#include <linux/err.h>
6#include <linux/highmem.h>
7#include <linux/mm.h>
8#include <linux/pagemap.h>
9#include <linux/slab.h>
10#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070011#ifdef CONFIG_BLOCK
12#include <linux/bio.h>
13#endif
Sage Weilf24e9982009-10-06 11:31:10 -070014
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070015#include <linux/ceph/libceph.h>
16#include <linux/ceph/osd_client.h>
17#include <linux/ceph/messenger.h>
18#include <linux/ceph/decode.h>
19#include <linux/ceph/auth.h>
20#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070021
Sage Weilc16e7862010-03-01 13:02:00 -080022#define OSD_OP_FRONT_LEN 4096
23#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080024
Alex Elder5522ae02013-05-01 12:43:04 -050025static struct kmem_cache *ceph_osd_request_cache;
26
Tobias Klauser9e327892010-05-20 10:40:19 +020027static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070028
Alex Elderf9d25192013-02-15 11:42:29 -060029static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080030static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070031static void __register_request(struct ceph_osd_client *osdc,
32 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040033static void __unregister_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070035static void __unregister_linger_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
Ilya Dryomov2cc61282014-09-03 14:41:45 +040037static void __enqueue_request(struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080038static void __send_request(struct ceph_osd_client *osdc,
39 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070040
41/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050067 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070070 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070071
Alex Elder60e56f12013-02-15 11:42:29 -060072 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050073 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070075 if (r < 0)
76 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050077 if (*objlen < orig_len) {
78 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060079 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
Alex Elder75d1c942013-03-13 20:50:00 -050083 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070084
Alex Elder3ff5f382013-02-15 22:10:17 -060085 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070086}
87
Alex Elderc54d47b2013-04-03 01:28:57 -050088static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
89{
90 memset(osd_data, 0, sizeof (*osd_data));
91 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
92}
93
Alex Eldera4ce40a2013-04-05 01:27:12 -050094static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050095 struct page **pages, u64 length, u32 alignment,
96 bool pages_from_pool, bool own_pages)
97{
98 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
99 osd_data->pages = pages;
100 osd_data->length = length;
101 osd_data->alignment = alignment;
102 osd_data->pages_from_pool = pages_from_pool;
103 osd_data->own_pages = own_pages;
104}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500105
Alex Eldera4ce40a2013-04-05 01:27:12 -0500106static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500107 struct ceph_pagelist *pagelist)
108{
109 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
110 osd_data->pagelist = pagelist;
111}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112
113#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500114static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500115 struct bio *bio, size_t bio_length)
116{
117 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
118 osd_data->bio = bio;
119 osd_data->bio_length = bio_length;
120}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500121#endif /* CONFIG_BLOCK */
122
Alex Elder863c7eb2013-04-15 14:50:36 -0500123#define osd_req_op_data(oreq, whch, typ, fld) \
124 ({ \
125 BUG_ON(whch >= (oreq)->r_num_ops); \
126 &(oreq)->r_ops[whch].typ.fld; \
127 })
128
Alex Elder49719772013-02-11 12:33:24 -0600129static struct ceph_osd_data *
130osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
131{
132 BUG_ON(which >= osd_req->r_num_ops);
133
134 return &osd_req->r_ops[which].raw_data_in;
135}
136
Alex Eldera4ce40a2013-04-05 01:27:12 -0500137struct ceph_osd_data *
138osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500139 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500140{
Alex Elder863c7eb2013-04-15 14:50:36 -0500141 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500142}
143EXPORT_SYMBOL(osd_req_op_extent_osd_data);
144
145struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500146osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
147 unsigned int which)
148{
Alex Elder863c7eb2013-04-15 14:50:36 -0500149 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500150}
151EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
152
Alex Elder49719772013-02-11 12:33:24 -0600153void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
154 unsigned int which, struct page **pages,
155 u64 length, u32 alignment,
156 bool pages_from_pool, bool own_pages)
157{
158 struct ceph_osd_data *osd_data;
159
160 osd_data = osd_req_op_raw_data_in(osd_req, which);
161 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
162 pages_from_pool, own_pages);
163}
164EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
165
Alex Eldera4ce40a2013-04-05 01:27:12 -0500166void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500167 unsigned int which, struct page **pages,
168 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500169 bool pages_from_pool, bool own_pages)
170{
171 struct ceph_osd_data *osd_data;
172
Alex Elder863c7eb2013-04-15 14:50:36 -0500173 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500174 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
175 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500176}
177EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
178
179void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500180 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500181{
182 struct ceph_osd_data *osd_data;
183
Alex Elder863c7eb2013-04-15 14:50:36 -0500184 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500185 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500186}
187EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
188
189#ifdef CONFIG_BLOCK
190void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500191 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500192{
193 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500194
195 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500196 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500197}
198EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
199#endif /* CONFIG_BLOCK */
200
201static void osd_req_op_cls_request_info_pagelist(
202 struct ceph_osd_request *osd_req,
203 unsigned int which, struct ceph_pagelist *pagelist)
204{
205 struct ceph_osd_data *osd_data;
206
Alex Elder863c7eb2013-04-15 14:50:36 -0500207 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500208 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500209}
210
Alex Elder04017e22013-04-05 14:46:02 -0500211void osd_req_op_cls_request_data_pagelist(
212 struct ceph_osd_request *osd_req,
213 unsigned int which, struct ceph_pagelist *pagelist)
214{
215 struct ceph_osd_data *osd_data;
216
Alex Elder863c7eb2013-04-15 14:50:36 -0500217 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500218 ceph_osd_data_pagelist_init(osd_data, pagelist);
219}
220EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
221
Alex Elder6c57b552013-04-19 15:34:49 -0500222void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
223 unsigned int which, struct page **pages, u64 length,
224 u32 alignment, bool pages_from_pool, bool own_pages)
225{
226 struct ceph_osd_data *osd_data;
227
228 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
229 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
230 pages_from_pool, own_pages);
231}
232EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
233
Alex Eldera4ce40a2013-04-05 01:27:12 -0500234void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
235 unsigned int which, struct page **pages, u64 length,
236 u32 alignment, bool pages_from_pool, bool own_pages)
237{
238 struct ceph_osd_data *osd_data;
239
Alex Elder863c7eb2013-04-15 14:50:36 -0500240 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500241 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
242 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500243}
244EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
245
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500246static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
247{
248 switch (osd_data->type) {
249 case CEPH_OSD_DATA_TYPE_NONE:
250 return 0;
251 case CEPH_OSD_DATA_TYPE_PAGES:
252 return osd_data->length;
253 case CEPH_OSD_DATA_TYPE_PAGELIST:
254 return (u64)osd_data->pagelist->length;
255#ifdef CONFIG_BLOCK
256 case CEPH_OSD_DATA_TYPE_BIO:
257 return (u64)osd_data->bio_length;
258#endif /* CONFIG_BLOCK */
259 default:
260 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
261 return 0;
262 }
263}
264
Alex Elderc54d47b2013-04-03 01:28:57 -0500265static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
266{
Alex Elder54764922013-04-05 01:27:12 -0500267 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500268 int num_pages;
269
270 num_pages = calc_pages_for((u64)osd_data->alignment,
271 (u64)osd_data->length);
272 ceph_release_page_vector(osd_data->pages, num_pages);
273 }
Alex Elder54764922013-04-05 01:27:12 -0500274 ceph_osd_data_init(osd_data);
275}
276
277static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
278 unsigned int which)
279{
280 struct ceph_osd_req_op *op;
281
282 BUG_ON(which >= osd_req->r_num_ops);
283 op = &osd_req->r_ops[which];
284
285 switch (op->op) {
286 case CEPH_OSD_OP_READ:
287 case CEPH_OSD_OP_WRITE:
288 ceph_osd_data_release(&op->extent.osd_data);
289 break;
290 case CEPH_OSD_OP_CALL:
291 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500292 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500293 ceph_osd_data_release(&op->cls.response_data);
294 break;
295 default:
296 break;
297 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500298}
299
Sage Weilf24e9982009-10-06 11:31:10 -0700300/*
301 * requests
302 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400303static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700304{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400305 struct ceph_osd_request *req = container_of(kref,
306 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500307 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800308
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400309 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
310 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400311 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
312 WARN_ON(!list_empty(&req->r_req_lru_item));
313 WARN_ON(!list_empty(&req->r_osd_item));
314 WARN_ON(!list_empty(&req->r_linger_item));
315 WARN_ON(!list_empty(&req->r_linger_osd_item));
316 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400317
Sage Weil415e49a2009-12-07 13:37:03 -0800318 if (req->r_request)
319 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500320 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500321 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500322 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500323 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600324
Alex Elder54764922013-04-05 01:27:12 -0500325 for (which = 0; which < req->r_num_ops; which++)
326 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600327
Sage Weil415e49a2009-12-07 13:37:03 -0800328 ceph_put_snap_context(req->r_snapc);
329 if (req->r_mempool)
330 mempool_free(req, req->r_osdc->req_mempool);
331 else
Alex Elder5522ae02013-05-01 12:43:04 -0500332 kmem_cache_free(ceph_osd_request_cache, req);
333
Sage Weilf24e9982009-10-06 11:31:10 -0700334}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400335
336void ceph_osdc_get_request(struct ceph_osd_request *req)
337{
338 dout("%s %p (was %d)\n", __func__, req,
339 atomic_read(&req->r_kref.refcount));
340 kref_get(&req->r_kref);
341}
342EXPORT_SYMBOL(ceph_osdc_get_request);
343
344void ceph_osdc_put_request(struct ceph_osd_request *req)
345{
346 dout("%s %p (was %d)\n", __func__, req,
347 atomic_read(&req->r_kref.refcount));
348 kref_put(&req->r_kref, ceph_osdc_release_request);
349}
350EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700351
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700352struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700353 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800354 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600356 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700357{
358 struct ceph_osd_request *req;
359 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800360 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361
Alex Elder79528732013-04-03 21:32:51 -0500362 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
363 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
364
Sage Weil1b83bef2013-02-25 16:11:12 -0800365 msg_size = 4 + 4 + 8 + 8 + 4+8;
366 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
367 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200368 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800369 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
370 msg_size += 8; /* snapid */
371 msg_size += 8; /* snap_seq */
372 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
373 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
375 if (use_mempool) {
376 req = mempool_alloc(osdc->req_mempool, gfp_flags);
377 memset(req, 0, sizeof(*req));
378 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500379 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700380 }
381 if (req == NULL)
382 return NULL;
383
384 req->r_osdc = osdc;
385 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500386 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700387
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388 kref_init(&req->r_kref);
389 init_completion(&req->r_completion);
390 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600391 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700393 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400394 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700395 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700396 INIT_LIST_HEAD(&req->r_osd_item);
397
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200398 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200399 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200400
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 /* create reply message */
402 if (use_mempool)
403 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
404 else
405 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700406 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700407 if (!msg) {
408 ceph_osdc_put_request(req);
409 return NULL;
410 }
411 req->r_reply = msg;
412
413 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700414 if (use_mempool)
415 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
416 else
Sage Weilb61c2762011-08-09 15:03:46 -0700417 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700418 if (!msg) {
419 ceph_osdc_put_request(req);
420 return NULL;
421 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700422
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700423 memset(msg->front.iov_base, 0, msg->front.iov_len);
424
425 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700426
427 return req;
428}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700429EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430
Alex Eldera8dd0a32013-03-13 20:50:00 -0500431static bool osd_req_opcode_valid(u16 opcode)
432{
433 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400434#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
435__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
436#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500437 default:
438 return false;
439 }
440}
441
Alex Elder33803f32013-03-13 20:50:00 -0500442/*
443 * This is an osd op init function for opcodes that have no data or
444 * other information associated with them. It also serves as a
445 * common init routine for all the other init functions, below.
446 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500447static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600448_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500449 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500450{
Alex Elderc99d2d42013-04-05 01:27:11 -0500451 struct ceph_osd_req_op *op;
452
453 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500454 BUG_ON(!osd_req_opcode_valid(opcode));
455
Alex Elderc99d2d42013-04-05 01:27:11 -0500456 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500457 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500458 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500459
460 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500461}
462
Alex Elder49719772013-02-11 12:33:24 -0600463void osd_req_op_init(struct ceph_osd_request *osd_req,
464 unsigned int which, u16 opcode)
465{
466 (void)_osd_req_op_init(osd_req, which, opcode);
467}
468EXPORT_SYMBOL(osd_req_op_init);
469
Alex Elderc99d2d42013-04-05 01:27:11 -0500470void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
471 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500472 u64 offset, u64 length,
473 u64 truncate_size, u32 truncate_seq)
474{
Alex Elder49719772013-02-11 12:33:24 -0600475 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500476 size_t payload_len = 0;
477
Li Wangad7a60d2013-08-15 11:51:44 +0800478 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
479 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
480 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500481
Alex Elder33803f32013-03-13 20:50:00 -0500482 op->extent.offset = offset;
483 op->extent.length = length;
484 op->extent.truncate_size = truncate_size;
485 op->extent.truncate_seq = truncate_seq;
486 if (opcode == CEPH_OSD_OP_WRITE)
487 payload_len += length;
488
489 op->payload_len = payload_len;
490}
491EXPORT_SYMBOL(osd_req_op_extent_init);
492
Alex Elderc99d2d42013-04-05 01:27:11 -0500493void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
494 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500495{
Alex Elderc99d2d42013-04-05 01:27:11 -0500496 struct ceph_osd_req_op *op;
497 u64 previous;
498
499 BUG_ON(which >= osd_req->r_num_ops);
500 op = &osd_req->r_ops[which];
501 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500502
503 if (length == previous)
504 return; /* Nothing to do */
505 BUG_ON(length > previous);
506
507 op->extent.length = length;
508 op->payload_len -= previous - length;
509}
510EXPORT_SYMBOL(osd_req_op_extent_update);
511
Alex Elderc99d2d42013-04-05 01:27:11 -0500512void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500513 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500514{
Alex Elder49719772013-02-11 12:33:24 -0600515 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500516 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500517 size_t payload_len = 0;
518 size_t size;
519
520 BUG_ON(opcode != CEPH_OSD_OP_CALL);
521
Alex Elder5f562df2013-04-05 01:27:12 -0500522 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
523 BUG_ON(!pagelist);
524 ceph_pagelist_init(pagelist);
525
Alex Elder33803f32013-03-13 20:50:00 -0500526 op->cls.class_name = class;
527 size = strlen(class);
528 BUG_ON(size > (size_t) U8_MAX);
529 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500530 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500531 payload_len += size;
532
533 op->cls.method_name = method;
534 size = strlen(method);
535 BUG_ON(size > (size_t) U8_MAX);
536 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500537 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500538 payload_len += size;
539
Alex Eldera4ce40a2013-04-05 01:27:12 -0500540 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500541
Alex Elder33803f32013-03-13 20:50:00 -0500542 op->cls.argc = 0; /* currently unused */
543
544 op->payload_len = payload_len;
545}
546EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500547
Alex Elderc99d2d42013-04-05 01:27:11 -0500548void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
549 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500550 u64 cookie, u64 version, int flag)
551{
Alex Elder49719772013-02-11 12:33:24 -0600552 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500553
Alex Elderc99d2d42013-04-05 01:27:11 -0500554 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500555
556 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500557 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500558 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500559 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500560}
561EXPORT_SYMBOL(osd_req_op_watch_init);
562
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200563void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
564 unsigned int which,
565 u64 expected_object_size,
566 u64 expected_write_size)
567{
568 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
569 CEPH_OSD_OP_SETALLOCHINT);
570
571 op->alloc_hint.expected_object_size = expected_object_size;
572 op->alloc_hint.expected_write_size = expected_write_size;
573
574 /*
575 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
576 * not worth a feature bit. Set FAILOK per-op flag to make
577 * sure older osds don't trip over an unsupported opcode.
578 */
579 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
580}
581EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
582
Alex Elder90af3602013-04-05 14:46:01 -0500583static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500584 struct ceph_osd_data *osd_data)
585{
586 u64 length = ceph_osd_data_length(osd_data);
587
588 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
589 BUG_ON(length > (u64) SIZE_MAX);
590 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500591 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500592 length, osd_data->alignment);
593 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
594 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500595 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500596#ifdef CONFIG_BLOCK
597 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500598 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500599#endif
600 } else {
601 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
602 }
603}
604
Alex Elder175face2013-03-08 13:35:36 -0600605static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500606 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700607{
Alex Elder79528732013-04-03 21:32:51 -0500608 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500609 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500610 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500611 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600612
Alex Elder79528732013-04-03 21:32:51 -0500613 BUG_ON(which >= req->r_num_ops);
614 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500615 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
616 pr_err("unrecognized osd opcode %d\n", src->op);
617
618 return 0;
619 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700620
Alex Elder065a68f2012-04-20 15:49:43 -0500621 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600622 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600623 osd_data = &src->raw_data_in;
624 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600625 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700626 case CEPH_OSD_OP_READ:
627 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800628 case CEPH_OSD_OP_ZERO:
629 case CEPH_OSD_OP_DELETE:
630 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600631 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500632 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600633 dst->extent.offset = cpu_to_le64(src->extent.offset);
634 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700635 dst->extent.truncate_size =
636 cpu_to_le64(src->extent.truncate_size);
637 dst->extent.truncate_seq =
638 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500639 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500640 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500641 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500642 else
Alex Elder04017e22013-04-05 14:46:02 -0500643 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700644 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700645 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700646 dst->cls.class_len = src->cls.class_len;
647 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500648 osd_data = &src->cls.request_info;
649 ceph_osdc_msg_data_add(req->r_request, osd_data);
650 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
651 request_data_len = osd_data->pagelist->length;
652
653 osd_data = &src->cls.request_data;
654 data_length = ceph_osd_data_length(osd_data);
655 if (data_length) {
656 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
657 dst->cls.indata_len = cpu_to_le32(data_length);
658 ceph_osdc_msg_data_add(req->r_request, osd_data);
659 src->payload_len += data_length;
660 request_data_len += data_length;
661 }
662 osd_data = &src->cls.response_data;
663 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700664 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700665 case CEPH_OSD_OP_STARTSYNC:
666 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700667 case CEPH_OSD_OP_NOTIFY_ACK:
668 case CEPH_OSD_OP_WATCH:
669 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
670 dst->watch.ver = cpu_to_le64(src->watch.ver);
671 dst->watch.flag = src->watch.flag;
672 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200673 case CEPH_OSD_OP_SETALLOCHINT:
674 dst->alloc_hint.expected_object_size =
675 cpu_to_le64(src->alloc_hint.expected_object_size);
676 dst->alloc_hint.expected_write_size =
677 cpu_to_le64(src->alloc_hint.expected_write_size);
678 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700679 default:
Alex Elder4c464592013-02-15 11:42:30 -0600680 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600681 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600682 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500683
684 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700685 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200686
Alex Eldera8dd0a32013-03-13 20:50:00 -0500687 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200688 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700689 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600690
Alex Elder54d50642013-04-03 01:28:58 -0500691 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700692}
693
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700694/*
Sage Weilf24e9982009-10-06 11:31:10 -0700695 * build new request AND message, calculate layout, and adjust file
696 * extent as needed.
697 *
698 * if the file was recently truncated, we include information about its
699 * old and new size so that the object can be updated appropriately. (we
700 * avoid synchronously deleting truncated objects because it's slow.)
701 *
702 * if @do_sync, include a 'startsync' command so that the osd will flush
703 * data quickly.
704 */
705struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
706 struct ceph_file_layout *layout,
707 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500708 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700709 int opcode, int flags,
710 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700711 u32 truncate_seq,
712 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600713 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700714{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700715 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500716 u64 objnum = 0;
717 u64 objoff = 0;
718 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500719 u32 object_size;
720 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700721 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700722
Li Wangad7a60d2013-08-15 11:51:44 +0800723 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
724 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
725 opcode != CEPH_OSD_OP_TRUNCATE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700726
Alex Elderacead002013-03-14 14:09:05 -0500727 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600728 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700729 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700730 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500731
Alex Elderd178a9e2012-11-13 21:11:15 -0600732 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700733
734 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500735 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600736 if (r < 0) {
737 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700738 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600739 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500740
Alex Elderd18d1e22013-03-13 20:50:01 -0500741 object_size = le32_to_cpu(layout->fl_object_size);
742 object_base = off - objoff;
Yan, Zhengccca4e32013-06-02 18:40:23 +0800743 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
744 if (truncate_size <= object_base) {
745 truncate_size = 0;
746 } else {
747 truncate_size -= object_base;
748 if (truncate_size > object_size)
749 truncate_size = object_size;
750 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500751 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500752
Alex Elderc99d2d42013-04-05 01:27:11 -0500753 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500754 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500755
Alex Elderacead002013-03-14 14:09:05 -0500756 /*
757 * A second op in the ops array means the caller wants to
758 * also issue a include a 'startsync' command so that the
759 * osd will flush data quickly.
760 */
761 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500762 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500763
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200764 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700765
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200766 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200767 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200768 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600769
Sage Weilf24e9982009-10-06 11:31:10 -0700770 return req;
771}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700772EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700773
774/*
775 * We keep osd requests in an rbtree, sorted by ->r_tid.
776 */
777static void __insert_request(struct ceph_osd_client *osdc,
778 struct ceph_osd_request *new)
779{
780 struct rb_node **p = &osdc->requests.rb_node;
781 struct rb_node *parent = NULL;
782 struct ceph_osd_request *req = NULL;
783
784 while (*p) {
785 parent = *p;
786 req = rb_entry(parent, struct ceph_osd_request, r_node);
787 if (new->r_tid < req->r_tid)
788 p = &(*p)->rb_left;
789 else if (new->r_tid > req->r_tid)
790 p = &(*p)->rb_right;
791 else
792 BUG();
793 }
794
795 rb_link_node(&new->r_node, parent, p);
796 rb_insert_color(&new->r_node, &osdc->requests);
797}
798
799static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
800 u64 tid)
801{
802 struct ceph_osd_request *req;
803 struct rb_node *n = osdc->requests.rb_node;
804
805 while (n) {
806 req = rb_entry(n, struct ceph_osd_request, r_node);
807 if (tid < req->r_tid)
808 n = n->rb_left;
809 else if (tid > req->r_tid)
810 n = n->rb_right;
811 else
812 return req;
813 }
814 return NULL;
815}
816
817static struct ceph_osd_request *
818__lookup_request_ge(struct ceph_osd_client *osdc,
819 u64 tid)
820{
821 struct ceph_osd_request *req;
822 struct rb_node *n = osdc->requests.rb_node;
823
824 while (n) {
825 req = rb_entry(n, struct ceph_osd_request, r_node);
826 if (tid < req->r_tid) {
827 if (!n->rb_left)
828 return req;
829 n = n->rb_left;
830 } else if (tid > req->r_tid) {
831 n = n->rb_right;
832 } else {
833 return req;
834 }
835 }
836 return NULL;
837}
838
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400839static void __kick_linger_request(struct ceph_osd_request *req)
840{
841 struct ceph_osd_client *osdc = req->r_osdc;
842 struct ceph_osd *osd = req->r_osd;
843
844 /*
845 * Linger requests need to be resent with a new tid to avoid
846 * the dup op detection logic on the OSDs. Achieve this with
847 * a re-register dance instead of open-coding.
848 */
849 ceph_osdc_get_request(req);
850 if (!list_empty(&req->r_linger_item))
851 __unregister_linger_request(osdc, req);
852 else
853 __unregister_request(osdc, req);
854 __register_request(osdc, req);
855 ceph_osdc_put_request(req);
856
857 /*
858 * Unless request has been registered as both normal and
859 * lingering, __unregister{,_linger}_request clears r_osd.
860 * However, here we need to preserve r_osd to make sure we
861 * requeue on the same OSD.
862 */
863 WARN_ON(req->r_osd || !osd);
864 req->r_osd = osd;
865
866 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
867 __enqueue_request(req);
868}
869
Sage Weil6f6c7002011-01-17 20:34:08 -0800870/*
871 * Resubmit requests pending on the given osd.
872 */
873static void __kick_osd_requests(struct ceph_osd_client *osdc,
874 struct ceph_osd *osd)
875{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700876 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500877 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400878 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800879 int err;
880
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400881 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800882 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600883 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800884 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400885
Alex Eldere02493c2013-03-25 18:16:11 -0500886 /*
887 * Build up a list of requests to resend by traversing the
888 * osd's list of requests. Requests for a given object are
889 * sent in tid order, and that is also the order they're
890 * kept on this list. Therefore all requests that are in
891 * flight will be found first, followed by all requests that
892 * have not yet been sent. And to resend requests while
893 * preserving this order we will want to put any sent
894 * requests back on the front of the osd client's unsent
895 * list.
896 *
897 * So we build a separate ordered list of already-sent
898 * requests for the affected osd and splice it onto the
899 * front of the osd client's unsent list. Once we've seen a
900 * request that has not yet been sent we're done. Those
901 * requests are already sitting right where they belong.
902 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800903 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500904 if (!req->r_sent)
905 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400906
907 if (!req->r_linger) {
908 dout("%s requeueing %p tid %llu\n", __func__, req,
909 req->r_tid);
910 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700911 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400912 } else {
913 list_move_tail(&req->r_req_lru_item, &resend_linger);
914 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700915 }
Alex Eldere02493c2013-03-25 18:16:11 -0500916 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700917
Alex Eldere02493c2013-03-25 18:16:11 -0500918 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400919 * Both registered and not yet registered linger requests are
920 * enqueued with a new tid on the same OSD. We add/move them
921 * to req_unsent/o_requests at the end to keep things in tid
922 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500923 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700924 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400925 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400926 WARN_ON(!list_empty(&req->r_req_lru_item));
927 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800928 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400929
930 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
931 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800932}
933
Sage Weilf24e9982009-10-06 11:31:10 -0700934/*
Sage Weil81b024e2009-10-09 10:29:18 -0700935 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700936 */
937static void osd_reset(struct ceph_connection *con)
938{
939 struct ceph_osd *osd = con->private;
940 struct ceph_osd_client *osdc;
941
942 if (!osd)
943 return;
944 dout("osd_reset osd%d\n", osd->o_osd);
945 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700946 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800947 mutex_lock(&osdc->request_mutex);
948 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600949 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800950 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700951 up_read(&osdc->map_sem);
952}
953
954/*
955 * Track open sessions with osds.
956 */
Alex Eldere10006f2012-05-26 23:26:43 -0500957static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700958{
959 struct ceph_osd *osd;
960
961 osd = kzalloc(sizeof(*osd), GFP_NOFS);
962 if (!osd)
963 return NULL;
964
965 atomic_set(&osd->o_ref, 1);
966 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500967 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600968 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700969 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700970 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800971 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700972 osd->o_incarnation = 1;
973
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700974 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800975
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800976 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700977 return osd;
978}
979
980static struct ceph_osd *get_osd(struct ceph_osd *osd)
981{
982 if (atomic_inc_not_zero(&osd->o_ref)) {
983 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
984 atomic_read(&osd->o_ref));
985 return osd;
986 } else {
987 dout("get_osd %p FAIL\n", osd);
988 return NULL;
989 }
990}
991
992static void put_osd(struct ceph_osd *osd)
993{
994 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
995 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500996 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700997 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
998
Sage Weil27859f92013-03-25 10:26:14 -0700999 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001000 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001001 }
Sage Weilf24e9982009-10-06 11:31:10 -07001002}
1003
1004/*
1005 * remove an osd from our map
1006 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001007static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001008{
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001009 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 BUG_ON(!list_empty(&osd->o_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001011 BUG_ON(!list_empty(&osd->o_linger_requests));
1012
Sage Weilf24e9982009-10-06 11:31:10 -07001013 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001014 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001015 ceph_con_close(&osd->o_con);
1016 put_osd(osd);
1017}
1018
Sage Weilaca420b2011-08-31 14:45:53 -07001019static void remove_all_osds(struct ceph_osd_client *osdc)
1020{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001021 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001022 mutex_lock(&osdc->request_mutex);
1023 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1024 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1025 struct ceph_osd, o_node);
1026 __remove_osd(osdc, osd);
1027 }
1028 mutex_unlock(&osdc->request_mutex);
1029}
1030
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001031static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1032 struct ceph_osd *osd)
1033{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001034 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001035 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001036
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001037 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001038 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001039}
1040
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001041static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1042 struct ceph_osd *osd)
1043{
1044 dout("%s %p\n", __func__, osd);
1045
1046 if (list_empty(&osd->o_requests) &&
1047 list_empty(&osd->o_linger_requests))
1048 __move_osd_to_lru(osdc, osd);
1049}
1050
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001051static void __remove_osd_from_lru(struct ceph_osd *osd)
1052{
1053 dout("__remove_osd_from_lru %p\n", osd);
1054 if (!list_empty(&osd->o_osd_lru))
1055 list_del_init(&osd->o_osd_lru);
1056}
1057
Sage Weilaca420b2011-08-31 14:45:53 -07001058static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001059{
1060 struct ceph_osd *osd, *nosd;
1061
1062 dout("__remove_old_osds %p\n", osdc);
1063 mutex_lock(&osdc->request_mutex);
1064 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001065 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001066 break;
1067 __remove_osd(osdc, osd);
1068 }
1069 mutex_unlock(&osdc->request_mutex);
1070}
1071
Sage Weilf24e9982009-10-06 11:31:10 -07001072/*
1073 * reset osd connect
1074 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001075static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001076{
Alex Elderc3acb182012-12-07 09:57:58 -06001077 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001078
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001079 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001080 if (list_empty(&osd->o_requests) &&
1081 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001082 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001083
1084 return -ENODEV;
1085 }
1086
1087 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1088 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1089 !ceph_con_opened(&osd->o_con)) {
1090 struct ceph_osd_request *req;
1091
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001092 dout("osd addr hasn't changed and connection never opened, "
1093 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001094 /* touch each r_stamp for handle_timeout()'s benfit */
1095 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1096 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001097
1098 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001099 }
Alex Elderc3acb182012-12-07 09:57:58 -06001100
1101 ceph_con_close(&osd->o_con);
1102 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1103 osd->o_incarnation++;
1104
1105 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001106}
1107
1108static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1109{
1110 struct rb_node **p = &osdc->osds.rb_node;
1111 struct rb_node *parent = NULL;
1112 struct ceph_osd *osd = NULL;
1113
Sage Weilaca420b2011-08-31 14:45:53 -07001114 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001115 while (*p) {
1116 parent = *p;
1117 osd = rb_entry(parent, struct ceph_osd, o_node);
1118 if (new->o_osd < osd->o_osd)
1119 p = &(*p)->rb_left;
1120 else if (new->o_osd > osd->o_osd)
1121 p = &(*p)->rb_right;
1122 else
1123 BUG();
1124 }
1125
1126 rb_link_node(&new->o_node, parent, p);
1127 rb_insert_color(&new->o_node, &osdc->osds);
1128}
1129
1130static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1131{
1132 struct ceph_osd *osd;
1133 struct rb_node *n = osdc->osds.rb_node;
1134
1135 while (n) {
1136 osd = rb_entry(n, struct ceph_osd, o_node);
1137 if (o < osd->o_osd)
1138 n = n->rb_left;
1139 else if (o > osd->o_osd)
1140 n = n->rb_right;
1141 else
1142 return osd;
1143 }
1144 return NULL;
1145}
1146
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001147static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1148{
1149 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001150 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001151}
1152
1153static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1154{
1155 cancel_delayed_work(&osdc->timeout_work);
1156}
Sage Weilf24e9982009-10-06 11:31:10 -07001157
1158/*
1159 * Register request, assign tid. If this is the first request, set up
1160 * the timeout event.
1161 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001162static void __register_request(struct ceph_osd_client *osdc,
1163 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001164{
Sage Weilf24e9982009-10-06 11:31:10 -07001165 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001166 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001167 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001168 __insert_request(osdc, req);
1169 ceph_osdc_get_request(req);
1170 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001171 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001172 dout(" first request, scheduling timeout\n");
1173 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001174 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001175}
1176
Sage Weilf24e9982009-10-06 11:31:10 -07001177/*
1178 * called under osdc->request_mutex
1179 */
1180static void __unregister_request(struct ceph_osd_client *osdc,
1181 struct ceph_osd_request *req)
1182{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001183 if (RB_EMPTY_NODE(&req->r_node)) {
1184 dout("__unregister_request %p tid %lld not registered\n",
1185 req, req->r_tid);
1186 return;
1187 }
1188
Sage Weilf24e9982009-10-06 11:31:10 -07001189 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1190 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001191 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001192 osdc->num_requests--;
1193
Sage Weil0ba64782009-10-08 16:57:16 -07001194 if (req->r_osd) {
1195 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001196 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001197
1198 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001199 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001200 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001201 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001202 }
Sage Weilf24e9982009-10-06 11:31:10 -07001203
Alex Elder7d5f2482012-11-29 08:37:03 -06001204 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001205 ceph_osdc_put_request(req);
1206
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001207 if (osdc->num_requests == 0) {
1208 dout(" no requests, canceling timeout\n");
1209 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001210 }
1211}
1212
1213/*
1214 * Cancel a previously queued request message
1215 */
1216static void __cancel_request(struct ceph_osd_request *req)
1217{
Sage Weil6bc18872010-09-27 10:18:52 -07001218 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001219 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001220 req->r_sent = 0;
1221 }
1222}
1223
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001224static void __register_linger_request(struct ceph_osd_client *osdc,
1225 struct ceph_osd_request *req)
1226{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001227 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1228 WARN_ON(!req->r_linger);
1229
Alex Elder96e4dac2013-05-22 20:54:25 -05001230 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001231 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001232 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001233 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001234 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001235}
1236
1237static void __unregister_linger_request(struct ceph_osd_client *osdc,
1238 struct ceph_osd_request *req)
1239{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001240 WARN_ON(!req->r_linger);
1241
1242 if (list_empty(&req->r_linger_item)) {
1243 dout("%s %p tid %llu not registered\n", __func__, req,
1244 req->r_tid);
1245 return;
1246 }
1247
1248 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001249 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001250
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001251 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001252 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001253 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001254 if (list_empty(&req->r_osd_item))
1255 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001256 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001257 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001258}
1259
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001260void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1261 struct ceph_osd_request *req)
1262{
1263 if (!req->r_linger) {
1264 dout("set_request_linger %p\n", req);
1265 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001266 }
1267}
1268EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1269
Sage Weilf24e9982009-10-06 11:31:10 -07001270/*
Josh Durgind29adb32013-12-02 19:11:48 -08001271 * Returns whether a request should be blocked from being sent
1272 * based on the current osdmap and osd_client settings.
1273 *
1274 * Caller should hold map_sem for read.
1275 */
1276static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1277 struct ceph_osd_request *req)
1278{
1279 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1280 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1281 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1282 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1283 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1284}
1285
1286/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001287 * Calculate mapping of a request to a PG. Takes tiering into account.
1288 */
1289static int __calc_request_pg(struct ceph_osdmap *osdmap,
1290 struct ceph_osd_request *req,
1291 struct ceph_pg *pg_out)
1292{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001293 bool need_check_tiering;
1294
1295 need_check_tiering = false;
1296 if (req->r_target_oloc.pool == -1) {
1297 req->r_target_oloc = req->r_base_oloc; /* struct */
1298 need_check_tiering = true;
1299 }
1300 if (req->r_target_oid.name_len == 0) {
1301 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1302 need_check_tiering = true;
1303 }
1304
1305 if (need_check_tiering &&
1306 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001307 struct ceph_pg_pool_info *pi;
1308
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001309 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001310 if (pi) {
1311 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1312 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001313 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001314 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1315 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001316 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001317 }
1318 /* !pi is caught in ceph_oloc_oid_to_pg() */
1319 }
1320
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001321 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1322 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001323}
1324
Ilya Dryomovf671b582014-09-02 13:40:33 +04001325static void __enqueue_request(struct ceph_osd_request *req)
1326{
1327 struct ceph_osd_client *osdc = req->r_osdc;
1328
1329 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1330 req->r_osd ? req->r_osd->o_osd : -1);
1331
1332 if (req->r_osd) {
1333 __remove_osd_from_lru(req->r_osd);
1334 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1335 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1336 } else {
1337 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1338 }
1339}
1340
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001341/*
Sage Weilf24e9982009-10-06 11:31:10 -07001342 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1343 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001344 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001345 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001346 *
1347 * Return 0 if unchanged, 1 if changed, or negative on error.
1348 *
1349 * Caller should hold map_sem for read and request_mutex.
1350 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001351static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001352 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001353{
Sage Weil5b191d92013-02-23 10:38:16 -08001354 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001355 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001356 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001357 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001358 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001359
Sage Weil6f6c7002011-01-17 20:34:08 -08001360 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001361
1362 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001363 if (err) {
1364 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001365 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001366 }
Sage Weil7740a422010-01-08 15:58:25 -08001367 req->r_pgid = pgid;
1368
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001369 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1370 if (num < 0)
1371 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001372
Josh Durgind29adb32013-12-02 19:11:48 -08001373 was_paused = req->r_paused;
1374 req->r_paused = __req_should_be_paused(osdc, req);
1375 if (was_paused && !req->r_paused)
1376 force_resend = 1;
1377
Sage Weil38d64532011-10-14 13:33:55 -07001378 if ((!force_resend &&
1379 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001380 req->r_sent >= req->r_osd->o_incarnation &&
1381 req->r_num_pg_osds == num &&
1382 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001383 (req->r_osd == NULL && o == -1) ||
1384 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001385 return 0; /* no change */
1386
Sage Weil5b191d92013-02-23 10:38:16 -08001387 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1388 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001389 req->r_osd ? req->r_osd->o_osd : -1);
1390
Sage Weild85b7052010-05-10 10:24:48 -07001391 /* record full pg acting set */
1392 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1393 req->r_num_pg_osds = num;
1394
Sage Weilf24e9982009-10-06 11:31:10 -07001395 if (req->r_osd) {
1396 __cancel_request(req);
1397 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001398 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001399 req->r_osd = NULL;
1400 }
1401
1402 req->r_osd = __lookup_osd(osdc, o);
1403 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001404 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001405 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001406 if (!req->r_osd) {
1407 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001408 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001409 }
Sage Weilf24e9982009-10-06 11:31:10 -07001410
Sage Weil6f6c7002011-01-17 20:34:08 -08001411 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001412 __insert_osd(osdc, req->r_osd);
1413
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001414 ceph_con_open(&req->r_osd->o_con,
1415 CEPH_ENTITY_TYPE_OSD, o,
1416 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001417 }
1418
Ilya Dryomovf671b582014-09-02 13:40:33 +04001419 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001420 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001421
1422out:
Sage Weilf24e9982009-10-06 11:31:10 -07001423 return err;
1424}
1425
1426/*
1427 * caller should hold map_sem (for read) and request_mutex
1428 */
Sage Weil56e925b2012-01-03 12:34:34 -08001429static void __send_request(struct ceph_osd_client *osdc,
1430 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001431{
Sage Weil1b83bef2013-02-25 16:11:12 -08001432 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001433
Sage Weil1b83bef2013-02-25 16:11:12 -08001434 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1435 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1436 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001437
Sage Weil1b83bef2013-02-25 16:11:12 -08001438 /* fill in message content that changes each time we send it */
1439 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1440 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001441 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001442 p = req->r_request_pgid;
1443 ceph_encode_64(&p, req->r_pgid.pool);
1444 ceph_encode_32(&p, req->r_pgid.seed);
1445 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1446 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1447 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001448
Sage Weil3dd72fc2010-03-22 14:42:30 -07001449 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001450 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001451
1452 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001453
Sage Weilf24e9982009-10-06 11:31:10 -07001454 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001455
1456 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001457}
1458
1459/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001460 * Send any requests in the queue (req_unsent).
1461 */
Alex Elderf9d25192013-02-15 11:42:29 -06001462static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001463{
1464 struct ceph_osd_request *req, *tmp;
1465
Alex Elderf9d25192013-02-15 11:42:29 -06001466 dout("__send_queued\n");
1467 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001468 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001469}
1470
1471/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001472 * Caller should hold map_sem for read and request_mutex.
1473 */
1474static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1475 struct ceph_osd_request *req,
1476 bool nofail)
1477{
1478 int rc;
1479
1480 __register_request(osdc, req);
1481 req->r_sent = 0;
1482 req->r_got_reply = 0;
1483 rc = __map_request(osdc, req, 0);
1484 if (rc < 0) {
1485 if (nofail) {
1486 dout("osdc_start_request failed map, "
1487 " will retry %lld\n", req->r_tid);
1488 rc = 0;
1489 } else {
1490 __unregister_request(osdc, req);
1491 }
1492 return rc;
1493 }
1494
1495 if (req->r_osd == NULL) {
1496 dout("send_request %p no up osds in pg\n", req);
1497 ceph_monc_request_next_osdmap(&osdc->client->monc);
1498 } else {
1499 __send_queued(osdc);
1500 }
1501
1502 return 0;
1503}
1504
1505/*
Sage Weilf24e9982009-10-06 11:31:10 -07001506 * Timeout callback, called every N seconds when 1 or more osd
1507 * requests has been active for more than N seconds. When this
1508 * happens, we ping all OSDs with requests who have timed out to
1509 * ensure any communications channel reset is detected. Reset the
1510 * request timeouts another N seconds in the future as we go.
1511 * Reschedule the timeout event another N seconds in future (unless
1512 * there are no open requests).
1513 */
1514static void handle_timeout(struct work_struct *work)
1515{
1516 struct ceph_osd_client *osdc =
1517 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001518 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001519 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001520 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001521 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001522 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001523 dout("timeout\n");
1524 down_read(&osdc->map_sem);
1525
1526 ceph_monc_request_next_osdmap(&osdc->client->monc);
1527
1528 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001529
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001530 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001531 * ping osds that are a bit slow. this ensures that if there
1532 * is a break in the TCP connection we will notice, and reopen
1533 * a connection with that osd (from the fault callback).
1534 */
1535 INIT_LIST_HEAD(&slow_osds);
1536 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001537 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001538 break;
1539
1540 osd = req->r_osd;
1541 BUG_ON(!osd);
1542 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001543 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001544 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1545 }
1546 while (!list_empty(&slow_osds)) {
1547 osd = list_entry(slow_osds.next, struct ceph_osd,
1548 o_keepalive_item);
1549 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001550 ceph_con_keepalive(&osd->o_con);
1551 }
1552
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001553 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001554 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001555 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001556 up_read(&osdc->map_sem);
1557}
1558
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001559static void handle_osds_timeout(struct work_struct *work)
1560{
1561 struct ceph_osd_client *osdc =
1562 container_of(work, struct ceph_osd_client,
1563 osds_timeout_work.work);
1564 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001565 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001566
1567 dout("osds timeout\n");
1568 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001569 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001570 up_read(&osdc->map_sem);
1571
1572 schedule_delayed_work(&osdc->osds_timeout_work,
1573 round_jiffies_relative(delay));
1574}
1575
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001576static int ceph_oloc_decode(void **p, void *end,
1577 struct ceph_object_locator *oloc)
1578{
1579 u8 struct_v, struct_cv;
1580 u32 len;
1581 void *struct_end;
1582 int ret = 0;
1583
1584 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1585 struct_v = ceph_decode_8(p);
1586 struct_cv = ceph_decode_8(p);
1587 if (struct_v < 3) {
1588 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1589 struct_v, struct_cv);
1590 goto e_inval;
1591 }
1592 if (struct_cv > 6) {
1593 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1594 struct_v, struct_cv);
1595 goto e_inval;
1596 }
1597 len = ceph_decode_32(p);
1598 ceph_decode_need(p, end, len, e_inval);
1599 struct_end = *p + len;
1600
1601 oloc->pool = ceph_decode_64(p);
1602 *p += 4; /* skip preferred */
1603
1604 len = ceph_decode_32(p);
1605 if (len > 0) {
1606 pr_warn("ceph_object_locator::key is set\n");
1607 goto e_inval;
1608 }
1609
1610 if (struct_v >= 5) {
1611 len = ceph_decode_32(p);
1612 if (len > 0) {
1613 pr_warn("ceph_object_locator::nspace is set\n");
1614 goto e_inval;
1615 }
1616 }
1617
1618 if (struct_v >= 6) {
1619 s64 hash = ceph_decode_64(p);
1620 if (hash != -1) {
1621 pr_warn("ceph_object_locator::hash is set\n");
1622 goto e_inval;
1623 }
1624 }
1625
1626 /* skip the rest */
1627 *p = struct_end;
1628out:
1629 return ret;
1630
1631e_inval:
1632 ret = -EINVAL;
1633 goto out;
1634}
1635
1636static int ceph_redirect_decode(void **p, void *end,
1637 struct ceph_request_redirect *redir)
1638{
1639 u8 struct_v, struct_cv;
1640 u32 len;
1641 void *struct_end;
1642 int ret;
1643
1644 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1645 struct_v = ceph_decode_8(p);
1646 struct_cv = ceph_decode_8(p);
1647 if (struct_cv > 1) {
1648 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1649 struct_v, struct_cv);
1650 goto e_inval;
1651 }
1652 len = ceph_decode_32(p);
1653 ceph_decode_need(p, end, len, e_inval);
1654 struct_end = *p + len;
1655
1656 ret = ceph_oloc_decode(p, end, &redir->oloc);
1657 if (ret)
1658 goto out;
1659
1660 len = ceph_decode_32(p);
1661 if (len > 0) {
1662 pr_warn("ceph_request_redirect::object_name is set\n");
1663 goto e_inval;
1664 }
1665
1666 len = ceph_decode_32(p);
1667 *p += len; /* skip osd_instructions */
1668
1669 /* skip the rest */
1670 *p = struct_end;
1671out:
1672 return ret;
1673
1674e_inval:
1675 ret = -EINVAL;
1676 goto out;
1677}
1678
Sage Weil25845472011-06-03 09:37:09 -07001679static void complete_request(struct ceph_osd_request *req)
1680{
Sage Weil25845472011-06-03 09:37:09 -07001681 complete_all(&req->r_safe_completion); /* fsync waiter */
1682}
1683
Sage Weilf24e9982009-10-06 11:31:10 -07001684/*
1685 * handle osd op reply. either call the callback if it is specified,
1686 * or do the completion to wake up the waiting thread.
1687 */
Sage Weil350b1c32009-12-22 10:45:45 -08001688static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1689 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001690{
Sage Weil1b83bef2013-02-25 16:11:12 -08001691 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001692 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001693 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001694 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001695 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001696 unsigned int numops;
1697 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001698 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001699 s32 retry_attempt;
1700 struct ceph_pg pg;
1701 int err;
1702 u32 reassert_epoch;
1703 u64 reassert_version;
1704 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001705 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001706 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001707 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001708
Sage Weil6df058c2009-12-22 11:24:33 -08001709 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001710 dout("handle_reply %p tid %llu\n", msg, tid);
1711
1712 p = msg->front.iov_base;
1713 end = p + msg->front.iov_len;
1714
1715 ceph_decode_need(&p, end, 4, bad);
1716 object_len = ceph_decode_32(&p);
1717 ceph_decode_need(&p, end, object_len, bad);
1718 p += object_len;
1719
Alex Elderef4859d2013-04-01 18:58:26 -05001720 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001721 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001722 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001723
1724 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1725 flags = ceph_decode_64(&p);
1726 result = ceph_decode_32(&p);
1727 reassert_epoch = ceph_decode_32(&p);
1728 reassert_version = ceph_decode_64(&p);
1729 osdmap_epoch = ceph_decode_32(&p);
1730
Sage Weilf24e9982009-10-06 11:31:10 -07001731 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001732 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001733 mutex_lock(&osdc->request_mutex);
1734 req = __lookup_request(osdc, tid);
1735 if (req == NULL) {
1736 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001737 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001738 }
1739 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001740
1741 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1742 req, result);
1743
Dan Carpenter18741192013-08-15 08:51:58 +03001744 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001745 numops = ceph_decode_32(&p);
1746 if (numops > CEPH_OSD_MAX_OP)
1747 goto bad_put;
1748 if (numops != req->r_num_ops)
1749 goto bad_put;
1750 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001751 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001752 for (i = 0; i < numops; i++) {
1753 struct ceph_osd_op *op = p;
1754 int len;
1755
1756 len = le32_to_cpu(op->payload_len);
1757 req->r_reply_op_len[i] = len;
1758 dout(" op %d has %d bytes\n", i, len);
1759 payload_len += len;
1760 p += sizeof(*op);
1761 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001762 bytes = le32_to_cpu(msg->hdr.data_len);
1763 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001764 pr_warn("sum of op payload lens %d != data_len %d\n",
1765 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001766 goto bad_put;
1767 }
1768
Dan Carpenter18741192013-08-15 08:51:58 +03001769 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001770 retry_attempt = ceph_decode_32(&p);
1771 for (i = 0; i < numops; i++)
1772 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001773
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001774 if (le16_to_cpu(msg->hdr.version) >= 6) {
1775 p += 8 + 4; /* skip replay_version */
1776 p += 8; /* skip user_version */
1777
1778 err = ceph_redirect_decode(&p, end, &redir);
1779 if (err)
1780 goto bad_put;
1781 } else {
1782 redir.oloc.pool = -1;
1783 }
1784
1785 if (redir.oloc.pool != -1) {
1786 dout("redirect pool %lld\n", redir.oloc.pool);
1787
1788 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001789
1790 req->r_target_oloc = redir.oloc; /* struct */
1791
1792 /*
1793 * Start redirect requests with nofail=true. If
1794 * mapping fails, request will end up on the notarget
1795 * list, waiting for the new osdmap (which can take
1796 * a while), even though the original request mapped
1797 * successfully. In the future we might want to follow
1798 * original request's nofail setting here.
1799 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001800 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001801 BUG_ON(err);
1802
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001803 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001804 }
1805
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001806 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001807 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001808 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001809 dout("handle_reply result %d bytes %d\n", req->r_result,
1810 bytes);
1811 if (req->r_result == 0)
1812 req->r_result = bytes;
1813
1814 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001815 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1816 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001817
1818 req->r_got_reply = 1;
1819 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1820 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001821 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001822 }
1823
1824 dout("handle_reply tid %llu flags %d\n", tid, flags);
1825
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001826 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1827 __register_linger_request(osdc, req);
1828
Sage Weilf24e9982009-10-06 11:31:10 -07001829 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001830 if (result < 0 ||
1831 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001832 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1833 __unregister_request(osdc, req);
1834
1835 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001836 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001837
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001838 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001839 if (req->r_unsafe_callback &&
1840 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1841 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001842 if (req->r_callback)
1843 req->r_callback(req, msg);
1844 else
1845 complete_all(&req->r_completion);
1846 }
Sage Weilf24e9982009-10-06 11:31:10 -07001847
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001848 if (flags & CEPH_OSD_FLAG_ONDISK) {
1849 if (req->r_unsafe_callback && already_completed)
1850 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001851 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001852 }
Sage Weilf24e9982009-10-06 11:31:10 -07001853
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001854out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001855 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001856 ceph_osdc_put_request(req);
1857 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001858out_unlock:
1859 mutex_unlock(&osdc->request_mutex);
1860 up_read(&osdc->map_sem);
1861 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001862
Sage Weil1b83bef2013-02-25 16:11:12 -08001863bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001864 req->r_result = -EIO;
1865 __unregister_request(osdc, req);
1866 if (req->r_callback)
1867 req->r_callback(req, msg);
1868 else
1869 complete_all(&req->r_completion);
1870 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001871 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001872bad_mutex:
1873 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001874 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001875bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001876 pr_err("corrupt osd_op_reply got %d %d\n",
1877 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001878 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001879}
1880
Sage Weil6f6c7002011-01-17 20:34:08 -08001881static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001882{
Sage Weilf24e9982009-10-06 11:31:10 -07001883 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001884
Sage Weil6f6c7002011-01-17 20:34:08 -08001885 for (p = rb_first(&osdc->osds); p; p = n) {
1886 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001887
Sage Weil6f6c7002011-01-17 20:34:08 -08001888 n = rb_next(p);
1889 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1890 memcmp(&osd->o_con.peer_addr,
1891 ceph_osd_addr(osdc->osdmap,
1892 osd->o_osd),
1893 sizeof(struct ceph_entity_addr)) != 0)
1894 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001895 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001896}
1897
1898/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001899 * Requeue requests whose mapping to an OSD has changed. If requests map to
1900 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001901 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001902 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001903 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001904static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1905 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001906{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001907 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001908 struct rb_node *p;
1909 int needmap = 0;
1910 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001911 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001912
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001913 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1914 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001915 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001916 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001917 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001918 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001919
1920 /*
1921 * For linger requests that have not yet been
1922 * registered, move them to the linger list; they'll
1923 * be sent to the osd in the loop below. Unregister
1924 * the request before re-registering it as a linger
1925 * request to ensure the __map_request() below
1926 * will decide it needs to be sent.
1927 */
1928 if (req->r_linger && list_empty(&req->r_linger_item)) {
1929 dout("%p tid %llu restart on osd%d\n",
1930 req, req->r_tid,
1931 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001932 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001933 __unregister_request(osdc, req);
1934 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001935 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001936 continue;
1937 }
1938
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001939 force_resend_req = force_resend ||
1940 (force_resend_writes &&
1941 req->r_flags & CEPH_OSD_FLAG_WRITE);
1942 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001943 if (err < 0)
1944 continue; /* error */
1945 if (req->r_osd == NULL) {
1946 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1947 needmap++; /* request a newer map */
1948 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001949 if (!req->r_linger) {
1950 dout("%p tid %llu requeued on osd%d\n", req,
1951 req->r_tid,
1952 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001953 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001954 }
1955 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001956 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001957
1958 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1959 r_linger_item) {
1960 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1961
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001962 err = __map_request(osdc, req,
1963 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06001964 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001965 if (err == 0)
1966 continue; /* no change and no osd was specified */
1967 if (err < 0)
1968 continue; /* hrm! */
1969 if (req->r_osd == NULL) {
1970 dout("tid %llu maps to no valid osd\n", req->r_tid);
1971 needmap++; /* request a newer map */
1972 continue;
1973 }
1974
1975 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1976 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001977 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001978 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001979 }
Alex Elder14d2f382013-05-15 16:28:33 -05001980 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001981 mutex_unlock(&osdc->request_mutex);
1982
1983 if (needmap) {
1984 dout("%d requests for down osds, need new map\n", needmap);
1985 ceph_monc_request_next_osdmap(&osdc->client->monc);
1986 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001987}
Sage Weil6f6c7002011-01-17 20:34:08 -08001988
1989
Sage Weilf24e9982009-10-06 11:31:10 -07001990/*
1991 * Process updated osd map.
1992 *
1993 * The message contains any number of incremental and full maps, normally
1994 * indicating some sort of topology change in the cluster. Kick requests
1995 * off to different OSDs as needed.
1996 */
1997void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1998{
1999 void *p, *end, *next;
2000 u32 nr_maps, maplen;
2001 u32 epoch;
2002 struct ceph_osdmap *newmap = NULL, *oldmap;
2003 int err;
2004 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002005 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002006
2007 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2008 p = msg->front.iov_base;
2009 end = p + msg->front.iov_len;
2010
2011 /* verify fsid */
2012 ceph_decode_need(&p, end, sizeof(fsid), bad);
2013 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002014 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2015 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002016
2017 down_write(&osdc->map_sem);
2018
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002019 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2020
Sage Weilf24e9982009-10-06 11:31:10 -07002021 /* incremental maps */
2022 ceph_decode_32_safe(&p, end, nr_maps, bad);
2023 dout(" %d inc maps\n", nr_maps);
2024 while (nr_maps > 0) {
2025 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002026 epoch = ceph_decode_32(&p);
2027 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002028 ceph_decode_need(&p, end, maplen, bad);
2029 next = p + maplen;
2030 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2031 dout("applying incremental map %u len %d\n",
2032 epoch, maplen);
2033 newmap = osdmap_apply_incremental(&p, next,
2034 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002035 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002036 if (IS_ERR(newmap)) {
2037 err = PTR_ERR(newmap);
2038 goto bad;
2039 }
Sage Weil30dc6382009-12-21 14:49:37 -08002040 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002041 if (newmap != osdc->osdmap) {
2042 ceph_osdmap_destroy(osdc->osdmap);
2043 osdc->osdmap = newmap;
2044 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002045 was_full = was_full ||
2046 ceph_osdmap_flag(osdc->osdmap,
2047 CEPH_OSDMAP_FULL);
2048 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002049 } else {
2050 dout("ignoring incremental map %u len %d\n",
2051 epoch, maplen);
2052 }
2053 p = next;
2054 nr_maps--;
2055 }
2056 if (newmap)
2057 goto done;
2058
2059 /* full maps */
2060 ceph_decode_32_safe(&p, end, nr_maps, bad);
2061 dout(" %d full maps\n", nr_maps);
2062 while (nr_maps) {
2063 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002064 epoch = ceph_decode_32(&p);
2065 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002066 ceph_decode_need(&p, end, maplen, bad);
2067 if (nr_maps > 1) {
2068 dout("skipping non-latest full map %u len %d\n",
2069 epoch, maplen);
2070 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2071 dout("skipping full map %u len %d, "
2072 "older than our %u\n", epoch, maplen,
2073 osdc->osdmap->epoch);
2074 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002075 int skipped_map = 0;
2076
Sage Weilf24e9982009-10-06 11:31:10 -07002077 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002078 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002079 if (IS_ERR(newmap)) {
2080 err = PTR_ERR(newmap);
2081 goto bad;
2082 }
Sage Weil30dc6382009-12-21 14:49:37 -08002083 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002084 oldmap = osdc->osdmap;
2085 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002086 if (oldmap) {
2087 if (oldmap->epoch + 1 < newmap->epoch)
2088 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002089 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002090 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002091 was_full = was_full ||
2092 ceph_osdmap_flag(osdc->osdmap,
2093 CEPH_OSDMAP_FULL);
2094 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002095 }
2096 p += maplen;
2097 nr_maps--;
2098 }
2099
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002100 if (!osdc->osdmap)
2101 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002102done:
2103 downgrade_write(&osdc->map_sem);
2104 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002105
2106 /*
2107 * subscribe to subsequent osdmap updates if full to ensure
2108 * we find out when we are no longer full and stop returning
2109 * ENOSPC.
2110 */
Josh Durgind29adb32013-12-02 19:11:48 -08002111 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2112 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2113 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002114 ceph_monc_request_next_osdmap(&osdc->client->monc);
2115
Alex Elderf9d25192013-02-15 11:42:29 -06002116 mutex_lock(&osdc->request_mutex);
2117 __send_queued(osdc);
2118 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002119 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002120 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002121 return;
2122
2123bad:
2124 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002125 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002126 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002127}
2128
Sage Weilf24e9982009-10-06 11:31:10 -07002129/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002130 * watch/notify callback event infrastructure
2131 *
2132 * These callbacks are used both for watch and notify operations.
2133 */
2134static void __release_event(struct kref *kref)
2135{
2136 struct ceph_osd_event *event =
2137 container_of(kref, struct ceph_osd_event, kref);
2138
2139 dout("__release_event %p\n", event);
2140 kfree(event);
2141}
2142
2143static void get_event(struct ceph_osd_event *event)
2144{
2145 kref_get(&event->kref);
2146}
2147
2148void ceph_osdc_put_event(struct ceph_osd_event *event)
2149{
2150 kref_put(&event->kref, __release_event);
2151}
2152EXPORT_SYMBOL(ceph_osdc_put_event);
2153
2154static void __insert_event(struct ceph_osd_client *osdc,
2155 struct ceph_osd_event *new)
2156{
2157 struct rb_node **p = &osdc->event_tree.rb_node;
2158 struct rb_node *parent = NULL;
2159 struct ceph_osd_event *event = NULL;
2160
2161 while (*p) {
2162 parent = *p;
2163 event = rb_entry(parent, struct ceph_osd_event, node);
2164 if (new->cookie < event->cookie)
2165 p = &(*p)->rb_left;
2166 else if (new->cookie > event->cookie)
2167 p = &(*p)->rb_right;
2168 else
2169 BUG();
2170 }
2171
2172 rb_link_node(&new->node, parent, p);
2173 rb_insert_color(&new->node, &osdc->event_tree);
2174}
2175
2176static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2177 u64 cookie)
2178{
2179 struct rb_node **p = &osdc->event_tree.rb_node;
2180 struct rb_node *parent = NULL;
2181 struct ceph_osd_event *event = NULL;
2182
2183 while (*p) {
2184 parent = *p;
2185 event = rb_entry(parent, struct ceph_osd_event, node);
2186 if (cookie < event->cookie)
2187 p = &(*p)->rb_left;
2188 else if (cookie > event->cookie)
2189 p = &(*p)->rb_right;
2190 else
2191 return event;
2192 }
2193 return NULL;
2194}
2195
2196static void __remove_event(struct ceph_osd_event *event)
2197{
2198 struct ceph_osd_client *osdc = event->osdc;
2199
2200 if (!RB_EMPTY_NODE(&event->node)) {
2201 dout("__remove_event removed %p\n", event);
2202 rb_erase(&event->node, &osdc->event_tree);
2203 ceph_osdc_put_event(event);
2204 } else {
2205 dout("__remove_event didn't remove %p\n", event);
2206 }
2207}
2208
2209int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2210 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002211 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002212{
2213 struct ceph_osd_event *event;
2214
2215 event = kmalloc(sizeof(*event), GFP_NOIO);
2216 if (!event)
2217 return -ENOMEM;
2218
2219 dout("create_event %p\n", event);
2220 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002221 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002222 event->data = data;
2223 event->osdc = osdc;
2224 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002225 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002226 kref_init(&event->kref); /* one ref for us */
2227 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002228
2229 spin_lock(&osdc->event_lock);
2230 event->cookie = ++osdc->event_count;
2231 __insert_event(osdc, event);
2232 spin_unlock(&osdc->event_lock);
2233
2234 *pevent = event;
2235 return 0;
2236}
2237EXPORT_SYMBOL(ceph_osdc_create_event);
2238
2239void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2240{
2241 struct ceph_osd_client *osdc = event->osdc;
2242
2243 dout("cancel_event %p\n", event);
2244 spin_lock(&osdc->event_lock);
2245 __remove_event(event);
2246 spin_unlock(&osdc->event_lock);
2247 ceph_osdc_put_event(event); /* caller's */
2248}
2249EXPORT_SYMBOL(ceph_osdc_cancel_event);
2250
2251
2252static void do_event_work(struct work_struct *work)
2253{
2254 struct ceph_osd_event_work *event_work =
2255 container_of(work, struct ceph_osd_event_work, work);
2256 struct ceph_osd_event *event = event_work->event;
2257 u64 ver = event_work->ver;
2258 u64 notify_id = event_work->notify_id;
2259 u8 opcode = event_work->opcode;
2260
2261 dout("do_event_work completing %p\n", event);
2262 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002263 dout("do_event_work completed %p\n", event);
2264 ceph_osdc_put_event(event);
2265 kfree(event_work);
2266}
2267
2268
2269/*
2270 * Process osd watch notifications
2271 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002272static void handle_watch_notify(struct ceph_osd_client *osdc,
2273 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002274{
2275 void *p, *end;
2276 u8 proto_ver;
2277 u64 cookie, ver, notify_id;
2278 u8 opcode;
2279 struct ceph_osd_event *event;
2280 struct ceph_osd_event_work *event_work;
2281
2282 p = msg->front.iov_base;
2283 end = p + msg->front.iov_len;
2284
2285 ceph_decode_8_safe(&p, end, proto_ver, bad);
2286 ceph_decode_8_safe(&p, end, opcode, bad);
2287 ceph_decode_64_safe(&p, end, cookie, bad);
2288 ceph_decode_64_safe(&p, end, ver, bad);
2289 ceph_decode_64_safe(&p, end, notify_id, bad);
2290
2291 spin_lock(&osdc->event_lock);
2292 event = __find_event(osdc, cookie);
2293 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002294 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002295 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002296 }
2297 spin_unlock(&osdc->event_lock);
2298 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2299 cookie, ver, event);
2300 if (event) {
2301 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002302 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002303 pr_err("couldn't allocate event_work\n");
2304 ceph_osdc_put_event(event);
2305 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002306 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002307 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002308 event_work->event = event;
2309 event_work->ver = ver;
2310 event_work->notify_id = notify_id;
2311 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002312
2313 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002314 }
2315
2316 return;
2317
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002318bad:
2319 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002320}
2321
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002322/*
Alex Eldere65550f2013-04-05 01:27:12 -05002323 * build new request AND message
2324 *
2325 */
2326void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2327 struct ceph_snap_context *snapc, u64 snap_id,
2328 struct timespec *mtime)
2329{
2330 struct ceph_msg *msg = req->r_request;
2331 void *p;
2332 size_t msg_size;
2333 int flags = req->r_flags;
2334 u64 data_len;
2335 unsigned int i;
2336
2337 req->r_snapid = snap_id;
2338 req->r_snapc = ceph_get_snap_context(snapc);
2339
2340 /* encode request */
2341 msg->hdr.version = cpu_to_le16(4);
2342
2343 p = msg->front.iov_base;
2344 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2345 req->r_request_osdmap_epoch = p;
2346 p += 4;
2347 req->r_request_flags = p;
2348 p += 4;
2349 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2350 ceph_encode_timespec(p, mtime);
2351 p += sizeof(struct ceph_timespec);
2352 req->r_request_reassert_version = p;
2353 p += sizeof(struct ceph_eversion); /* will get filled in */
2354
2355 /* oloc */
2356 ceph_encode_8(&p, 4);
2357 ceph_encode_8(&p, 4);
2358 ceph_encode_32(&p, 8 + 4 + 4);
2359 req->r_request_pool = p;
2360 p += 8;
2361 ceph_encode_32(&p, -1); /* preferred */
2362 ceph_encode_32(&p, 0); /* key len */
2363
2364 ceph_encode_8(&p, 1);
2365 req->r_request_pgid = p;
2366 p += 8 + 4;
2367 ceph_encode_32(&p, -1); /* preferred */
2368
2369 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002370 ceph_encode_32(&p, req->r_base_oid.name_len);
2371 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2372 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2373 req->r_base_oid.name, req->r_base_oid.name_len);
2374 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002375
2376 /* ops--can imply data */
2377 ceph_encode_16(&p, (u16)req->r_num_ops);
2378 data_len = 0;
2379 for (i = 0; i < req->r_num_ops; i++) {
2380 data_len += osd_req_encode_op(req, p, i);
2381 p += sizeof(struct ceph_osd_op);
2382 }
2383
2384 /* snaps */
2385 ceph_encode_64(&p, req->r_snapid);
2386 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2387 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2388 if (req->r_snapc) {
2389 for (i = 0; i < snapc->num_snaps; i++) {
2390 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2391 }
2392 }
2393
2394 req->r_request_attempts = p;
2395 p += 4;
2396
2397 /* data */
2398 if (flags & CEPH_OSD_FLAG_WRITE) {
2399 u16 data_off;
2400
2401 /*
2402 * The header "data_off" is a hint to the receiver
2403 * allowing it to align received data into its
2404 * buffers such that there's no need to re-copy
2405 * it before writing it to disk (direct I/O).
2406 */
2407 data_off = (u16) (off & 0xffff);
2408 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2409 }
2410 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2411
2412 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2413 msg_size = p - msg->front.iov_base;
2414 msg->front.iov_len = msg_size;
2415 msg->hdr.front_len = cpu_to_le32(msg_size);
2416
2417 dout("build_request msg_size was %d\n", (int)msg_size);
2418}
2419EXPORT_SYMBOL(ceph_osdc_build_request);
2420
2421/*
Sage Weilf24e9982009-10-06 11:31:10 -07002422 * Register request, send initial attempt.
2423 */
2424int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2425 struct ceph_osd_request *req,
2426 bool nofail)
2427{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002428 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002429
Sage Weilf24e9982009-10-06 11:31:10 -07002430 down_read(&osdc->map_sem);
2431 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002432
2433 rc = __ceph_osdc_start_request(osdc, req, nofail);
2434
Sage Weilf24e9982009-10-06 11:31:10 -07002435 mutex_unlock(&osdc->request_mutex);
2436 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002437
Sage Weilf24e9982009-10-06 11:31:10 -07002438 return rc;
2439}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002440EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002441
2442/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002443 * Unregister a registered request. The request is not completed (i.e.
2444 * no callbacks or wakeups) - higher layers are supposed to know what
2445 * they are canceling.
2446 */
2447void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2448{
2449 struct ceph_osd_client *osdc = req->r_osdc;
2450
2451 mutex_lock(&osdc->request_mutex);
2452 if (req->r_linger)
2453 __unregister_linger_request(osdc, req);
2454 __unregister_request(osdc, req);
2455 mutex_unlock(&osdc->request_mutex);
2456
2457 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2458}
2459EXPORT_SYMBOL(ceph_osdc_cancel_request);
2460
2461/*
Sage Weilf24e9982009-10-06 11:31:10 -07002462 * wait for a request to complete
2463 */
2464int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2465 struct ceph_osd_request *req)
2466{
2467 int rc;
2468
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002469 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2470
Sage Weilf24e9982009-10-06 11:31:10 -07002471 rc = wait_for_completion_interruptible(&req->r_completion);
2472 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002473 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2474 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002475 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002476 return rc;
2477 }
2478
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002479 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2480 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002481 return req->r_result;
2482}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002483EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002484
2485/*
2486 * sync - wait for all in-flight requests to flush. avoid starvation.
2487 */
2488void ceph_osdc_sync(struct ceph_osd_client *osdc)
2489{
2490 struct ceph_osd_request *req;
2491 u64 last_tid, next_tid = 0;
2492
2493 mutex_lock(&osdc->request_mutex);
2494 last_tid = osdc->last_tid;
2495 while (1) {
2496 req = __lookup_request_ge(osdc, next_tid);
2497 if (!req)
2498 break;
2499 if (req->r_tid > last_tid)
2500 break;
2501
2502 next_tid = req->r_tid + 1;
2503 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2504 continue;
2505
2506 ceph_osdc_get_request(req);
2507 mutex_unlock(&osdc->request_mutex);
2508 dout("sync waiting on tid %llu (last is %llu)\n",
2509 req->r_tid, last_tid);
2510 wait_for_completion(&req->r_safe_completion);
2511 mutex_lock(&osdc->request_mutex);
2512 ceph_osdc_put_request(req);
2513 }
2514 mutex_unlock(&osdc->request_mutex);
2515 dout("sync done (thru tid %llu)\n", last_tid);
2516}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002517EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002518
2519/*
Josh Durgindd935f42013-08-28 21:43:09 -07002520 * Call all pending notify callbacks - for use after a watch is
2521 * unregistered, to make sure no more callbacks for it will be invoked
2522 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002523void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002524{
2525 flush_workqueue(osdc->notify_wq);
2526}
2527EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2528
2529
2530/*
Sage Weilf24e9982009-10-06 11:31:10 -07002531 * init, shutdown
2532 */
2533int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2534{
2535 int err;
2536
2537 dout("init\n");
2538 osdc->client = client;
2539 osdc->osdmap = NULL;
2540 init_rwsem(&osdc->map_sem);
2541 init_completion(&osdc->map_waiters);
2542 osdc->last_requested_map = 0;
2543 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002544 osdc->last_tid = 0;
2545 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002546 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002547 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002548 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002549 INIT_LIST_HEAD(&osdc->req_unsent);
2550 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002551 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002552 osdc->num_requests = 0;
2553 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002554 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002555 spin_lock_init(&osdc->event_lock);
2556 osdc->event_tree = RB_ROOT;
2557 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002558
2559 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002560 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002561
Sage Weil5f44f142009-11-18 14:52:18 -08002562 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002563 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2564 sizeof(struct ceph_osd_request));
2565 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002566 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002567
Sage Weild50b4092012-07-09 14:22:34 -07002568 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2569 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002570 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002571 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002572 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002573 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002574 OSD_OPREPLY_FRONT_LEN, 10, true,
2575 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002576 if (err < 0)
2577 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002578
Dan Carpenterdbcae082013-08-15 08:58:59 +03002579 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002580 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002581 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002582 goto out_msgpool_reply;
2583
Sage Weilf24e9982009-10-06 11:31:10 -07002584 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002585
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002586out_msgpool_reply:
2587 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002588out_msgpool:
2589 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002590out_mempool:
2591 mempool_destroy(osdc->req_mempool);
2592out:
2593 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002594}
2595
2596void ceph_osdc_stop(struct ceph_osd_client *osdc)
2597{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002598 flush_workqueue(osdc->notify_wq);
2599 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002600 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002601 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002602 if (osdc->osdmap) {
2603 ceph_osdmap_destroy(osdc->osdmap);
2604 osdc->osdmap = NULL;
2605 }
Sage Weilaca420b2011-08-31 14:45:53 -07002606 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002607 mempool_destroy(osdc->req_mempool);
2608 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002609 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002610}
2611
2612/*
2613 * Read some contiguous pages. If we cross a stripe boundary, shorten
2614 * *plen. Return number of bytes read, or error.
2615 */
2616int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2617 struct ceph_vino vino, struct ceph_file_layout *layout,
2618 u64 off, u64 *plen,
2619 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002620 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002621{
2622 struct ceph_osd_request *req;
2623 int rc = 0;
2624
2625 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2626 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002627 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002628 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002629 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002630 false);
Sage Weil68162822012-09-24 21:01:02 -07002631 if (IS_ERR(req))
2632 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002633
2634 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002635
Alex Elder406e2c92013-04-15 14:50:36 -05002636 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002637 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002638
Alex Eldere0c59482013-03-07 15:38:25 -06002639 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002640 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002641
Alex Elder79528732013-04-03 21:32:51 -05002642 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002643
Sage Weilf24e9982009-10-06 11:31:10 -07002644 rc = ceph_osdc_start_request(osdc, req, false);
2645 if (!rc)
2646 rc = ceph_osdc_wait_request(osdc, req);
2647
2648 ceph_osdc_put_request(req);
2649 dout("readpages result %d\n", rc);
2650 return rc;
2651}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002652EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002653
2654/*
2655 * do a synchronous write on N pages
2656 */
2657int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2658 struct ceph_file_layout *layout,
2659 struct ceph_snap_context *snapc,
2660 u64 off, u64 len,
2661 u32 truncate_seq, u64 truncate_size,
2662 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002663 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002664{
2665 struct ceph_osd_request *req;
2666 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002667 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002668
Alex Elderacead002013-03-14 14:09:05 -05002669 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002670 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002671 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002672 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002673 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002674 true);
Sage Weil68162822012-09-24 21:01:02 -07002675 if (IS_ERR(req))
2676 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002677
2678 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002679 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002680 false, false);
2681 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002682
Alex Elder79528732013-04-03 21:32:51 -05002683 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002684
Alex Elder87f979d2013-02-15 11:42:29 -06002685 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002686 if (!rc)
2687 rc = ceph_osdc_wait_request(osdc, req);
2688
2689 ceph_osdc_put_request(req);
2690 if (rc == 0)
2691 rc = len;
2692 dout("writepages result %d\n", rc);
2693 return rc;
2694}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002695EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002696
Alex Elder5522ae02013-05-01 12:43:04 -05002697int ceph_osdc_setup(void)
2698{
2699 BUG_ON(ceph_osd_request_cache);
2700 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2701 sizeof (struct ceph_osd_request),
2702 __alignof__(struct ceph_osd_request),
2703 0, NULL);
2704
2705 return ceph_osd_request_cache ? 0 : -ENOMEM;
2706}
2707EXPORT_SYMBOL(ceph_osdc_setup);
2708
2709void ceph_osdc_cleanup(void)
2710{
2711 BUG_ON(!ceph_osd_request_cache);
2712 kmem_cache_destroy(ceph_osd_request_cache);
2713 ceph_osd_request_cache = NULL;
2714}
2715EXPORT_SYMBOL(ceph_osdc_cleanup);
2716
Sage Weilf24e9982009-10-06 11:31:10 -07002717/*
2718 * handle incoming message
2719 */
2720static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2721{
2722 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002723 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002724 int type = le16_to_cpu(msg->hdr.type);
2725
2726 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002727 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002728 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002729
2730 switch (type) {
2731 case CEPH_MSG_OSD_MAP:
2732 ceph_osdc_handle_map(osdc, msg);
2733 break;
2734 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002735 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002736 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002737 case CEPH_MSG_WATCH_NOTIFY:
2738 handle_watch_notify(osdc, msg);
2739 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002740
2741 default:
2742 pr_err("received unknown message type %d %s\n", type,
2743 ceph_msg_type_name(type));
2744 }
Sage Weil4a32f932010-06-13 10:27:53 -07002745out:
Sage Weilf24e9982009-10-06 11:31:10 -07002746 ceph_msg_put(msg);
2747}
2748
Sage Weil5b3a4db2010-02-19 21:43:23 -08002749/*
Sage Weil21b667f2010-03-04 10:22:59 -08002750 * lookup and return message for incoming reply. set up reply message
2751 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002752 */
2753static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002754 struct ceph_msg_header *hdr,
2755 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002756{
2757 struct ceph_osd *osd = con->private;
2758 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002759 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002760 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002761 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002762 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002763 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002764
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002765 tid = le64_to_cpu(hdr->tid);
2766 mutex_lock(&osdc->request_mutex);
2767 req = __lookup_request(osdc, tid);
2768 if (!req) {
2769 *skip = 1;
2770 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002771 dout("get_reply unknown tid %llu from osd%d\n", tid,
2772 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002773 goto out;
2774 }
Sage Weilc16e7862010-03-01 13:02:00 -08002775
Alex Elderace6d3a2013-04-01 16:12:14 -05002776 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002777 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002778 req->r_reply, req->r_reply->con);
2779 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002780
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002781 if (front_len > req->r_reply->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002782 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2783 front_len, req->r_reply->front_alloc_len,
2784 (unsigned int)con->peer_name.type,
2785 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002786 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2787 false);
Sage Weila79832f2010-04-01 16:06:19 -07002788 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002789 goto out;
2790 ceph_msg_put(req->r_reply);
2791 req->r_reply = m;
2792 }
2793 m = ceph_msg_get(req->r_reply);
2794
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002795 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002796 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002797
Alex Eldera4ce40a2013-04-05 01:27:12 -05002798 /*
2799 * XXX This is assuming there is only one op containing
2800 * XXX page data. Probably OK for reads, but this
2801 * XXX ought to be done more generally.
2802 */
Alex Elder406e2c92013-04-15 14:50:36 -05002803 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002804 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002805 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002806 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002807
Joe Perchesb9a67892014-09-09 21:17:29 -07002808 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
Alex Eldere0c59482013-03-07 15:38:25 -06002809 tid, data_len, osd_data->length);
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002810 *skip = 1;
2811 ceph_msg_put(m);
2812 m = NULL;
2813 goto out;
2814 }
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002815 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002816 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002817 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002818 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002819
2820out:
2821 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002822 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002823
2824}
2825
2826static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2827 struct ceph_msg_header *hdr,
2828 int *skip)
2829{
2830 struct ceph_osd *osd = con->private;
2831 int type = le16_to_cpu(hdr->type);
2832 int front = le32_to_cpu(hdr->front_len);
2833
Alex Elder1c20f2d2012-06-04 14:43:32 -05002834 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002835 switch (type) {
2836 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002837 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002838 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002839 case CEPH_MSG_OSD_OPREPLY:
2840 return get_reply(con, hdr, skip);
2841 default:
2842 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2843 osd->o_osd);
2844 *skip = 1;
2845 return NULL;
2846 }
Sage Weilf24e9982009-10-06 11:31:10 -07002847}
2848
2849/*
2850 * Wrappers to refcount containing ceph_osd struct
2851 */
2852static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2853{
2854 struct ceph_osd *osd = con->private;
2855 if (get_osd(osd))
2856 return con;
2857 return NULL;
2858}
2859
2860static void put_osd_con(struct ceph_connection *con)
2861{
2862 struct ceph_osd *osd = con->private;
2863 put_osd(osd);
2864}
2865
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002866/*
2867 * authentication
2868 */
Alex Eldera3530df2012-05-16 15:16:39 -05002869/*
2870 * Note: returned pointer is the address of a structure that's
2871 * managed separately. Caller must *not* attempt to free it.
2872 */
2873static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002874 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002875{
2876 struct ceph_osd *o = con->private;
2877 struct ceph_osd_client *osdc = o->o_osdc;
2878 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002879 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002880
Alex Elder74f18692012-05-16 15:16:39 -05002881 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002882 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002883 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002884 }
Sage Weil27859f92013-03-25 10:26:14 -07002885 if (!auth->authorizer) {
2886 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2887 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002888 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002889 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002890 } else {
2891 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002892 auth);
2893 if (ret)
2894 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002895 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002896 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002897
Alex Eldera3530df2012-05-16 15:16:39 -05002898 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002899}
2900
2901
2902static int verify_authorizer_reply(struct ceph_connection *con, int len)
2903{
2904 struct ceph_osd *o = con->private;
2905 struct ceph_osd_client *osdc = o->o_osdc;
2906 struct ceph_auth_client *ac = osdc->client->monc.auth;
2907
Sage Weil27859f92013-03-25 10:26:14 -07002908 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002909}
2910
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002911static int invalidate_authorizer(struct ceph_connection *con)
2912{
2913 struct ceph_osd *o = con->private;
2914 struct ceph_osd_client *osdc = o->o_osdc;
2915 struct ceph_auth_client *ac = osdc->client->monc.auth;
2916
Sage Weil27859f92013-03-25 10:26:14 -07002917 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002918 return ceph_monc_validate_auth(&osdc->client->monc);
2919}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002920
Tobias Klauser9e327892010-05-20 10:40:19 +02002921static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002922 .get = get_osd_con,
2923 .put = put_osd_con,
2924 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002925 .get_authorizer = get_authorizer,
2926 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002927 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002928 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002929 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002930};