blob: c4ec9239249ae6541a8ee378f95230a42c2f3a3d [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;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800295 case CEPH_OSD_OP_SETXATTR:
296 case CEPH_OSD_OP_CMPXATTR:
297 ceph_osd_data_release(&op->xattr.osd_data);
298 break;
Alex Elder54764922013-04-05 01:27:12 -0500299 default:
300 break;
301 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500302}
303
Sage Weilf24e9982009-10-06 11:31:10 -0700304/*
305 * requests
306 */
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400307static void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700308{
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400309 struct ceph_osd_request *req = container_of(kref,
310 struct ceph_osd_request, r_kref);
Alex Elder54764922013-04-05 01:27:12 -0500311 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800312
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400313 dout("%s %p (r_request %p r_reply %p)\n", __func__, req,
314 req->r_request, req->r_reply);
Ilya Dryomov6562d662014-06-20 14:14:42 +0400315 WARN_ON(!RB_EMPTY_NODE(&req->r_node));
316 WARN_ON(!list_empty(&req->r_req_lru_item));
317 WARN_ON(!list_empty(&req->r_osd_item));
318 WARN_ON(!list_empty(&req->r_linger_item));
319 WARN_ON(!list_empty(&req->r_linger_osd_item));
320 WARN_ON(req->r_osd);
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400321
Sage Weil415e49a2009-12-07 13:37:03 -0800322 if (req->r_request)
323 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500324 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500325 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500326 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500327 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600328
Alex Elder54764922013-04-05 01:27:12 -0500329 for (which = 0; which < req->r_num_ops; which++)
330 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600331
Sage Weil415e49a2009-12-07 13:37:03 -0800332 ceph_put_snap_context(req->r_snapc);
333 if (req->r_mempool)
334 mempool_free(req, req->r_osdc->req_mempool);
335 else
Alex Elder5522ae02013-05-01 12:43:04 -0500336 kmem_cache_free(ceph_osd_request_cache, req);
337
Sage Weilf24e9982009-10-06 11:31:10 -0700338}
Ilya Dryomov9e94af22014-06-20 14:14:42 +0400339
340void ceph_osdc_get_request(struct ceph_osd_request *req)
341{
342 dout("%s %p (was %d)\n", __func__, req,
343 atomic_read(&req->r_kref.refcount));
344 kref_get(&req->r_kref);
345}
346EXPORT_SYMBOL(ceph_osdc_get_request);
347
348void ceph_osdc_put_request(struct ceph_osd_request *req)
349{
350 dout("%s %p (was %d)\n", __func__, req,
351 atomic_read(&req->r_kref.refcount));
352 kref_put(&req->r_kref, ceph_osdc_release_request);
353}
354EXPORT_SYMBOL(ceph_osdc_put_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700355
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700356struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700357 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800358 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700359 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600360 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361{
362 struct ceph_osd_request *req;
363 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800364 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365
Alex Elder79528732013-04-03 21:32:51 -0500366 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
367 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
368
Sage Weil1b83bef2013-02-25 16:11:12 -0800369 msg_size = 4 + 4 + 8 + 8 + 4+8;
370 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
371 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200372 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800373 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
374 msg_size += 8; /* snapid */
375 msg_size += 8; /* snap_seq */
376 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
377 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378
379 if (use_mempool) {
380 req = mempool_alloc(osdc->req_mempool, gfp_flags);
381 memset(req, 0, sizeof(*req));
382 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500383 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700384 }
385 if (req == NULL)
386 return NULL;
387
388 req->r_osdc = osdc;
389 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500390 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700391
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700392 kref_init(&req->r_kref);
393 init_completion(&req->r_completion);
394 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600395 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700396 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700397 INIT_LIST_HEAD(&req->r_linger_item);
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400398 INIT_LIST_HEAD(&req->r_linger_osd_item);
Sage Weil935b6392011-09-16 11:13:17 -0700399 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700400 INIT_LIST_HEAD(&req->r_osd_item);
401
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200402 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200403 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200404
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700405 /* create reply message */
406 if (use_mempool)
407 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
408 else
409 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700410 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700411 if (!msg) {
412 ceph_osdc_put_request(req);
413 return NULL;
414 }
415 req->r_reply = msg;
416
417 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700418 if (use_mempool)
419 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
420 else
Sage Weilb61c2762011-08-09 15:03:46 -0700421 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700422 if (!msg) {
423 ceph_osdc_put_request(req);
424 return NULL;
425 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700426
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700427 memset(msg->front.iov_base, 0, msg->front.iov_len);
428
429 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430
431 return req;
432}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700433EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700434
Alex Eldera8dd0a32013-03-13 20:50:00 -0500435static bool osd_req_opcode_valid(u16 opcode)
436{
437 switch (opcode) {
Ilya Dryomov70b5bfa2014-10-02 17:22:29 +0400438#define GENERATE_CASE(op, opcode, str) case CEPH_OSD_OP_##op: return true;
439__CEPH_FORALL_OSD_OPS(GENERATE_CASE)
440#undef GENERATE_CASE
Alex Eldera8dd0a32013-03-13 20:50:00 -0500441 default:
442 return false;
443 }
444}
445
Alex Elder33803f32013-03-13 20:50:00 -0500446/*
447 * This is an osd op init function for opcodes that have no data or
448 * other information associated with them. It also serves as a
449 * common init routine for all the other init functions, below.
450 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500451static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600452_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500453 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500454{
Alex Elderc99d2d42013-04-05 01:27:11 -0500455 struct ceph_osd_req_op *op;
456
457 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500458 BUG_ON(!osd_req_opcode_valid(opcode));
459
Alex Elderc99d2d42013-04-05 01:27:11 -0500460 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500461 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500462 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500463
464 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500465}
466
Alex Elder49719772013-02-11 12:33:24 -0600467void osd_req_op_init(struct ceph_osd_request *osd_req,
468 unsigned int which, u16 opcode)
469{
470 (void)_osd_req_op_init(osd_req, which, opcode);
471}
472EXPORT_SYMBOL(osd_req_op_init);
473
Alex Elderc99d2d42013-04-05 01:27:11 -0500474void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
475 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500476 u64 offset, u64 length,
477 u64 truncate_size, u32 truncate_seq)
478{
Alex Elder49719772013-02-11 12:33:24 -0600479 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500480 size_t payload_len = 0;
481
Li Wangad7a60d2013-08-15 11:51:44 +0800482 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800483 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500484
Alex Elder33803f32013-03-13 20:50:00 -0500485 op->extent.offset = offset;
486 op->extent.length = length;
487 op->extent.truncate_size = truncate_size;
488 op->extent.truncate_seq = truncate_seq;
489 if (opcode == CEPH_OSD_OP_WRITE)
490 payload_len += length;
491
492 op->payload_len = payload_len;
493}
494EXPORT_SYMBOL(osd_req_op_extent_init);
495
Alex Elderc99d2d42013-04-05 01:27:11 -0500496void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
497 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500498{
Alex Elderc99d2d42013-04-05 01:27:11 -0500499 struct ceph_osd_req_op *op;
500 u64 previous;
501
502 BUG_ON(which >= osd_req->r_num_ops);
503 op = &osd_req->r_ops[which];
504 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500505
506 if (length == previous)
507 return; /* Nothing to do */
508 BUG_ON(length > previous);
509
510 op->extent.length = length;
511 op->payload_len -= previous - length;
512}
513EXPORT_SYMBOL(osd_req_op_extent_update);
514
Alex Elderc99d2d42013-04-05 01:27:11 -0500515void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500516 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500517{
Alex Elder49719772013-02-11 12:33:24 -0600518 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500519 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500520 size_t payload_len = 0;
521 size_t size;
522
523 BUG_ON(opcode != CEPH_OSD_OP_CALL);
524
Alex Elder5f562df2013-04-05 01:27:12 -0500525 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
526 BUG_ON(!pagelist);
527 ceph_pagelist_init(pagelist);
528
Alex Elder33803f32013-03-13 20:50:00 -0500529 op->cls.class_name = class;
530 size = strlen(class);
531 BUG_ON(size > (size_t) U8_MAX);
532 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500533 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500534 payload_len += size;
535
536 op->cls.method_name = method;
537 size = strlen(method);
538 BUG_ON(size > (size_t) U8_MAX);
539 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500540 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500541 payload_len += size;
542
Alex Eldera4ce40a2013-04-05 01:27:12 -0500543 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500544
Alex Elder33803f32013-03-13 20:50:00 -0500545 op->cls.argc = 0; /* currently unused */
546
547 op->payload_len = payload_len;
548}
549EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500550
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800551int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
552 u16 opcode, const char *name, const void *value,
553 size_t size, u8 cmp_op, u8 cmp_mode)
554{
555 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
556 struct ceph_pagelist *pagelist;
557 size_t payload_len;
558
559 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
560
561 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
562 if (!pagelist)
563 return -ENOMEM;
564
565 ceph_pagelist_init(pagelist);
566
567 payload_len = strlen(name);
568 op->xattr.name_len = payload_len;
569 ceph_pagelist_append(pagelist, name, payload_len);
570
571 op->xattr.value_len = size;
572 ceph_pagelist_append(pagelist, value, size);
573 payload_len += size;
574
575 op->xattr.cmp_op = cmp_op;
576 op->xattr.cmp_mode = cmp_mode;
577
578 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
579 op->payload_len = payload_len;
580 return 0;
581}
582EXPORT_SYMBOL(osd_req_op_xattr_init);
583
Alex Elderc99d2d42013-04-05 01:27:11 -0500584void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
585 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500586 u64 cookie, u64 version, int flag)
587{
Alex Elder49719772013-02-11 12:33:24 -0600588 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500589
Alex Elderc99d2d42013-04-05 01:27:11 -0500590 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500591
592 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500593 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500594 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500595 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500596}
597EXPORT_SYMBOL(osd_req_op_watch_init);
598
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200599void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
600 unsigned int which,
601 u64 expected_object_size,
602 u64 expected_write_size)
603{
604 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
605 CEPH_OSD_OP_SETALLOCHINT);
606
607 op->alloc_hint.expected_object_size = expected_object_size;
608 op->alloc_hint.expected_write_size = expected_write_size;
609
610 /*
611 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
612 * not worth a feature bit. Set FAILOK per-op flag to make
613 * sure older osds don't trip over an unsupported opcode.
614 */
615 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
616}
617EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
618
Alex Elder90af3602013-04-05 14:46:01 -0500619static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500620 struct ceph_osd_data *osd_data)
621{
622 u64 length = ceph_osd_data_length(osd_data);
623
624 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
625 BUG_ON(length > (u64) SIZE_MAX);
626 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500627 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500628 length, osd_data->alignment);
629 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
630 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500631 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500632#ifdef CONFIG_BLOCK
633 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500634 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500635#endif
636 } else {
637 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
638 }
639}
640
Alex Elder175face2013-03-08 13:35:36 -0600641static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500642 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700643{
Alex Elder79528732013-04-03 21:32:51 -0500644 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500645 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500646 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500647 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600648
Alex Elder79528732013-04-03 21:32:51 -0500649 BUG_ON(which >= req->r_num_ops);
650 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500651 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
652 pr_err("unrecognized osd opcode %d\n", src->op);
653
654 return 0;
655 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700656
Alex Elder065a68f2012-04-20 15:49:43 -0500657 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600658 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600659 osd_data = &src->raw_data_in;
660 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600661 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700662 case CEPH_OSD_OP_READ:
663 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800664 case CEPH_OSD_OP_ZERO:
Li Wangad7a60d2013-08-15 11:51:44 +0800665 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600666 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500667 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600668 dst->extent.offset = cpu_to_le64(src->extent.offset);
669 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700670 dst->extent.truncate_size =
671 cpu_to_le64(src->extent.truncate_size);
672 dst->extent.truncate_seq =
673 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500674 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500675 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500676 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500677 else
Alex Elder04017e22013-04-05 14:46:02 -0500678 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700679 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700680 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700681 dst->cls.class_len = src->cls.class_len;
682 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500683 osd_data = &src->cls.request_info;
684 ceph_osdc_msg_data_add(req->r_request, osd_data);
685 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
686 request_data_len = osd_data->pagelist->length;
687
688 osd_data = &src->cls.request_data;
689 data_length = ceph_osd_data_length(osd_data);
690 if (data_length) {
691 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
692 dst->cls.indata_len = cpu_to_le32(data_length);
693 ceph_osdc_msg_data_add(req->r_request, osd_data);
694 src->payload_len += data_length;
695 request_data_len += data_length;
696 }
697 osd_data = &src->cls.response_data;
698 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700699 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700700 case CEPH_OSD_OP_STARTSYNC:
701 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700702 case CEPH_OSD_OP_NOTIFY_ACK:
703 case CEPH_OSD_OP_WATCH:
704 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
705 dst->watch.ver = cpu_to_le64(src->watch.ver);
706 dst->watch.flag = src->watch.flag;
707 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200708 case CEPH_OSD_OP_SETALLOCHINT:
709 dst->alloc_hint.expected_object_size =
710 cpu_to_le64(src->alloc_hint.expected_object_size);
711 dst->alloc_hint.expected_write_size =
712 cpu_to_le64(src->alloc_hint.expected_write_size);
713 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800714 case CEPH_OSD_OP_SETXATTR:
715 case CEPH_OSD_OP_CMPXATTR:
716 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
717 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
718 dst->xattr.cmp_op = src->xattr.cmp_op;
719 dst->xattr.cmp_mode = src->xattr.cmp_mode;
720 osd_data = &src->xattr.osd_data;
721 ceph_osdc_msg_data_add(req->r_request, osd_data);
722 request_data_len = osd_data->pagelist->length;
723 break;
Yan, Zheng864e9192014-11-13 10:47:25 +0800724 case CEPH_OSD_OP_CREATE:
725 case CEPH_OSD_OP_DELETE:
726 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700727 default:
Alex Elder4c464592013-02-15 11:42:30 -0600728 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600729 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600730 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500731
732 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700733 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200734
Alex Eldera8dd0a32013-03-13 20:50:00 -0500735 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200736 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700737 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600738
Alex Elder54d50642013-04-03 01:28:58 -0500739 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700740}
741
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700742/*
Sage Weilf24e9982009-10-06 11:31:10 -0700743 * build new request AND message, calculate layout, and adjust file
744 * extent as needed.
745 *
746 * if the file was recently truncated, we include information about its
747 * old and new size so that the object can be updated appropriately. (we
748 * avoid synchronously deleting truncated objects because it's slow.)
749 *
750 * if @do_sync, include a 'startsync' command so that the osd will flush
751 * data quickly.
752 */
753struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
754 struct ceph_file_layout *layout,
755 struct ceph_vino vino,
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800756 u64 off, u64 *plen,
757 unsigned int which, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700758 int opcode, int flags,
759 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700760 u32 truncate_seq,
761 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600762 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700763{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700764 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500765 u64 objnum = 0;
766 u64 objoff = 0;
767 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700768 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700769
Li Wangad7a60d2013-08-15 11:51:44 +0800770 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
Yan, Zheng864e9192014-11-13 10:47:25 +0800771 opcode != CEPH_OSD_OP_ZERO && opcode != CEPH_OSD_OP_TRUNCATE &&
772 opcode != CEPH_OSD_OP_CREATE && opcode != CEPH_OSD_OP_DELETE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700773
Alex Elderacead002013-03-14 14:09:05 -0500774 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600775 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700776 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700777 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500778
Alex Elderd178a9e2012-11-13 21:11:15 -0600779 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700780
781 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500782 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600783 if (r < 0) {
784 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700785 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600786 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500787
Yan, Zheng864e9192014-11-13 10:47:25 +0800788 if (opcode == CEPH_OSD_OP_CREATE || opcode == CEPH_OSD_OP_DELETE) {
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800789 osd_req_op_init(req, which, opcode);
Yan, Zheng864e9192014-11-13 10:47:25 +0800790 } else {
791 u32 object_size = le32_to_cpu(layout->fl_object_size);
792 u32 object_base = off - objoff;
793 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
794 if (truncate_size <= object_base) {
795 truncate_size = 0;
796 } else {
797 truncate_size -= object_base;
798 if (truncate_size > object_size)
799 truncate_size = object_size;
800 }
Yan, Zhengccca4e32013-06-02 18:40:23 +0800801 }
Yan, Zheng715e4cd2014-11-13 14:40:37 +0800802 osd_req_op_extent_init(req, which, opcode, objoff, objlen,
Yan, Zheng864e9192014-11-13 10:47:25 +0800803 truncate_size, truncate_seq);
Alex Eldera19dadf2013-03-13 20:50:01 -0500804 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500805
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200806 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700807
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200808 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200809 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200810 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600811
Sage Weilf24e9982009-10-06 11:31:10 -0700812 return req;
813}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700814EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700815
816/*
817 * We keep osd requests in an rbtree, sorted by ->r_tid.
818 */
819static void __insert_request(struct ceph_osd_client *osdc,
820 struct ceph_osd_request *new)
821{
822 struct rb_node **p = &osdc->requests.rb_node;
823 struct rb_node *parent = NULL;
824 struct ceph_osd_request *req = NULL;
825
826 while (*p) {
827 parent = *p;
828 req = rb_entry(parent, struct ceph_osd_request, r_node);
829 if (new->r_tid < req->r_tid)
830 p = &(*p)->rb_left;
831 else if (new->r_tid > req->r_tid)
832 p = &(*p)->rb_right;
833 else
834 BUG();
835 }
836
837 rb_link_node(&new->r_node, parent, p);
838 rb_insert_color(&new->r_node, &osdc->requests);
839}
840
841static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
842 u64 tid)
843{
844 struct ceph_osd_request *req;
845 struct rb_node *n = osdc->requests.rb_node;
846
847 while (n) {
848 req = rb_entry(n, struct ceph_osd_request, r_node);
849 if (tid < req->r_tid)
850 n = n->rb_left;
851 else if (tid > req->r_tid)
852 n = n->rb_right;
853 else
854 return req;
855 }
856 return NULL;
857}
858
859static struct ceph_osd_request *
860__lookup_request_ge(struct ceph_osd_client *osdc,
861 u64 tid)
862{
863 struct ceph_osd_request *req;
864 struct rb_node *n = osdc->requests.rb_node;
865
866 while (n) {
867 req = rb_entry(n, struct ceph_osd_request, r_node);
868 if (tid < req->r_tid) {
869 if (!n->rb_left)
870 return req;
871 n = n->rb_left;
872 } else if (tid > req->r_tid) {
873 n = n->rb_right;
874 } else {
875 return req;
876 }
877 }
878 return NULL;
879}
880
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400881static void __kick_linger_request(struct ceph_osd_request *req)
882{
883 struct ceph_osd_client *osdc = req->r_osdc;
884 struct ceph_osd *osd = req->r_osd;
885
886 /*
887 * Linger requests need to be resent with a new tid to avoid
888 * the dup op detection logic on the OSDs. Achieve this with
889 * a re-register dance instead of open-coding.
890 */
891 ceph_osdc_get_request(req);
892 if (!list_empty(&req->r_linger_item))
893 __unregister_linger_request(osdc, req);
894 else
895 __unregister_request(osdc, req);
896 __register_request(osdc, req);
897 ceph_osdc_put_request(req);
898
899 /*
900 * Unless request has been registered as both normal and
901 * lingering, __unregister{,_linger}_request clears r_osd.
902 * However, here we need to preserve r_osd to make sure we
903 * requeue on the same OSD.
904 */
905 WARN_ON(req->r_osd || !osd);
906 req->r_osd = osd;
907
908 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
909 __enqueue_request(req);
910}
911
Sage Weil6f6c7002011-01-17 20:34:08 -0800912/*
913 * Resubmit requests pending on the given osd.
914 */
915static void __kick_osd_requests(struct ceph_osd_client *osdc,
916 struct ceph_osd *osd)
917{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700918 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500919 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400920 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800921 int err;
922
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400923 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800924 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600925 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800926 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400927
Alex Eldere02493c2013-03-25 18:16:11 -0500928 /*
929 * Build up a list of requests to resend by traversing the
930 * osd's list of requests. Requests for a given object are
931 * sent in tid order, and that is also the order they're
932 * kept on this list. Therefore all requests that are in
933 * flight will be found first, followed by all requests that
934 * have not yet been sent. And to resend requests while
935 * preserving this order we will want to put any sent
936 * requests back on the front of the osd client's unsent
937 * list.
938 *
939 * So we build a separate ordered list of already-sent
940 * requests for the affected osd and splice it onto the
941 * front of the osd client's unsent list. Once we've seen a
942 * request that has not yet been sent we're done. Those
943 * requests are already sitting right where they belong.
944 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800945 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500946 if (!req->r_sent)
947 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400948
949 if (!req->r_linger) {
950 dout("%s requeueing %p tid %llu\n", __func__, req,
951 req->r_tid);
952 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700953 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400954 } else {
955 list_move_tail(&req->r_req_lru_item, &resend_linger);
956 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700957 }
Alex Eldere02493c2013-03-25 18:16:11 -0500958 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700959
Alex Eldere02493c2013-03-25 18:16:11 -0500960 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400961 * Both registered and not yet registered linger requests are
962 * enqueued with a new tid on the same OSD. We add/move them
963 * to req_unsent/o_requests at the end to keep things in tid
964 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500965 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700966 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400967 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400968 WARN_ON(!list_empty(&req->r_req_lru_item));
969 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800970 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400971
972 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
973 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800974}
975
Sage Weilf24e9982009-10-06 11:31:10 -0700976/*
Sage Weil81b024e2009-10-09 10:29:18 -0700977 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700978 */
979static void osd_reset(struct ceph_connection *con)
980{
981 struct ceph_osd *osd = con->private;
982 struct ceph_osd_client *osdc;
983
984 if (!osd)
985 return;
986 dout("osd_reset osd%d\n", osd->o_osd);
987 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700988 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800989 mutex_lock(&osdc->request_mutex);
990 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600991 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800992 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700993 up_read(&osdc->map_sem);
994}
995
996/*
997 * Track open sessions with osds.
998 */
Alex Eldere10006f2012-05-26 23:26:43 -0500999static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001000{
1001 struct ceph_osd *osd;
1002
1003 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1004 if (!osd)
1005 return NULL;
1006
1007 atomic_set(&osd->o_ref, 1);
1008 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001009 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001010 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001011 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001012 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001013 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001014 osd->o_incarnation = 1;
1015
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001016 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001017
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001018 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 return osd;
1020}
1021
1022static struct ceph_osd *get_osd(struct ceph_osd *osd)
1023{
1024 if (atomic_inc_not_zero(&osd->o_ref)) {
1025 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1026 atomic_read(&osd->o_ref));
1027 return osd;
1028 } else {
1029 dout("get_osd %p FAIL\n", osd);
1030 return NULL;
1031 }
1032}
1033
1034static void put_osd(struct ceph_osd *osd)
1035{
1036 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1037 atomic_read(&osd->o_ref) - 1);
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001038 if (atomic_dec_and_test(&osd->o_ref)) {
Sage Weil79494d12010-05-27 14:15:49 -07001039 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1040
Ilya Dryomovb28ec2f2015-02-16 11:49:42 +03001041 if (osd->o_auth.authorizer)
1042 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001043 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001044 }
Sage Weilf24e9982009-10-06 11:31:10 -07001045}
1046
1047/*
1048 * remove an osd from our map
1049 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001050static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001051{
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001052 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001053 WARN_ON(!list_empty(&osd->o_requests));
1054 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001055
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001056 list_del_init(&osd->o_osd_lru);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001057 rb_erase(&osd->o_node, &osdc->osds);
1058 RB_CLEAR_NODE(&osd->o_node);
1059}
1060
1061static void remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
1062{
1063 dout("%s %p osd%d\n", __func__, osd, osd->o_osd);
1064
1065 if (!RB_EMPTY_NODE(&osd->o_node)) {
1066 ceph_con_close(&osd->o_con);
1067 __remove_osd(osdc, osd);
1068 put_osd(osd);
1069 }
Sage Weilf24e9982009-10-06 11:31:10 -07001070}
1071
Sage Weilaca420b2011-08-31 14:45:53 -07001072static void remove_all_osds(struct ceph_osd_client *osdc)
1073{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001074 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001075 mutex_lock(&osdc->request_mutex);
1076 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1077 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1078 struct ceph_osd, o_node);
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001079 remove_osd(osdc, osd);
Sage Weilaca420b2011-08-31 14:45:53 -07001080 }
1081 mutex_unlock(&osdc->request_mutex);
1082}
1083
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001084static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1085 struct ceph_osd *osd)
1086{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001087 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001088 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001089
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001090 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001091 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001092}
1093
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001094static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1095 struct ceph_osd *osd)
1096{
1097 dout("%s %p\n", __func__, osd);
1098
1099 if (list_empty(&osd->o_requests) &&
1100 list_empty(&osd->o_linger_requests))
1101 __move_osd_to_lru(osdc, osd);
1102}
1103
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104static void __remove_osd_from_lru(struct ceph_osd *osd)
1105{
1106 dout("__remove_osd_from_lru %p\n", osd);
1107 if (!list_empty(&osd->o_osd_lru))
1108 list_del_init(&osd->o_osd_lru);
1109}
1110
Sage Weilaca420b2011-08-31 14:45:53 -07001111static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001112{
1113 struct ceph_osd *osd, *nosd;
1114
1115 dout("__remove_old_osds %p\n", osdc);
1116 mutex_lock(&osdc->request_mutex);
1117 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001118 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001119 break;
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001120 remove_osd(osdc, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001121 }
1122 mutex_unlock(&osdc->request_mutex);
1123}
1124
Sage Weilf24e9982009-10-06 11:31:10 -07001125/*
1126 * reset osd connect
1127 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001128static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001129{
Alex Elderc3acb182012-12-07 09:57:58 -06001130 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001131
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001132 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001133 if (list_empty(&osd->o_requests) &&
1134 list_empty(&osd->o_linger_requests)) {
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001135 remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001136 return -ENODEV;
1137 }
1138
1139 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1140 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1141 !ceph_con_opened(&osd->o_con)) {
1142 struct ceph_osd_request *req;
1143
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001144 dout("osd addr hasn't changed and connection never opened, "
1145 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001146 /* touch each r_stamp for handle_timeout()'s benfit */
1147 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1148 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001149
1150 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001151 }
Alex Elderc3acb182012-12-07 09:57:58 -06001152
1153 ceph_con_close(&osd->o_con);
1154 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1155 osd->o_incarnation++;
1156
1157 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001158}
1159
1160static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1161{
1162 struct rb_node **p = &osdc->osds.rb_node;
1163 struct rb_node *parent = NULL;
1164 struct ceph_osd *osd = NULL;
1165
Sage Weilaca420b2011-08-31 14:45:53 -07001166 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001167 while (*p) {
1168 parent = *p;
1169 osd = rb_entry(parent, struct ceph_osd, o_node);
1170 if (new->o_osd < osd->o_osd)
1171 p = &(*p)->rb_left;
1172 else if (new->o_osd > osd->o_osd)
1173 p = &(*p)->rb_right;
1174 else
1175 BUG();
1176 }
1177
1178 rb_link_node(&new->o_node, parent, p);
1179 rb_insert_color(&new->o_node, &osdc->osds);
1180}
1181
1182static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1183{
1184 struct ceph_osd *osd;
1185 struct rb_node *n = osdc->osds.rb_node;
1186
1187 while (n) {
1188 osd = rb_entry(n, struct ceph_osd, o_node);
1189 if (o < osd->o_osd)
1190 n = n->rb_left;
1191 else if (o > osd->o_osd)
1192 n = n->rb_right;
1193 else
1194 return osd;
1195 }
1196 return NULL;
1197}
1198
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001199static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1200{
1201 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001202 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001203}
1204
1205static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1206{
1207 cancel_delayed_work(&osdc->timeout_work);
1208}
Sage Weilf24e9982009-10-06 11:31:10 -07001209
1210/*
1211 * Register request, assign tid. If this is the first request, set up
1212 * the timeout event.
1213 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001214static void __register_request(struct ceph_osd_client *osdc,
1215 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001216{
Sage Weilf24e9982009-10-06 11:31:10 -07001217 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001218 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001219 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001220 __insert_request(osdc, req);
1221 ceph_osdc_get_request(req);
1222 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001223 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001224 dout(" first request, scheduling timeout\n");
1225 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001226 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001227}
1228
Sage Weilf24e9982009-10-06 11:31:10 -07001229/*
1230 * called under osdc->request_mutex
1231 */
1232static void __unregister_request(struct ceph_osd_client *osdc,
1233 struct ceph_osd_request *req)
1234{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001235 if (RB_EMPTY_NODE(&req->r_node)) {
1236 dout("__unregister_request %p tid %lld not registered\n",
1237 req, req->r_tid);
1238 return;
1239 }
1240
Sage Weilf24e9982009-10-06 11:31:10 -07001241 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1242 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001243 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001244 osdc->num_requests--;
1245
Sage Weil0ba64782009-10-08 16:57:16 -07001246 if (req->r_osd) {
1247 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001248 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001249
1250 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001251 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001252 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001253 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001254 }
Sage Weilf24e9982009-10-06 11:31:10 -07001255
Alex Elder7d5f2482012-11-29 08:37:03 -06001256 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001257 ceph_osdc_put_request(req);
1258
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001259 if (osdc->num_requests == 0) {
1260 dout(" no requests, canceling timeout\n");
1261 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001262 }
1263}
1264
1265/*
1266 * Cancel a previously queued request message
1267 */
1268static void __cancel_request(struct ceph_osd_request *req)
1269{
Sage Weil6bc18872010-09-27 10:18:52 -07001270 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001271 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001272 req->r_sent = 0;
1273 }
1274}
1275
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001276static void __register_linger_request(struct ceph_osd_client *osdc,
1277 struct ceph_osd_request *req)
1278{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001279 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1280 WARN_ON(!req->r_linger);
1281
Alex Elder96e4dac2013-05-22 20:54:25 -05001282 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001283 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001284 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001285 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001286 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001287}
1288
1289static void __unregister_linger_request(struct ceph_osd_client *osdc,
1290 struct ceph_osd_request *req)
1291{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001292 WARN_ON(!req->r_linger);
1293
1294 if (list_empty(&req->r_linger_item)) {
1295 dout("%s %p tid %llu not registered\n", __func__, req,
1296 req->r_tid);
1297 return;
1298 }
1299
1300 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001301 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001302
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001303 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001304 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001305 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001306 if (list_empty(&req->r_osd_item))
1307 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001308 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001309 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001310}
1311
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001312void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1313 struct ceph_osd_request *req)
1314{
1315 if (!req->r_linger) {
1316 dout("set_request_linger %p\n", req);
1317 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001318 }
1319}
1320EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1321
Sage Weilf24e9982009-10-06 11:31:10 -07001322/*
Josh Durgind29adb32013-12-02 19:11:48 -08001323 * Returns whether a request should be blocked from being sent
1324 * based on the current osdmap and osd_client settings.
1325 *
1326 * Caller should hold map_sem for read.
1327 */
1328static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1329 struct ceph_osd_request *req)
1330{
1331 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1332 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1333 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1334 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1335 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1336}
1337
1338/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001339 * Calculate mapping of a request to a PG. Takes tiering into account.
1340 */
1341static int __calc_request_pg(struct ceph_osdmap *osdmap,
1342 struct ceph_osd_request *req,
1343 struct ceph_pg *pg_out)
1344{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001345 bool need_check_tiering;
1346
1347 need_check_tiering = false;
1348 if (req->r_target_oloc.pool == -1) {
1349 req->r_target_oloc = req->r_base_oloc; /* struct */
1350 need_check_tiering = true;
1351 }
1352 if (req->r_target_oid.name_len == 0) {
1353 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1354 need_check_tiering = true;
1355 }
1356
1357 if (need_check_tiering &&
1358 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001359 struct ceph_pg_pool_info *pi;
1360
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001361 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001362 if (pi) {
1363 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1364 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001365 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001366 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1367 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001368 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001369 }
1370 /* !pi is caught in ceph_oloc_oid_to_pg() */
1371 }
1372
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001373 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1374 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001375}
1376
Ilya Dryomovf671b582014-09-02 13:40:33 +04001377static void __enqueue_request(struct ceph_osd_request *req)
1378{
1379 struct ceph_osd_client *osdc = req->r_osdc;
1380
1381 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1382 req->r_osd ? req->r_osd->o_osd : -1);
1383
1384 if (req->r_osd) {
1385 __remove_osd_from_lru(req->r_osd);
1386 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1387 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1388 } else {
1389 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1390 }
1391}
1392
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001393/*
Sage Weilf24e9982009-10-06 11:31:10 -07001394 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1395 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001396 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001397 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001398 *
1399 * Return 0 if unchanged, 1 if changed, or negative on error.
1400 *
1401 * Caller should hold map_sem for read and request_mutex.
1402 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001403static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001404 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001405{
Sage Weil5b191d92013-02-23 10:38:16 -08001406 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001407 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001408 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001409 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001410 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001411
Sage Weil6f6c7002011-01-17 20:34:08 -08001412 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001413
1414 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001415 if (err) {
1416 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001417 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001418 }
Sage Weil7740a422010-01-08 15:58:25 -08001419 req->r_pgid = pgid;
1420
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001421 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1422 if (num < 0)
1423 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001424
Josh Durgind29adb32013-12-02 19:11:48 -08001425 was_paused = req->r_paused;
1426 req->r_paused = __req_should_be_paused(osdc, req);
1427 if (was_paused && !req->r_paused)
1428 force_resend = 1;
1429
Sage Weil38d64532011-10-14 13:33:55 -07001430 if ((!force_resend &&
1431 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001432 req->r_sent >= req->r_osd->o_incarnation &&
1433 req->r_num_pg_osds == num &&
1434 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001435 (req->r_osd == NULL && o == -1) ||
1436 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001437 return 0; /* no change */
1438
Sage Weil5b191d92013-02-23 10:38:16 -08001439 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1440 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001441 req->r_osd ? req->r_osd->o_osd : -1);
1442
Sage Weild85b7052010-05-10 10:24:48 -07001443 /* record full pg acting set */
1444 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1445 req->r_num_pg_osds = num;
1446
Sage Weilf24e9982009-10-06 11:31:10 -07001447 if (req->r_osd) {
1448 __cancel_request(req);
1449 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001450 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001451 req->r_osd = NULL;
1452 }
1453
1454 req->r_osd = __lookup_osd(osdc, o);
1455 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001456 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001457 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001458 if (!req->r_osd) {
1459 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001460 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001461 }
Sage Weilf24e9982009-10-06 11:31:10 -07001462
Sage Weil6f6c7002011-01-17 20:34:08 -08001463 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001464 __insert_osd(osdc, req->r_osd);
1465
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001466 ceph_con_open(&req->r_osd->o_con,
1467 CEPH_ENTITY_TYPE_OSD, o,
1468 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001469 }
1470
Ilya Dryomovf671b582014-09-02 13:40:33 +04001471 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001472 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001473
1474out:
Sage Weilf24e9982009-10-06 11:31:10 -07001475 return err;
1476}
1477
1478/*
1479 * caller should hold map_sem (for read) and request_mutex
1480 */
Sage Weil56e925b2012-01-03 12:34:34 -08001481static void __send_request(struct ceph_osd_client *osdc,
1482 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001483{
Sage Weil1b83bef2013-02-25 16:11:12 -08001484 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001485
Sage Weil1b83bef2013-02-25 16:11:12 -08001486 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1487 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1488 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001489
Sage Weil1b83bef2013-02-25 16:11:12 -08001490 /* fill in message content that changes each time we send it */
1491 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1492 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001493 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001494 p = req->r_request_pgid;
1495 ceph_encode_64(&p, req->r_pgid.pool);
1496 ceph_encode_32(&p, req->r_pgid.seed);
1497 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1498 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1499 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001500
Sage Weil3dd72fc2010-03-22 14:42:30 -07001501 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001502 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001503
1504 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001505
Sage Weilf24e9982009-10-06 11:31:10 -07001506 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001507
1508 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001509}
1510
1511/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001512 * Send any requests in the queue (req_unsent).
1513 */
Alex Elderf9d25192013-02-15 11:42:29 -06001514static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001515{
1516 struct ceph_osd_request *req, *tmp;
1517
Alex Elderf9d25192013-02-15 11:42:29 -06001518 dout("__send_queued\n");
1519 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001520 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001521}
1522
1523/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001524 * Caller should hold map_sem for read and request_mutex.
1525 */
1526static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1527 struct ceph_osd_request *req,
1528 bool nofail)
1529{
1530 int rc;
1531
1532 __register_request(osdc, req);
1533 req->r_sent = 0;
1534 req->r_got_reply = 0;
1535 rc = __map_request(osdc, req, 0);
1536 if (rc < 0) {
1537 if (nofail) {
1538 dout("osdc_start_request failed map, "
1539 " will retry %lld\n", req->r_tid);
1540 rc = 0;
1541 } else {
1542 __unregister_request(osdc, req);
1543 }
1544 return rc;
1545 }
1546
1547 if (req->r_osd == NULL) {
1548 dout("send_request %p no up osds in pg\n", req);
1549 ceph_monc_request_next_osdmap(&osdc->client->monc);
1550 } else {
1551 __send_queued(osdc);
1552 }
1553
1554 return 0;
1555}
1556
1557/*
Sage Weilf24e9982009-10-06 11:31:10 -07001558 * Timeout callback, called every N seconds when 1 or more osd
1559 * requests has been active for more than N seconds. When this
1560 * happens, we ping all OSDs with requests who have timed out to
1561 * ensure any communications channel reset is detected. Reset the
1562 * request timeouts another N seconds in the future as we go.
1563 * Reschedule the timeout event another N seconds in future (unless
1564 * there are no open requests).
1565 */
1566static void handle_timeout(struct work_struct *work)
1567{
1568 struct ceph_osd_client *osdc =
1569 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001570 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001571 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001572 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001573 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001574 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001575 dout("timeout\n");
1576 down_read(&osdc->map_sem);
1577
1578 ceph_monc_request_next_osdmap(&osdc->client->monc);
1579
1580 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001581
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001582 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001583 * ping osds that are a bit slow. this ensures that if there
1584 * is a break in the TCP connection we will notice, and reopen
1585 * a connection with that osd (from the fault callback).
1586 */
1587 INIT_LIST_HEAD(&slow_osds);
1588 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001589 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001590 break;
1591
1592 osd = req->r_osd;
1593 BUG_ON(!osd);
1594 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001595 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001596 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1597 }
1598 while (!list_empty(&slow_osds)) {
1599 osd = list_entry(slow_osds.next, struct ceph_osd,
1600 o_keepalive_item);
1601 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001602 ceph_con_keepalive(&osd->o_con);
1603 }
1604
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001605 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001606 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001607 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001608 up_read(&osdc->map_sem);
1609}
1610
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001611static void handle_osds_timeout(struct work_struct *work)
1612{
1613 struct ceph_osd_client *osdc =
1614 container_of(work, struct ceph_osd_client,
1615 osds_timeout_work.work);
1616 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001617 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001618
1619 dout("osds timeout\n");
1620 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001621 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001622 up_read(&osdc->map_sem);
1623
1624 schedule_delayed_work(&osdc->osds_timeout_work,
1625 round_jiffies_relative(delay));
1626}
1627
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001628static int ceph_oloc_decode(void **p, void *end,
1629 struct ceph_object_locator *oloc)
1630{
1631 u8 struct_v, struct_cv;
1632 u32 len;
1633 void *struct_end;
1634 int ret = 0;
1635
1636 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1637 struct_v = ceph_decode_8(p);
1638 struct_cv = ceph_decode_8(p);
1639 if (struct_v < 3) {
1640 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1641 struct_v, struct_cv);
1642 goto e_inval;
1643 }
1644 if (struct_cv > 6) {
1645 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1646 struct_v, struct_cv);
1647 goto e_inval;
1648 }
1649 len = ceph_decode_32(p);
1650 ceph_decode_need(p, end, len, e_inval);
1651 struct_end = *p + len;
1652
1653 oloc->pool = ceph_decode_64(p);
1654 *p += 4; /* skip preferred */
1655
1656 len = ceph_decode_32(p);
1657 if (len > 0) {
1658 pr_warn("ceph_object_locator::key is set\n");
1659 goto e_inval;
1660 }
1661
1662 if (struct_v >= 5) {
1663 len = ceph_decode_32(p);
1664 if (len > 0) {
1665 pr_warn("ceph_object_locator::nspace is set\n");
1666 goto e_inval;
1667 }
1668 }
1669
1670 if (struct_v >= 6) {
1671 s64 hash = ceph_decode_64(p);
1672 if (hash != -1) {
1673 pr_warn("ceph_object_locator::hash is set\n");
1674 goto e_inval;
1675 }
1676 }
1677
1678 /* skip the rest */
1679 *p = struct_end;
1680out:
1681 return ret;
1682
1683e_inval:
1684 ret = -EINVAL;
1685 goto out;
1686}
1687
1688static int ceph_redirect_decode(void **p, void *end,
1689 struct ceph_request_redirect *redir)
1690{
1691 u8 struct_v, struct_cv;
1692 u32 len;
1693 void *struct_end;
1694 int ret;
1695
1696 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1697 struct_v = ceph_decode_8(p);
1698 struct_cv = ceph_decode_8(p);
1699 if (struct_cv > 1) {
1700 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1701 struct_v, struct_cv);
1702 goto e_inval;
1703 }
1704 len = ceph_decode_32(p);
1705 ceph_decode_need(p, end, len, e_inval);
1706 struct_end = *p + len;
1707
1708 ret = ceph_oloc_decode(p, end, &redir->oloc);
1709 if (ret)
1710 goto out;
1711
1712 len = ceph_decode_32(p);
1713 if (len > 0) {
1714 pr_warn("ceph_request_redirect::object_name is set\n");
1715 goto e_inval;
1716 }
1717
1718 len = ceph_decode_32(p);
1719 *p += len; /* skip osd_instructions */
1720
1721 /* skip the rest */
1722 *p = struct_end;
1723out:
1724 return ret;
1725
1726e_inval:
1727 ret = -EINVAL;
1728 goto out;
1729}
1730
Sage Weil25845472011-06-03 09:37:09 -07001731static void complete_request(struct ceph_osd_request *req)
1732{
Sage Weil25845472011-06-03 09:37:09 -07001733 complete_all(&req->r_safe_completion); /* fsync waiter */
1734}
1735
Sage Weilf24e9982009-10-06 11:31:10 -07001736/*
1737 * handle osd op reply. either call the callback if it is specified,
1738 * or do the completion to wake up the waiting thread.
1739 */
Sage Weil350b1c32009-12-22 10:45:45 -08001740static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1741 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001742{
Sage Weil1b83bef2013-02-25 16:11:12 -08001743 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001744 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001745 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001746 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001747 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001748 unsigned int numops;
1749 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001750 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001751 s32 retry_attempt;
1752 struct ceph_pg pg;
1753 int err;
1754 u32 reassert_epoch;
1755 u64 reassert_version;
1756 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001757 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001758 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001759 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001760
Sage Weil6df058c2009-12-22 11:24:33 -08001761 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001762 dout("handle_reply %p tid %llu\n", msg, tid);
1763
1764 p = msg->front.iov_base;
1765 end = p + msg->front.iov_len;
1766
1767 ceph_decode_need(&p, end, 4, bad);
1768 object_len = ceph_decode_32(&p);
1769 ceph_decode_need(&p, end, object_len, bad);
1770 p += object_len;
1771
Alex Elderef4859d2013-04-01 18:58:26 -05001772 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001773 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001774 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001775
1776 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1777 flags = ceph_decode_64(&p);
1778 result = ceph_decode_32(&p);
1779 reassert_epoch = ceph_decode_32(&p);
1780 reassert_version = ceph_decode_64(&p);
1781 osdmap_epoch = ceph_decode_32(&p);
1782
Sage Weilf24e9982009-10-06 11:31:10 -07001783 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001784 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001785 mutex_lock(&osdc->request_mutex);
1786 req = __lookup_request(osdc, tid);
1787 if (req == NULL) {
1788 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001789 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001790 }
1791 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001792
1793 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1794 req, result);
1795
Dan Carpenter18741192013-08-15 08:51:58 +03001796 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001797 numops = ceph_decode_32(&p);
1798 if (numops > CEPH_OSD_MAX_OP)
1799 goto bad_put;
1800 if (numops != req->r_num_ops)
1801 goto bad_put;
1802 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001803 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001804 for (i = 0; i < numops; i++) {
1805 struct ceph_osd_op *op = p;
1806 int len;
1807
1808 len = le32_to_cpu(op->payload_len);
1809 req->r_reply_op_len[i] = len;
1810 dout(" op %d has %d bytes\n", i, len);
1811 payload_len += len;
1812 p += sizeof(*op);
1813 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001814 bytes = le32_to_cpu(msg->hdr.data_len);
1815 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001816 pr_warn("sum of op payload lens %d != data_len %d\n",
1817 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001818 goto bad_put;
1819 }
1820
Dan Carpenter18741192013-08-15 08:51:58 +03001821 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001822 retry_attempt = ceph_decode_32(&p);
1823 for (i = 0; i < numops; i++)
1824 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001825
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001826 if (le16_to_cpu(msg->hdr.version) >= 6) {
1827 p += 8 + 4; /* skip replay_version */
1828 p += 8; /* skip user_version */
1829
1830 err = ceph_redirect_decode(&p, end, &redir);
1831 if (err)
1832 goto bad_put;
1833 } else {
1834 redir.oloc.pool = -1;
1835 }
1836
1837 if (redir.oloc.pool != -1) {
1838 dout("redirect pool %lld\n", redir.oloc.pool);
1839
1840 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001841
1842 req->r_target_oloc = redir.oloc; /* struct */
1843
1844 /*
1845 * Start redirect requests with nofail=true. If
1846 * mapping fails, request will end up on the notarget
1847 * list, waiting for the new osdmap (which can take
1848 * a while), even though the original request mapped
1849 * successfully. In the future we might want to follow
1850 * original request's nofail setting here.
1851 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001852 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001853 BUG_ON(err);
1854
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001855 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001856 }
1857
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001858 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001859 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001860 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001861 dout("handle_reply result %d bytes %d\n", req->r_result,
1862 bytes);
1863 if (req->r_result == 0)
1864 req->r_result = bytes;
1865
1866 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001867 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1868 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001869
1870 req->r_got_reply = 1;
1871 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1872 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001873 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001874 }
1875
1876 dout("handle_reply tid %llu flags %d\n", tid, flags);
1877
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001878 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1879 __register_linger_request(osdc, req);
1880
Sage Weilf24e9982009-10-06 11:31:10 -07001881 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001882 if (result < 0 ||
1883 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001884 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1885 __unregister_request(osdc, req);
1886
1887 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001888 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001889
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001890 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001891 if (req->r_unsafe_callback &&
1892 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1893 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001894 if (req->r_callback)
1895 req->r_callback(req, msg);
1896 else
1897 complete_all(&req->r_completion);
1898 }
Sage Weilf24e9982009-10-06 11:31:10 -07001899
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001900 if (flags & CEPH_OSD_FLAG_ONDISK) {
1901 if (req->r_unsafe_callback && already_completed)
1902 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001903 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001904 }
Sage Weilf24e9982009-10-06 11:31:10 -07001905
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001906out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001907 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001908 ceph_osdc_put_request(req);
1909 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001910out_unlock:
1911 mutex_unlock(&osdc->request_mutex);
1912 up_read(&osdc->map_sem);
1913 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001914
Sage Weil1b83bef2013-02-25 16:11:12 -08001915bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001916 req->r_result = -EIO;
1917 __unregister_request(osdc, req);
1918 if (req->r_callback)
1919 req->r_callback(req, msg);
1920 else
1921 complete_all(&req->r_completion);
1922 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001923 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001924bad_mutex:
1925 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001926 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001927bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001928 pr_err("corrupt osd_op_reply got %d %d\n",
1929 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001930 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001931}
1932
Sage Weil6f6c7002011-01-17 20:34:08 -08001933static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001934{
Sage Weilf24e9982009-10-06 11:31:10 -07001935 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001936
Ilya Dryomov7eb71e02015-02-17 19:37:15 +03001937 dout("%s %p\n", __func__, osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -08001938 for (p = rb_first(&osdc->osds); p; p = n) {
1939 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001940
Sage Weil6f6c7002011-01-17 20:34:08 -08001941 n = rb_next(p);
1942 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1943 memcmp(&osd->o_con.peer_addr,
1944 ceph_osd_addr(osdc->osdmap,
1945 osd->o_osd),
1946 sizeof(struct ceph_entity_addr)) != 0)
1947 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001948 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001949}
1950
1951/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001952 * Requeue requests whose mapping to an OSD has changed. If requests map to
1953 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001954 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001955 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001956 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001957static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1958 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001959{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001960 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001961 struct rb_node *p;
1962 int needmap = 0;
1963 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001964 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001965
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001966 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1967 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001968 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001969 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001970 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001971 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001972
1973 /*
1974 * For linger requests that have not yet been
1975 * registered, move them to the linger list; they'll
1976 * be sent to the osd in the loop below. Unregister
1977 * the request before re-registering it as a linger
1978 * request to ensure the __map_request() below
1979 * will decide it needs to be sent.
1980 */
1981 if (req->r_linger && list_empty(&req->r_linger_item)) {
1982 dout("%p tid %llu restart on osd%d\n",
1983 req, req->r_tid,
1984 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001985 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001986 __unregister_request(osdc, req);
1987 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001988 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001989 continue;
1990 }
1991
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001992 force_resend_req = force_resend ||
1993 (force_resend_writes &&
1994 req->r_flags & CEPH_OSD_FLAG_WRITE);
1995 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001996 if (err < 0)
1997 continue; /* error */
1998 if (req->r_osd == NULL) {
1999 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
2000 needmap++; /* request a newer map */
2001 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07002002 if (!req->r_linger) {
2003 dout("%p tid %llu requeued on osd%d\n", req,
2004 req->r_tid,
2005 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002006 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002007 }
2008 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002009 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002010
2011 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2012 r_linger_item) {
2013 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2014
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002015 err = __map_request(osdc, req,
2016 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002017 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002018 if (err < 0)
2019 continue; /* hrm! */
Ilya Dryomovb0494532015-05-11 17:53:10 +03002020 if (req->r_osd == NULL || err > 0) {
2021 if (req->r_osd == NULL) {
2022 dout("lingering %p tid %llu maps to no osd\n",
2023 req, req->r_tid);
2024 /*
2025 * A homeless lingering request makes
2026 * no sense, as it's job is to keep
2027 * a particular OSD connection open.
2028 * Request a newer map and kick the
2029 * request, knowing that it won't be
2030 * resent until we actually get a map
2031 * that can tell us where to send it.
2032 */
2033 needmap++;
2034 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002035
Ilya Dryomovb0494532015-05-11 17:53:10 +03002036 dout("kicking lingering %p tid %llu osd%d\n", req,
2037 req->r_tid, req->r_osd ? req->r_osd->o_osd : -1);
2038 __register_request(osdc, req);
2039 __unregister_linger_request(osdc, req);
2040 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002041 }
Alex Elder14d2f382013-05-15 16:28:33 -05002042 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002043 mutex_unlock(&osdc->request_mutex);
2044
2045 if (needmap) {
2046 dout("%d requests for down osds, need new map\n", needmap);
2047 ceph_monc_request_next_osdmap(&osdc->client->monc);
2048 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002049}
Sage Weil6f6c7002011-01-17 20:34:08 -08002050
2051
Sage Weilf24e9982009-10-06 11:31:10 -07002052/*
2053 * Process updated osd map.
2054 *
2055 * The message contains any number of incremental and full maps, normally
2056 * indicating some sort of topology change in the cluster. Kick requests
2057 * off to different OSDs as needed.
2058 */
2059void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2060{
2061 void *p, *end, *next;
2062 u32 nr_maps, maplen;
2063 u32 epoch;
2064 struct ceph_osdmap *newmap = NULL, *oldmap;
2065 int err;
2066 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002067 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002068
2069 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2070 p = msg->front.iov_base;
2071 end = p + msg->front.iov_len;
2072
2073 /* verify fsid */
2074 ceph_decode_need(&p, end, sizeof(fsid), bad);
2075 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002076 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2077 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002078
2079 down_write(&osdc->map_sem);
2080
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002081 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2082
Sage Weilf24e9982009-10-06 11:31:10 -07002083 /* incremental maps */
2084 ceph_decode_32_safe(&p, end, nr_maps, bad);
2085 dout(" %d inc maps\n", nr_maps);
2086 while (nr_maps > 0) {
2087 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002088 epoch = ceph_decode_32(&p);
2089 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002090 ceph_decode_need(&p, end, maplen, bad);
2091 next = p + maplen;
2092 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2093 dout("applying incremental map %u len %d\n",
2094 epoch, maplen);
2095 newmap = osdmap_apply_incremental(&p, next,
2096 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002097 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002098 if (IS_ERR(newmap)) {
2099 err = PTR_ERR(newmap);
2100 goto bad;
2101 }
Sage Weil30dc6382009-12-21 14:49:37 -08002102 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002103 if (newmap != osdc->osdmap) {
2104 ceph_osdmap_destroy(osdc->osdmap);
2105 osdc->osdmap = newmap;
2106 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002107 was_full = was_full ||
2108 ceph_osdmap_flag(osdc->osdmap,
2109 CEPH_OSDMAP_FULL);
2110 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002111 } else {
2112 dout("ignoring incremental map %u len %d\n",
2113 epoch, maplen);
2114 }
2115 p = next;
2116 nr_maps--;
2117 }
2118 if (newmap)
2119 goto done;
2120
2121 /* full maps */
2122 ceph_decode_32_safe(&p, end, nr_maps, bad);
2123 dout(" %d full maps\n", nr_maps);
2124 while (nr_maps) {
2125 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002126 epoch = ceph_decode_32(&p);
2127 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002128 ceph_decode_need(&p, end, maplen, bad);
2129 if (nr_maps > 1) {
2130 dout("skipping non-latest full map %u len %d\n",
2131 epoch, maplen);
2132 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2133 dout("skipping full map %u len %d, "
2134 "older than our %u\n", epoch, maplen,
2135 osdc->osdmap->epoch);
2136 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002137 int skipped_map = 0;
2138
Sage Weilf24e9982009-10-06 11:31:10 -07002139 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002140 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002141 if (IS_ERR(newmap)) {
2142 err = PTR_ERR(newmap);
2143 goto bad;
2144 }
Sage Weil30dc6382009-12-21 14:49:37 -08002145 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002146 oldmap = osdc->osdmap;
2147 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002148 if (oldmap) {
2149 if (oldmap->epoch + 1 < newmap->epoch)
2150 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002151 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002152 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002153 was_full = was_full ||
2154 ceph_osdmap_flag(osdc->osdmap,
2155 CEPH_OSDMAP_FULL);
2156 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002157 }
2158 p += maplen;
2159 nr_maps--;
2160 }
2161
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002162 if (!osdc->osdmap)
2163 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002164done:
2165 downgrade_write(&osdc->map_sem);
2166 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002167
2168 /*
2169 * subscribe to subsequent osdmap updates if full to ensure
2170 * we find out when we are no longer full and stop returning
2171 * ENOSPC.
2172 */
Josh Durgind29adb32013-12-02 19:11:48 -08002173 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2174 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2175 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002176 ceph_monc_request_next_osdmap(&osdc->client->monc);
2177
Alex Elderf9d25192013-02-15 11:42:29 -06002178 mutex_lock(&osdc->request_mutex);
2179 __send_queued(osdc);
2180 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002181 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002182 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002183 return;
2184
2185bad:
2186 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002187 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002188 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002189}
2190
Sage Weilf24e9982009-10-06 11:31:10 -07002191/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002192 * watch/notify callback event infrastructure
2193 *
2194 * These callbacks are used both for watch and notify operations.
2195 */
2196static void __release_event(struct kref *kref)
2197{
2198 struct ceph_osd_event *event =
2199 container_of(kref, struct ceph_osd_event, kref);
2200
2201 dout("__release_event %p\n", event);
2202 kfree(event);
2203}
2204
2205static void get_event(struct ceph_osd_event *event)
2206{
2207 kref_get(&event->kref);
2208}
2209
2210void ceph_osdc_put_event(struct ceph_osd_event *event)
2211{
2212 kref_put(&event->kref, __release_event);
2213}
2214EXPORT_SYMBOL(ceph_osdc_put_event);
2215
2216static void __insert_event(struct ceph_osd_client *osdc,
2217 struct ceph_osd_event *new)
2218{
2219 struct rb_node **p = &osdc->event_tree.rb_node;
2220 struct rb_node *parent = NULL;
2221 struct ceph_osd_event *event = NULL;
2222
2223 while (*p) {
2224 parent = *p;
2225 event = rb_entry(parent, struct ceph_osd_event, node);
2226 if (new->cookie < event->cookie)
2227 p = &(*p)->rb_left;
2228 else if (new->cookie > event->cookie)
2229 p = &(*p)->rb_right;
2230 else
2231 BUG();
2232 }
2233
2234 rb_link_node(&new->node, parent, p);
2235 rb_insert_color(&new->node, &osdc->event_tree);
2236}
2237
2238static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2239 u64 cookie)
2240{
2241 struct rb_node **p = &osdc->event_tree.rb_node;
2242 struct rb_node *parent = NULL;
2243 struct ceph_osd_event *event = NULL;
2244
2245 while (*p) {
2246 parent = *p;
2247 event = rb_entry(parent, struct ceph_osd_event, node);
2248 if (cookie < event->cookie)
2249 p = &(*p)->rb_left;
2250 else if (cookie > event->cookie)
2251 p = &(*p)->rb_right;
2252 else
2253 return event;
2254 }
2255 return NULL;
2256}
2257
2258static void __remove_event(struct ceph_osd_event *event)
2259{
2260 struct ceph_osd_client *osdc = event->osdc;
2261
2262 if (!RB_EMPTY_NODE(&event->node)) {
2263 dout("__remove_event removed %p\n", event);
2264 rb_erase(&event->node, &osdc->event_tree);
2265 ceph_osdc_put_event(event);
2266 } else {
2267 dout("__remove_event didn't remove %p\n", event);
2268 }
2269}
2270
2271int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2272 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002273 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002274{
2275 struct ceph_osd_event *event;
2276
2277 event = kmalloc(sizeof(*event), GFP_NOIO);
2278 if (!event)
2279 return -ENOMEM;
2280
2281 dout("create_event %p\n", event);
2282 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002283 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002284 event->data = data;
2285 event->osdc = osdc;
2286 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002287 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002288 kref_init(&event->kref); /* one ref for us */
2289 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002290
2291 spin_lock(&osdc->event_lock);
2292 event->cookie = ++osdc->event_count;
2293 __insert_event(osdc, event);
2294 spin_unlock(&osdc->event_lock);
2295
2296 *pevent = event;
2297 return 0;
2298}
2299EXPORT_SYMBOL(ceph_osdc_create_event);
2300
2301void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2302{
2303 struct ceph_osd_client *osdc = event->osdc;
2304
2305 dout("cancel_event %p\n", event);
2306 spin_lock(&osdc->event_lock);
2307 __remove_event(event);
2308 spin_unlock(&osdc->event_lock);
2309 ceph_osdc_put_event(event); /* caller's */
2310}
2311EXPORT_SYMBOL(ceph_osdc_cancel_event);
2312
2313
2314static void do_event_work(struct work_struct *work)
2315{
2316 struct ceph_osd_event_work *event_work =
2317 container_of(work, struct ceph_osd_event_work, work);
2318 struct ceph_osd_event *event = event_work->event;
2319 u64 ver = event_work->ver;
2320 u64 notify_id = event_work->notify_id;
2321 u8 opcode = event_work->opcode;
2322
2323 dout("do_event_work completing %p\n", event);
2324 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002325 dout("do_event_work completed %p\n", event);
2326 ceph_osdc_put_event(event);
2327 kfree(event_work);
2328}
2329
2330
2331/*
2332 * Process osd watch notifications
2333 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002334static void handle_watch_notify(struct ceph_osd_client *osdc,
2335 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002336{
2337 void *p, *end;
2338 u8 proto_ver;
2339 u64 cookie, ver, notify_id;
2340 u8 opcode;
2341 struct ceph_osd_event *event;
2342 struct ceph_osd_event_work *event_work;
2343
2344 p = msg->front.iov_base;
2345 end = p + msg->front.iov_len;
2346
2347 ceph_decode_8_safe(&p, end, proto_ver, bad);
2348 ceph_decode_8_safe(&p, end, opcode, bad);
2349 ceph_decode_64_safe(&p, end, cookie, bad);
2350 ceph_decode_64_safe(&p, end, ver, bad);
2351 ceph_decode_64_safe(&p, end, notify_id, bad);
2352
2353 spin_lock(&osdc->event_lock);
2354 event = __find_event(osdc, cookie);
2355 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002356 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002357 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002358 }
2359 spin_unlock(&osdc->event_lock);
2360 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2361 cookie, ver, event);
2362 if (event) {
2363 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002364 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002365 pr_err("couldn't allocate event_work\n");
2366 ceph_osdc_put_event(event);
2367 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002368 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002369 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002370 event_work->event = event;
2371 event_work->ver = ver;
2372 event_work->notify_id = notify_id;
2373 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002374
2375 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002376 }
2377
2378 return;
2379
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002380bad:
2381 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002382}
2383
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002384/*
Alex Eldere65550f2013-04-05 01:27:12 -05002385 * build new request AND message
2386 *
2387 */
2388void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2389 struct ceph_snap_context *snapc, u64 snap_id,
2390 struct timespec *mtime)
2391{
2392 struct ceph_msg *msg = req->r_request;
2393 void *p;
2394 size_t msg_size;
2395 int flags = req->r_flags;
2396 u64 data_len;
2397 unsigned int i;
2398
2399 req->r_snapid = snap_id;
2400 req->r_snapc = ceph_get_snap_context(snapc);
2401
2402 /* encode request */
2403 msg->hdr.version = cpu_to_le16(4);
2404
2405 p = msg->front.iov_base;
2406 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2407 req->r_request_osdmap_epoch = p;
2408 p += 4;
2409 req->r_request_flags = p;
2410 p += 4;
2411 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2412 ceph_encode_timespec(p, mtime);
2413 p += sizeof(struct ceph_timespec);
2414 req->r_request_reassert_version = p;
2415 p += sizeof(struct ceph_eversion); /* will get filled in */
2416
2417 /* oloc */
2418 ceph_encode_8(&p, 4);
2419 ceph_encode_8(&p, 4);
2420 ceph_encode_32(&p, 8 + 4 + 4);
2421 req->r_request_pool = p;
2422 p += 8;
2423 ceph_encode_32(&p, -1); /* preferred */
2424 ceph_encode_32(&p, 0); /* key len */
2425
2426 ceph_encode_8(&p, 1);
2427 req->r_request_pgid = p;
2428 p += 8 + 4;
2429 ceph_encode_32(&p, -1); /* preferred */
2430
2431 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002432 ceph_encode_32(&p, req->r_base_oid.name_len);
2433 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2434 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2435 req->r_base_oid.name, req->r_base_oid.name_len);
2436 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002437
2438 /* ops--can imply data */
2439 ceph_encode_16(&p, (u16)req->r_num_ops);
2440 data_len = 0;
2441 for (i = 0; i < req->r_num_ops; i++) {
2442 data_len += osd_req_encode_op(req, p, i);
2443 p += sizeof(struct ceph_osd_op);
2444 }
2445
2446 /* snaps */
2447 ceph_encode_64(&p, req->r_snapid);
2448 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2449 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2450 if (req->r_snapc) {
2451 for (i = 0; i < snapc->num_snaps; i++) {
2452 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2453 }
2454 }
2455
2456 req->r_request_attempts = p;
2457 p += 4;
2458
2459 /* data */
2460 if (flags & CEPH_OSD_FLAG_WRITE) {
2461 u16 data_off;
2462
2463 /*
2464 * The header "data_off" is a hint to the receiver
2465 * allowing it to align received data into its
2466 * buffers such that there's no need to re-copy
2467 * it before writing it to disk (direct I/O).
2468 */
2469 data_off = (u16) (off & 0xffff);
2470 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2471 }
2472 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2473
2474 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2475 msg_size = p - msg->front.iov_base;
2476 msg->front.iov_len = msg_size;
2477 msg->hdr.front_len = cpu_to_le32(msg_size);
2478
2479 dout("build_request msg_size was %d\n", (int)msg_size);
2480}
2481EXPORT_SYMBOL(ceph_osdc_build_request);
2482
2483/*
Sage Weilf24e9982009-10-06 11:31:10 -07002484 * Register request, send initial attempt.
2485 */
2486int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2487 struct ceph_osd_request *req,
2488 bool nofail)
2489{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002490 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002491
Sage Weilf24e9982009-10-06 11:31:10 -07002492 down_read(&osdc->map_sem);
2493 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002494
2495 rc = __ceph_osdc_start_request(osdc, req, nofail);
2496
Sage Weilf24e9982009-10-06 11:31:10 -07002497 mutex_unlock(&osdc->request_mutex);
2498 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002499
Sage Weilf24e9982009-10-06 11:31:10 -07002500 return rc;
2501}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002502EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002503
2504/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002505 * Unregister a registered request. The request is not completed (i.e.
2506 * no callbacks or wakeups) - higher layers are supposed to know what
2507 * they are canceling.
2508 */
2509void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2510{
2511 struct ceph_osd_client *osdc = req->r_osdc;
2512
2513 mutex_lock(&osdc->request_mutex);
2514 if (req->r_linger)
2515 __unregister_linger_request(osdc, req);
2516 __unregister_request(osdc, req);
2517 mutex_unlock(&osdc->request_mutex);
2518
2519 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2520}
2521EXPORT_SYMBOL(ceph_osdc_cancel_request);
2522
2523/*
Sage Weilf24e9982009-10-06 11:31:10 -07002524 * wait for a request to complete
2525 */
2526int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2527 struct ceph_osd_request *req)
2528{
2529 int rc;
2530
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002531 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2532
Sage Weilf24e9982009-10-06 11:31:10 -07002533 rc = wait_for_completion_interruptible(&req->r_completion);
2534 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002535 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2536 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002537 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002538 return rc;
2539 }
2540
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002541 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2542 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002543 return req->r_result;
2544}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002545EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002546
2547/*
2548 * sync - wait for all in-flight requests to flush. avoid starvation.
2549 */
2550void ceph_osdc_sync(struct ceph_osd_client *osdc)
2551{
2552 struct ceph_osd_request *req;
2553 u64 last_tid, next_tid = 0;
2554
2555 mutex_lock(&osdc->request_mutex);
2556 last_tid = osdc->last_tid;
2557 while (1) {
2558 req = __lookup_request_ge(osdc, next_tid);
2559 if (!req)
2560 break;
2561 if (req->r_tid > last_tid)
2562 break;
2563
2564 next_tid = req->r_tid + 1;
2565 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2566 continue;
2567
2568 ceph_osdc_get_request(req);
2569 mutex_unlock(&osdc->request_mutex);
2570 dout("sync waiting on tid %llu (last is %llu)\n",
2571 req->r_tid, last_tid);
2572 wait_for_completion(&req->r_safe_completion);
2573 mutex_lock(&osdc->request_mutex);
2574 ceph_osdc_put_request(req);
2575 }
2576 mutex_unlock(&osdc->request_mutex);
2577 dout("sync done (thru tid %llu)\n", last_tid);
2578}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002579EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002580
2581/*
Josh Durgindd935f42013-08-28 21:43:09 -07002582 * Call all pending notify callbacks - for use after a watch is
2583 * unregistered, to make sure no more callbacks for it will be invoked
2584 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002585void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002586{
2587 flush_workqueue(osdc->notify_wq);
2588}
2589EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2590
2591
2592/*
Sage Weilf24e9982009-10-06 11:31:10 -07002593 * init, shutdown
2594 */
2595int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2596{
2597 int err;
2598
2599 dout("init\n");
2600 osdc->client = client;
2601 osdc->osdmap = NULL;
2602 init_rwsem(&osdc->map_sem);
2603 init_completion(&osdc->map_waiters);
2604 osdc->last_requested_map = 0;
2605 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002606 osdc->last_tid = 0;
2607 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002608 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002609 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002610 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002611 INIT_LIST_HEAD(&osdc->req_unsent);
2612 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002613 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002614 osdc->num_requests = 0;
2615 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002616 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002617 spin_lock_init(&osdc->event_lock);
2618 osdc->event_tree = RB_ROOT;
2619 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002620
2621 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002622 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002623
Sage Weil5f44f142009-11-18 14:52:18 -08002624 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002625 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2626 sizeof(struct ceph_osd_request));
2627 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002628 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002629
Sage Weild50b4092012-07-09 14:22:34 -07002630 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2631 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002632 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002633 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002634 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002635 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002636 OSD_OPREPLY_FRONT_LEN, 10, true,
2637 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002638 if (err < 0)
2639 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002640
Dan Carpenterdbcae082013-08-15 08:58:59 +03002641 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002642 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002643 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002644 goto out_msgpool_reply;
2645
Sage Weilf24e9982009-10-06 11:31:10 -07002646 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002647
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002648out_msgpool_reply:
2649 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002650out_msgpool:
2651 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002652out_mempool:
2653 mempool_destroy(osdc->req_mempool);
2654out:
2655 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002656}
2657
2658void ceph_osdc_stop(struct ceph_osd_client *osdc)
2659{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002660 flush_workqueue(osdc->notify_wq);
2661 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002662 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002663 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002664 if (osdc->osdmap) {
2665 ceph_osdmap_destroy(osdc->osdmap);
2666 osdc->osdmap = NULL;
2667 }
Sage Weilaca420b2011-08-31 14:45:53 -07002668 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002669 mempool_destroy(osdc->req_mempool);
2670 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002671 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002672}
2673
2674/*
2675 * Read some contiguous pages. If we cross a stripe boundary, shorten
2676 * *plen. Return number of bytes read, or error.
2677 */
2678int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2679 struct ceph_vino vino, struct ceph_file_layout *layout,
2680 u64 off, u64 *plen,
2681 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002682 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002683{
2684 struct ceph_osd_request *req;
2685 int rc = 0;
2686
2687 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2688 vino.snap, off, *plen);
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002689 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002690 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002691 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002692 false);
Sage Weil68162822012-09-24 21:01:02 -07002693 if (IS_ERR(req))
2694 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002695
2696 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002697
Alex Elder406e2c92013-04-15 14:50:36 -05002698 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002699 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002700
Alex Eldere0c59482013-03-07 15:38:25 -06002701 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002702 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002703
Alex Elder79528732013-04-03 21:32:51 -05002704 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002705
Sage Weilf24e9982009-10-06 11:31:10 -07002706 rc = ceph_osdc_start_request(osdc, req, false);
2707 if (!rc)
2708 rc = ceph_osdc_wait_request(osdc, req);
2709
2710 ceph_osdc_put_request(req);
2711 dout("readpages result %d\n", rc);
2712 return rc;
2713}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002714EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002715
2716/*
2717 * do a synchronous write on N pages
2718 */
2719int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2720 struct ceph_file_layout *layout,
2721 struct ceph_snap_context *snapc,
2722 u64 off, u64 len,
2723 u32 truncate_seq, u64 truncate_size,
2724 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002725 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002726{
2727 struct ceph_osd_request *req;
2728 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002729 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002730
Alex Elderacead002013-03-14 14:09:05 -05002731 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Yan, Zheng715e4cd2014-11-13 14:40:37 +08002732 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 0, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002733 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002734 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002735 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002736 true);
Sage Weil68162822012-09-24 21:01:02 -07002737 if (IS_ERR(req))
2738 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002739
2740 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002741 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002742 false, false);
2743 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002744
Alex Elder79528732013-04-03 21:32:51 -05002745 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002746
Alex Elder87f979d2013-02-15 11:42:29 -06002747 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002748 if (!rc)
2749 rc = ceph_osdc_wait_request(osdc, req);
2750
2751 ceph_osdc_put_request(req);
2752 if (rc == 0)
2753 rc = len;
2754 dout("writepages result %d\n", rc);
2755 return rc;
2756}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002757EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002758
Alex Elder5522ae02013-05-01 12:43:04 -05002759int ceph_osdc_setup(void)
2760{
2761 BUG_ON(ceph_osd_request_cache);
2762 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2763 sizeof (struct ceph_osd_request),
2764 __alignof__(struct ceph_osd_request),
2765 0, NULL);
2766
2767 return ceph_osd_request_cache ? 0 : -ENOMEM;
2768}
2769EXPORT_SYMBOL(ceph_osdc_setup);
2770
2771void ceph_osdc_cleanup(void)
2772{
2773 BUG_ON(!ceph_osd_request_cache);
2774 kmem_cache_destroy(ceph_osd_request_cache);
2775 ceph_osd_request_cache = NULL;
2776}
2777EXPORT_SYMBOL(ceph_osdc_cleanup);
2778
Sage Weilf24e9982009-10-06 11:31:10 -07002779/*
2780 * handle incoming message
2781 */
2782static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2783{
2784 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002785 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002786 int type = le16_to_cpu(msg->hdr.type);
2787
2788 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002789 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002790 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002791
2792 switch (type) {
2793 case CEPH_MSG_OSD_MAP:
2794 ceph_osdc_handle_map(osdc, msg);
2795 break;
2796 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002797 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002798 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002799 case CEPH_MSG_WATCH_NOTIFY:
2800 handle_watch_notify(osdc, msg);
2801 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002802
2803 default:
2804 pr_err("received unknown message type %d %s\n", type,
2805 ceph_msg_type_name(type));
2806 }
Sage Weil4a32f932010-06-13 10:27:53 -07002807out:
Sage Weilf24e9982009-10-06 11:31:10 -07002808 ceph_msg_put(msg);
2809}
2810
Sage Weil5b3a4db2010-02-19 21:43:23 -08002811/*
Sage Weil21b667f2010-03-04 10:22:59 -08002812 * lookup and return message for incoming reply. set up reply message
2813 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002814 */
2815static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002816 struct ceph_msg_header *hdr,
2817 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002818{
2819 struct ceph_osd *osd = con->private;
2820 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002821 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002822 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002823 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002824 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002825 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002826
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002827 tid = le64_to_cpu(hdr->tid);
2828 mutex_lock(&osdc->request_mutex);
2829 req = __lookup_request(osdc, tid);
2830 if (!req) {
2831 *skip = 1;
2832 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002833 dout("get_reply unknown tid %llu from osd%d\n", tid,
2834 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002835 goto out;
2836 }
Sage Weilc16e7862010-03-01 13:02:00 -08002837
Alex Elderace6d3a2013-04-01 16:12:14 -05002838 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002839 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002840 req->r_reply, req->r_reply->con);
2841 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002842
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002843 if (front_len > req->r_reply->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002844 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2845 front_len, req->r_reply->front_alloc_len,
2846 (unsigned int)con->peer_name.type,
2847 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002848 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2849 false);
Sage Weila79832f2010-04-01 16:06:19 -07002850 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002851 goto out;
2852 ceph_msg_put(req->r_reply);
2853 req->r_reply = m;
2854 }
2855 m = ceph_msg_get(req->r_reply);
2856
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002857 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002858 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002859
Alex Eldera4ce40a2013-04-05 01:27:12 -05002860 /*
2861 * XXX This is assuming there is only one op containing
2862 * XXX page data. Probably OK for reads, but this
2863 * XXX ought to be done more generally.
2864 */
Alex Elder406e2c92013-04-15 14:50:36 -05002865 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002866 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002867 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002868 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002869
Joe Perchesb9a67892014-09-09 21:17:29 -07002870 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
Alex Eldere0c59482013-03-07 15:38:25 -06002871 tid, data_len, osd_data->length);
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002872 *skip = 1;
2873 ceph_msg_put(m);
2874 m = NULL;
2875 goto out;
2876 }
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002877 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002878 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002879 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002880 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002881
2882out:
2883 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002884 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002885
2886}
2887
2888static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2889 struct ceph_msg_header *hdr,
2890 int *skip)
2891{
2892 struct ceph_osd *osd = con->private;
2893 int type = le16_to_cpu(hdr->type);
2894 int front = le32_to_cpu(hdr->front_len);
2895
Alex Elder1c20f2d2012-06-04 14:43:32 -05002896 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002897 switch (type) {
2898 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002899 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002900 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002901 case CEPH_MSG_OSD_OPREPLY:
2902 return get_reply(con, hdr, skip);
2903 default:
2904 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2905 osd->o_osd);
2906 *skip = 1;
2907 return NULL;
2908 }
Sage Weilf24e9982009-10-06 11:31:10 -07002909}
2910
2911/*
2912 * Wrappers to refcount containing ceph_osd struct
2913 */
2914static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2915{
2916 struct ceph_osd *osd = con->private;
2917 if (get_osd(osd))
2918 return con;
2919 return NULL;
2920}
2921
2922static void put_osd_con(struct ceph_connection *con)
2923{
2924 struct ceph_osd *osd = con->private;
2925 put_osd(osd);
2926}
2927
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002928/*
2929 * authentication
2930 */
Alex Eldera3530df2012-05-16 15:16:39 -05002931/*
2932 * Note: returned pointer is the address of a structure that's
2933 * managed separately. Caller must *not* attempt to free it.
2934 */
2935static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002936 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002937{
2938 struct ceph_osd *o = con->private;
2939 struct ceph_osd_client *osdc = o->o_osdc;
2940 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002941 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002942
Alex Elder74f18692012-05-16 15:16:39 -05002943 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002944 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002945 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002946 }
Sage Weil27859f92013-03-25 10:26:14 -07002947 if (!auth->authorizer) {
2948 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2949 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002950 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002951 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002952 } else {
2953 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002954 auth);
2955 if (ret)
2956 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002957 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002958 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002959
Alex Eldera3530df2012-05-16 15:16:39 -05002960 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002961}
2962
2963
2964static int verify_authorizer_reply(struct ceph_connection *con, int len)
2965{
2966 struct ceph_osd *o = con->private;
2967 struct ceph_osd_client *osdc = o->o_osdc;
2968 struct ceph_auth_client *ac = osdc->client->monc.auth;
2969
Sage Weil27859f92013-03-25 10:26:14 -07002970 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002971}
2972
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002973static int invalidate_authorizer(struct ceph_connection *con)
2974{
2975 struct ceph_osd *o = con->private;
2976 struct ceph_osd_client *osdc = o->o_osdc;
2977 struct ceph_auth_client *ac = osdc->client->monc.auth;
2978
Sage Weil27859f92013-03-25 10:26:14 -07002979 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002980 return ceph_monc_validate_auth(&osdc->client->monc);
2981}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002982
Yan, Zheng33d07332014-11-04 16:33:37 +08002983static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2984{
2985 struct ceph_osd *o = con->private;
2986 struct ceph_auth_handshake *auth = &o->o_auth;
2987 return ceph_auth_sign_message(auth, msg);
2988}
2989
2990static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2991{
2992 struct ceph_osd *o = con->private;
2993 struct ceph_auth_handshake *auth = &o->o_auth;
2994 return ceph_auth_check_message_signature(auth, msg);
2995}
2996
Tobias Klauser9e327892010-05-20 10:40:19 +02002997static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002998 .get = get_osd_con,
2999 .put = put_osd_con,
3000 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08003001 .get_authorizer = get_authorizer,
3002 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08003003 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07003004 .alloc_msg = alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08003005 .sign_message = sign_message,
3006 .check_message_signature = check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07003007 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07003008};