blob: 2b0555b36e4dc21f5608180d3f660c96aab7ca84 [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 &&
483 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
484 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500485
Alex Elder33803f32013-03-13 20:50:00 -0500486 op->extent.offset = offset;
487 op->extent.length = length;
488 op->extent.truncate_size = truncate_size;
489 op->extent.truncate_seq = truncate_seq;
490 if (opcode == CEPH_OSD_OP_WRITE)
491 payload_len += length;
492
493 op->payload_len = payload_len;
494}
495EXPORT_SYMBOL(osd_req_op_extent_init);
496
Alex Elderc99d2d42013-04-05 01:27:11 -0500497void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
498 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500499{
Alex Elderc99d2d42013-04-05 01:27:11 -0500500 struct ceph_osd_req_op *op;
501 u64 previous;
502
503 BUG_ON(which >= osd_req->r_num_ops);
504 op = &osd_req->r_ops[which];
505 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500506
507 if (length == previous)
508 return; /* Nothing to do */
509 BUG_ON(length > previous);
510
511 op->extent.length = length;
512 op->payload_len -= previous - length;
513}
514EXPORT_SYMBOL(osd_req_op_extent_update);
515
Alex Elderc99d2d42013-04-05 01:27:11 -0500516void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500517 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500518{
Alex Elder49719772013-02-11 12:33:24 -0600519 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500520 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500521 size_t payload_len = 0;
522 size_t size;
523
524 BUG_ON(opcode != CEPH_OSD_OP_CALL);
525
Alex Elder5f562df2013-04-05 01:27:12 -0500526 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
527 BUG_ON(!pagelist);
528 ceph_pagelist_init(pagelist);
529
Alex Elder33803f32013-03-13 20:50:00 -0500530 op->cls.class_name = class;
531 size = strlen(class);
532 BUG_ON(size > (size_t) U8_MAX);
533 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500534 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500535 payload_len += size;
536
537 op->cls.method_name = method;
538 size = strlen(method);
539 BUG_ON(size > (size_t) U8_MAX);
540 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500541 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500542 payload_len += size;
543
Alex Eldera4ce40a2013-04-05 01:27:12 -0500544 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500545
Alex Elder33803f32013-03-13 20:50:00 -0500546 op->cls.argc = 0; /* currently unused */
547
548 op->payload_len = payload_len;
549}
550EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500551
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800552int osd_req_op_xattr_init(struct ceph_osd_request *osd_req, unsigned int which,
553 u16 opcode, const char *name, const void *value,
554 size_t size, u8 cmp_op, u8 cmp_mode)
555{
556 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
557 struct ceph_pagelist *pagelist;
558 size_t payload_len;
559
560 BUG_ON(opcode != CEPH_OSD_OP_SETXATTR && opcode != CEPH_OSD_OP_CMPXATTR);
561
562 pagelist = kmalloc(sizeof(*pagelist), GFP_NOFS);
563 if (!pagelist)
564 return -ENOMEM;
565
566 ceph_pagelist_init(pagelist);
567
568 payload_len = strlen(name);
569 op->xattr.name_len = payload_len;
570 ceph_pagelist_append(pagelist, name, payload_len);
571
572 op->xattr.value_len = size;
573 ceph_pagelist_append(pagelist, value, size);
574 payload_len += size;
575
576 op->xattr.cmp_op = cmp_op;
577 op->xattr.cmp_mode = cmp_mode;
578
579 ceph_osd_data_pagelist_init(&op->xattr.osd_data, pagelist);
580 op->payload_len = payload_len;
581 return 0;
582}
583EXPORT_SYMBOL(osd_req_op_xattr_init);
584
Alex Elderc99d2d42013-04-05 01:27:11 -0500585void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
586 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500587 u64 cookie, u64 version, int flag)
588{
Alex Elder49719772013-02-11 12:33:24 -0600589 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500590
Alex Elderc99d2d42013-04-05 01:27:11 -0500591 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500592
593 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500594 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500595 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500596 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500597}
598EXPORT_SYMBOL(osd_req_op_watch_init);
599
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200600void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
601 unsigned int which,
602 u64 expected_object_size,
603 u64 expected_write_size)
604{
605 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
606 CEPH_OSD_OP_SETALLOCHINT);
607
608 op->alloc_hint.expected_object_size = expected_object_size;
609 op->alloc_hint.expected_write_size = expected_write_size;
610
611 /*
612 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
613 * not worth a feature bit. Set FAILOK per-op flag to make
614 * sure older osds don't trip over an unsupported opcode.
615 */
616 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
617}
618EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
619
Alex Elder90af3602013-04-05 14:46:01 -0500620static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500621 struct ceph_osd_data *osd_data)
622{
623 u64 length = ceph_osd_data_length(osd_data);
624
625 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
626 BUG_ON(length > (u64) SIZE_MAX);
627 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500628 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500629 length, osd_data->alignment);
630 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
631 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500632 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500633#ifdef CONFIG_BLOCK
634 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500635 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500636#endif
637 } else {
638 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
639 }
640}
641
Alex Elder175face2013-03-08 13:35:36 -0600642static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500643 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700644{
Alex Elder79528732013-04-03 21:32:51 -0500645 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500646 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500647 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500648 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600649
Alex Elder79528732013-04-03 21:32:51 -0500650 BUG_ON(which >= req->r_num_ops);
651 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500652 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
653 pr_err("unrecognized osd opcode %d\n", src->op);
654
655 return 0;
656 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700657
Alex Elder065a68f2012-04-20 15:49:43 -0500658 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600659 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600660 osd_data = &src->raw_data_in;
661 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600662 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700663 case CEPH_OSD_OP_READ:
664 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800665 case CEPH_OSD_OP_ZERO:
666 case CEPH_OSD_OP_DELETE:
667 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600668 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500669 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600670 dst->extent.offset = cpu_to_le64(src->extent.offset);
671 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700672 dst->extent.truncate_size =
673 cpu_to_le64(src->extent.truncate_size);
674 dst->extent.truncate_seq =
675 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500676 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500677 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500678 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500679 else
Alex Elder04017e22013-04-05 14:46:02 -0500680 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700681 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700682 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700683 dst->cls.class_len = src->cls.class_len;
684 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500685 osd_data = &src->cls.request_info;
686 ceph_osdc_msg_data_add(req->r_request, osd_data);
687 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
688 request_data_len = osd_data->pagelist->length;
689
690 osd_data = &src->cls.request_data;
691 data_length = ceph_osd_data_length(osd_data);
692 if (data_length) {
693 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
694 dst->cls.indata_len = cpu_to_le32(data_length);
695 ceph_osdc_msg_data_add(req->r_request, osd_data);
696 src->payload_len += data_length;
697 request_data_len += data_length;
698 }
699 osd_data = &src->cls.response_data;
700 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700701 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700702 case CEPH_OSD_OP_STARTSYNC:
703 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700704 case CEPH_OSD_OP_NOTIFY_ACK:
705 case CEPH_OSD_OP_WATCH:
706 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
707 dst->watch.ver = cpu_to_le64(src->watch.ver);
708 dst->watch.flag = src->watch.flag;
709 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200710 case CEPH_OSD_OP_SETALLOCHINT:
711 dst->alloc_hint.expected_object_size =
712 cpu_to_le64(src->alloc_hint.expected_object_size);
713 dst->alloc_hint.expected_write_size =
714 cpu_to_le64(src->alloc_hint.expected_write_size);
715 break;
Yan, Zhengd74b50b2014-11-12 14:00:43 +0800716 case CEPH_OSD_OP_SETXATTR:
717 case CEPH_OSD_OP_CMPXATTR:
718 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
719 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
720 dst->xattr.cmp_op = src->xattr.cmp_op;
721 dst->xattr.cmp_mode = src->xattr.cmp_mode;
722 osd_data = &src->xattr.osd_data;
723 ceph_osdc_msg_data_add(req->r_request, osd_data);
724 request_data_len = osd_data->pagelist->length;
725 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700726 default:
Alex Elder4c464592013-02-15 11:42:30 -0600727 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600728 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600729 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500730
731 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700732 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200733
Alex Eldera8dd0a32013-03-13 20:50:00 -0500734 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200735 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700736 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600737
Alex Elder54d50642013-04-03 01:28:58 -0500738 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700739}
740
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700741/*
Sage Weilf24e9982009-10-06 11:31:10 -0700742 * build new request AND message, calculate layout, and adjust file
743 * extent as needed.
744 *
745 * if the file was recently truncated, we include information about its
746 * old and new size so that the object can be updated appropriately. (we
747 * avoid synchronously deleting truncated objects because it's slow.)
748 *
749 * if @do_sync, include a 'startsync' command so that the osd will flush
750 * data quickly.
751 */
752struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
753 struct ceph_file_layout *layout,
754 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500755 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700756 int opcode, int flags,
757 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700758 u32 truncate_seq,
759 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600760 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700761{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700762 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500763 u64 objnum = 0;
764 u64 objoff = 0;
765 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500766 u32 object_size;
767 u64 object_base;
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 &&
771 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
772 opcode != CEPH_OSD_OP_TRUNCATE);
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
Alex Elderd18d1e22013-03-13 20:50:01 -0500788 object_size = le32_to_cpu(layout->fl_object_size);
789 object_base = off - objoff;
Yan, Zhengccca4e32013-06-02 18:40:23 +0800790 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
791 if (truncate_size <= object_base) {
792 truncate_size = 0;
793 } else {
794 truncate_size -= object_base;
795 if (truncate_size > object_size)
796 truncate_size = object_size;
797 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500798 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500799
Alex Elderc99d2d42013-04-05 01:27:11 -0500800 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500801 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500802
Alex Elderacead002013-03-14 14:09:05 -0500803 /*
804 * A second op in the ops array means the caller wants to
805 * also issue a include a 'startsync' command so that the
806 * osd will flush data quickly.
807 */
808 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500809 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500810
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200811 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700812
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200813 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200814 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200815 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600816
Sage Weilf24e9982009-10-06 11:31:10 -0700817 return req;
818}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700819EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700820
821/*
822 * We keep osd requests in an rbtree, sorted by ->r_tid.
823 */
824static void __insert_request(struct ceph_osd_client *osdc,
825 struct ceph_osd_request *new)
826{
827 struct rb_node **p = &osdc->requests.rb_node;
828 struct rb_node *parent = NULL;
829 struct ceph_osd_request *req = NULL;
830
831 while (*p) {
832 parent = *p;
833 req = rb_entry(parent, struct ceph_osd_request, r_node);
834 if (new->r_tid < req->r_tid)
835 p = &(*p)->rb_left;
836 else if (new->r_tid > req->r_tid)
837 p = &(*p)->rb_right;
838 else
839 BUG();
840 }
841
842 rb_link_node(&new->r_node, parent, p);
843 rb_insert_color(&new->r_node, &osdc->requests);
844}
845
846static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
847 u64 tid)
848{
849 struct ceph_osd_request *req;
850 struct rb_node *n = osdc->requests.rb_node;
851
852 while (n) {
853 req = rb_entry(n, struct ceph_osd_request, r_node);
854 if (tid < req->r_tid)
855 n = n->rb_left;
856 else if (tid > req->r_tid)
857 n = n->rb_right;
858 else
859 return req;
860 }
861 return NULL;
862}
863
864static struct ceph_osd_request *
865__lookup_request_ge(struct ceph_osd_client *osdc,
866 u64 tid)
867{
868 struct ceph_osd_request *req;
869 struct rb_node *n = osdc->requests.rb_node;
870
871 while (n) {
872 req = rb_entry(n, struct ceph_osd_request, r_node);
873 if (tid < req->r_tid) {
874 if (!n->rb_left)
875 return req;
876 n = n->rb_left;
877 } else if (tid > req->r_tid) {
878 n = n->rb_right;
879 } else {
880 return req;
881 }
882 }
883 return NULL;
884}
885
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400886static void __kick_linger_request(struct ceph_osd_request *req)
887{
888 struct ceph_osd_client *osdc = req->r_osdc;
889 struct ceph_osd *osd = req->r_osd;
890
891 /*
892 * Linger requests need to be resent with a new tid to avoid
893 * the dup op detection logic on the OSDs. Achieve this with
894 * a re-register dance instead of open-coding.
895 */
896 ceph_osdc_get_request(req);
897 if (!list_empty(&req->r_linger_item))
898 __unregister_linger_request(osdc, req);
899 else
900 __unregister_request(osdc, req);
901 __register_request(osdc, req);
902 ceph_osdc_put_request(req);
903
904 /*
905 * Unless request has been registered as both normal and
906 * lingering, __unregister{,_linger}_request clears r_osd.
907 * However, here we need to preserve r_osd to make sure we
908 * requeue on the same OSD.
909 */
910 WARN_ON(req->r_osd || !osd);
911 req->r_osd = osd;
912
913 dout("%s requeueing %p tid %llu\n", __func__, req, req->r_tid);
914 __enqueue_request(req);
915}
916
Sage Weil6f6c7002011-01-17 20:34:08 -0800917/*
918 * Resubmit requests pending on the given osd.
919 */
920static void __kick_osd_requests(struct ceph_osd_client *osdc,
921 struct ceph_osd *osd)
922{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700923 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500924 LIST_HEAD(resend);
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400925 LIST_HEAD(resend_linger);
Sage Weil6f6c7002011-01-17 20:34:08 -0800926 int err;
927
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400928 dout("%s osd%d\n", __func__, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800929 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600930 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800931 return;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400932
Alex Eldere02493c2013-03-25 18:16:11 -0500933 /*
934 * Build up a list of requests to resend by traversing the
935 * osd's list of requests. Requests for a given object are
936 * sent in tid order, and that is also the order they're
937 * kept on this list. Therefore all requests that are in
938 * flight will be found first, followed by all requests that
939 * have not yet been sent. And to resend requests while
940 * preserving this order we will want to put any sent
941 * requests back on the front of the osd client's unsent
942 * list.
943 *
944 * So we build a separate ordered list of already-sent
945 * requests for the affected osd and splice it onto the
946 * front of the osd client's unsent list. Once we've seen a
947 * request that has not yet been sent we're done. Those
948 * requests are already sitting right where they belong.
949 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800950 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500951 if (!req->r_sent)
952 break;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400953
954 if (!req->r_linger) {
955 dout("%s requeueing %p tid %llu\n", __func__, req,
956 req->r_tid);
957 list_move_tail(&req->r_req_lru_item, &resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700958 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400959 } else {
960 list_move_tail(&req->r_req_lru_item, &resend_linger);
961 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700962 }
Alex Eldere02493c2013-03-25 18:16:11 -0500963 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700964
Alex Eldere02493c2013-03-25 18:16:11 -0500965 /*
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400966 * Both registered and not yet registered linger requests are
967 * enqueued with a new tid on the same OSD. We add/move them
968 * to req_unsent/o_requests at the end to keep things in tid
969 * order.
Alex Eldere02493c2013-03-25 18:16:11 -0500970 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700971 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
Ilya Dryomov1d0326b2014-06-20 14:14:41 +0400972 r_linger_osd_item) {
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400973 WARN_ON(!list_empty(&req->r_req_lru_item));
974 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800975 }
Ilya Dryomov2cc61282014-09-03 14:41:45 +0400976
977 list_for_each_entry_safe(req, nreq, &resend_linger, r_req_lru_item)
978 __kick_linger_request(req);
Sage Weil6f6c7002011-01-17 20:34:08 -0800979}
980
Sage Weilf24e9982009-10-06 11:31:10 -0700981/*
Sage Weil81b024e2009-10-09 10:29:18 -0700982 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700983 */
984static void osd_reset(struct ceph_connection *con)
985{
986 struct ceph_osd *osd = con->private;
987 struct ceph_osd_client *osdc;
988
989 if (!osd)
990 return;
991 dout("osd_reset osd%d\n", osd->o_osd);
992 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700993 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800994 mutex_lock(&osdc->request_mutex);
995 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600996 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800997 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700998 up_read(&osdc->map_sem);
999}
1000
1001/*
1002 * Track open sessions with osds.
1003 */
Alex Eldere10006f2012-05-26 23:26:43 -05001004static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -07001005{
1006 struct ceph_osd *osd;
1007
1008 osd = kzalloc(sizeof(*osd), GFP_NOFS);
1009 if (!osd)
1010 return NULL;
1011
1012 atomic_set(&osd->o_ref, 1);
1013 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -05001014 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -06001015 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001016 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001017 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001018 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 osd->o_incarnation = 1;
1020
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001021 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001022
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001023 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001024 return osd;
1025}
1026
1027static struct ceph_osd *get_osd(struct ceph_osd *osd)
1028{
1029 if (atomic_inc_not_zero(&osd->o_ref)) {
1030 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
1031 atomic_read(&osd->o_ref));
1032 return osd;
1033 } else {
1034 dout("get_osd %p FAIL\n", osd);
1035 return NULL;
1036 }
1037}
1038
1039static void put_osd(struct ceph_osd *osd)
1040{
1041 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
1042 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -05001043 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -07001044 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
1045
Sage Weil27859f92013-03-25 10:26:14 -07001046 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001047 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001048 }
Sage Weilf24e9982009-10-06 11:31:10 -07001049}
1050
1051/*
1052 * remove an osd from our map
1053 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001054static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001055{
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001056 dout("__remove_osd %p\n", osd);
Ilya Dryomovcc9f1f52014-11-05 19:33:44 +03001057 WARN_ON(!list_empty(&osd->o_requests));
1058 WARN_ON(!list_empty(&osd->o_linger_requests));
Ilya Dryomov7c6e6fc2014-06-18 13:02:12 +04001059
Sage Weilf24e9982009-10-06 11:31:10 -07001060 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001061 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001062 ceph_con_close(&osd->o_con);
1063 put_osd(osd);
1064}
1065
Sage Weilaca420b2011-08-31 14:45:53 -07001066static void remove_all_osds(struct ceph_osd_client *osdc)
1067{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001068 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001069 mutex_lock(&osdc->request_mutex);
1070 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1071 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1072 struct ceph_osd, o_node);
1073 __remove_osd(osdc, osd);
1074 }
1075 mutex_unlock(&osdc->request_mutex);
1076}
1077
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001078static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1079 struct ceph_osd *osd)
1080{
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001081 dout("%s %p\n", __func__, osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001082 BUG_ON(!list_empty(&osd->o_osd_lru));
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001083
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001084 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001085 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001086}
1087
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001088static void maybe_move_osd_to_lru(struct ceph_osd_client *osdc,
1089 struct ceph_osd *osd)
1090{
1091 dout("%s %p\n", __func__, osd);
1092
1093 if (list_empty(&osd->o_requests) &&
1094 list_empty(&osd->o_linger_requests))
1095 __move_osd_to_lru(osdc, osd);
1096}
1097
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001098static void __remove_osd_from_lru(struct ceph_osd *osd)
1099{
1100 dout("__remove_osd_from_lru %p\n", osd);
1101 if (!list_empty(&osd->o_osd_lru))
1102 list_del_init(&osd->o_osd_lru);
1103}
1104
Sage Weilaca420b2011-08-31 14:45:53 -07001105static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001106{
1107 struct ceph_osd *osd, *nosd;
1108
1109 dout("__remove_old_osds %p\n", osdc);
1110 mutex_lock(&osdc->request_mutex);
1111 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001112 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001113 break;
1114 __remove_osd(osdc, osd);
1115 }
1116 mutex_unlock(&osdc->request_mutex);
1117}
1118
Sage Weilf24e9982009-10-06 11:31:10 -07001119/*
1120 * reset osd connect
1121 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001122static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001123{
Alex Elderc3acb182012-12-07 09:57:58 -06001124 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001125
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001126 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001127 if (list_empty(&osd->o_requests) &&
1128 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001129 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001130
1131 return -ENODEV;
1132 }
1133
1134 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1135 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1136 !ceph_con_opened(&osd->o_con)) {
1137 struct ceph_osd_request *req;
1138
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001139 dout("osd addr hasn't changed and connection never opened, "
1140 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001141 /* touch each r_stamp for handle_timeout()'s benfit */
1142 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1143 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001144
1145 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001146 }
Alex Elderc3acb182012-12-07 09:57:58 -06001147
1148 ceph_con_close(&osd->o_con);
1149 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1150 osd->o_incarnation++;
1151
1152 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001153}
1154
1155static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1156{
1157 struct rb_node **p = &osdc->osds.rb_node;
1158 struct rb_node *parent = NULL;
1159 struct ceph_osd *osd = NULL;
1160
Sage Weilaca420b2011-08-31 14:45:53 -07001161 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001162 while (*p) {
1163 parent = *p;
1164 osd = rb_entry(parent, struct ceph_osd, o_node);
1165 if (new->o_osd < osd->o_osd)
1166 p = &(*p)->rb_left;
1167 else if (new->o_osd > osd->o_osd)
1168 p = &(*p)->rb_right;
1169 else
1170 BUG();
1171 }
1172
1173 rb_link_node(&new->o_node, parent, p);
1174 rb_insert_color(&new->o_node, &osdc->osds);
1175}
1176
1177static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1178{
1179 struct ceph_osd *osd;
1180 struct rb_node *n = osdc->osds.rb_node;
1181
1182 while (n) {
1183 osd = rb_entry(n, struct ceph_osd, o_node);
1184 if (o < osd->o_osd)
1185 n = n->rb_left;
1186 else if (o > osd->o_osd)
1187 n = n->rb_right;
1188 else
1189 return osd;
1190 }
1191 return NULL;
1192}
1193
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001194static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1195{
1196 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001197 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001198}
1199
1200static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1201{
1202 cancel_delayed_work(&osdc->timeout_work);
1203}
Sage Weilf24e9982009-10-06 11:31:10 -07001204
1205/*
1206 * Register request, assign tid. If this is the first request, set up
1207 * the timeout event.
1208 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001209static void __register_request(struct ceph_osd_client *osdc,
1210 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001211{
Sage Weilf24e9982009-10-06 11:31:10 -07001212 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001213 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001214 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001215 __insert_request(osdc, req);
1216 ceph_osdc_get_request(req);
1217 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001218 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001219 dout(" first request, scheduling timeout\n");
1220 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001221 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001222}
1223
Sage Weilf24e9982009-10-06 11:31:10 -07001224/*
1225 * called under osdc->request_mutex
1226 */
1227static void __unregister_request(struct ceph_osd_client *osdc,
1228 struct ceph_osd_request *req)
1229{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001230 if (RB_EMPTY_NODE(&req->r_node)) {
1231 dout("__unregister_request %p tid %lld not registered\n",
1232 req, req->r_tid);
1233 return;
1234 }
1235
Sage Weilf24e9982009-10-06 11:31:10 -07001236 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1237 rb_erase(&req->r_node, &osdc->requests);
Ilya Dryomov6562d662014-06-20 14:14:42 +04001238 RB_CLEAR_NODE(&req->r_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001239 osdc->num_requests--;
1240
Sage Weil0ba64782009-10-08 16:57:16 -07001241 if (req->r_osd) {
1242 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001243 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001244
1245 list_del_init(&req->r_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001246 maybe_move_osd_to_lru(osdc, req->r_osd);
Ilya Dryomov4f234092014-06-20 18:29:20 +04001247 if (list_empty(&req->r_linger_osd_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001248 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001249 }
Sage Weilf24e9982009-10-06 11:31:10 -07001250
Alex Elder7d5f2482012-11-29 08:37:03 -06001251 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001252 ceph_osdc_put_request(req);
1253
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001254 if (osdc->num_requests == 0) {
1255 dout(" no requests, canceling timeout\n");
1256 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001257 }
1258}
1259
1260/*
1261 * Cancel a previously queued request message
1262 */
1263static void __cancel_request(struct ceph_osd_request *req)
1264{
Sage Weil6bc18872010-09-27 10:18:52 -07001265 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001266 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001267 req->r_sent = 0;
1268 }
1269}
1270
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001271static void __register_linger_request(struct ceph_osd_client *osdc,
1272 struct ceph_osd_request *req)
1273{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001274 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
1275 WARN_ON(!req->r_linger);
1276
Alex Elder96e4dac2013-05-22 20:54:25 -05001277 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001278 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001279 if (req->r_osd)
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001280 list_add_tail(&req->r_linger_osd_item,
Sage Weil6194ea82012-07-30 16:19:28 -07001281 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001282}
1283
1284static void __unregister_linger_request(struct ceph_osd_client *osdc,
1285 struct ceph_osd_request *req)
1286{
Ilya Dryomovaf593062014-06-20 18:29:20 +04001287 WARN_ON(!req->r_linger);
1288
1289 if (list_empty(&req->r_linger_item)) {
1290 dout("%s %p tid %llu not registered\n", __func__, req,
1291 req->r_tid);
1292 return;
1293 }
1294
1295 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
Alex Elder61c74032012-12-06 09:37:23 -06001296 list_del_init(&req->r_linger_item);
Ilya Dryomovaf593062014-06-20 18:29:20 +04001297
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001298 if (req->r_osd) {
Ilya Dryomov1d0326b2014-06-20 14:14:41 +04001299 list_del_init(&req->r_linger_osd_item);
Ilya Dryomovbbf37ec2014-06-20 14:14:41 +04001300 maybe_move_osd_to_lru(osdc, req->r_osd);
Sage Weilfbdb9192011-03-29 12:11:06 -07001301 if (list_empty(&req->r_osd_item))
1302 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001303 }
Ilya Dryomovba9d1142014-11-05 15:45:58 +03001304
1305 list_del_init(&req->r_req_lru_item); /* can be on notarget */
Alex Elder96e4dac2013-05-22 20:54:25 -05001306 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001307}
1308
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001309void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1310 struct ceph_osd_request *req)
1311{
1312 if (!req->r_linger) {
1313 dout("set_request_linger %p\n", req);
1314 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001315 }
1316}
1317EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1318
Sage Weilf24e9982009-10-06 11:31:10 -07001319/*
Josh Durgind29adb32013-12-02 19:11:48 -08001320 * Returns whether a request should be blocked from being sent
1321 * based on the current osdmap and osd_client settings.
1322 *
1323 * Caller should hold map_sem for read.
1324 */
1325static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1326 struct ceph_osd_request *req)
1327{
1328 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1329 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1330 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1331 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1332 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1333}
1334
1335/*
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001336 * Calculate mapping of a request to a PG. Takes tiering into account.
1337 */
1338static int __calc_request_pg(struct ceph_osdmap *osdmap,
1339 struct ceph_osd_request *req,
1340 struct ceph_pg *pg_out)
1341{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001342 bool need_check_tiering;
1343
1344 need_check_tiering = false;
1345 if (req->r_target_oloc.pool == -1) {
1346 req->r_target_oloc = req->r_base_oloc; /* struct */
1347 need_check_tiering = true;
1348 }
1349 if (req->r_target_oid.name_len == 0) {
1350 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1351 need_check_tiering = true;
1352 }
1353
1354 if (need_check_tiering &&
1355 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001356 struct ceph_pg_pool_info *pi;
1357
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001358 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001359 if (pi) {
1360 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1361 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001362 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001363 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1364 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001365 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001366 }
1367 /* !pi is caught in ceph_oloc_oid_to_pg() */
1368 }
1369
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001370 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1371 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001372}
1373
Ilya Dryomovf671b582014-09-02 13:40:33 +04001374static void __enqueue_request(struct ceph_osd_request *req)
1375{
1376 struct ceph_osd_client *osdc = req->r_osdc;
1377
1378 dout("%s %p tid %llu to osd%d\n", __func__, req, req->r_tid,
1379 req->r_osd ? req->r_osd->o_osd : -1);
1380
1381 if (req->r_osd) {
1382 __remove_osd_from_lru(req->r_osd);
1383 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1384 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
1385 } else {
1386 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
1387 }
1388}
1389
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001390/*
Sage Weilf24e9982009-10-06 11:31:10 -07001391 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1392 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001393 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001394 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001395 *
1396 * Return 0 if unchanged, 1 if changed, or negative on error.
1397 *
1398 * Caller should hold map_sem for read and request_mutex.
1399 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001400static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001401 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001402{
Sage Weil5b191d92013-02-23 10:38:16 -08001403 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001404 int acting[CEPH_PG_MAX_SIZE];
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001405 int num, o;
Sage Weilf24e9982009-10-06 11:31:10 -07001406 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001407 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001408
Sage Weil6f6c7002011-01-17 20:34:08 -08001409 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e42014-01-27 17:40:19 +02001410
1411 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001412 if (err) {
1413 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001414 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001415 }
Sage Weil7740a422010-01-08 15:58:25 -08001416 req->r_pgid = pgid;
1417
Ilya Dryomov8008ab12014-03-24 17:12:48 +02001418 num = ceph_calc_pg_acting(osdc->osdmap, pgid, acting, &o);
1419 if (num < 0)
1420 num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001421
Josh Durgind29adb32013-12-02 19:11:48 -08001422 was_paused = req->r_paused;
1423 req->r_paused = __req_should_be_paused(osdc, req);
1424 if (was_paused && !req->r_paused)
1425 force_resend = 1;
1426
Sage Weil38d64532011-10-14 13:33:55 -07001427 if ((!force_resend &&
1428 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001429 req->r_sent >= req->r_osd->o_incarnation &&
1430 req->r_num_pg_osds == num &&
1431 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001432 (req->r_osd == NULL && o == -1) ||
1433 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001434 return 0; /* no change */
1435
Sage Weil5b191d92013-02-23 10:38:16 -08001436 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1437 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001438 req->r_osd ? req->r_osd->o_osd : -1);
1439
Sage Weild85b7052010-05-10 10:24:48 -07001440 /* record full pg acting set */
1441 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1442 req->r_num_pg_osds = num;
1443
Sage Weilf24e9982009-10-06 11:31:10 -07001444 if (req->r_osd) {
1445 __cancel_request(req);
1446 list_del_init(&req->r_osd_item);
Ilya Dryomova390de02014-11-04 18:32:14 +03001447 list_del_init(&req->r_linger_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001448 req->r_osd = NULL;
1449 }
1450
1451 req->r_osd = __lookup_osd(osdc, o);
1452 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001453 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001454 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001455 if (!req->r_osd) {
1456 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001457 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001458 }
Sage Weilf24e9982009-10-06 11:31:10 -07001459
Sage Weil6f6c7002011-01-17 20:34:08 -08001460 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001461 __insert_osd(osdc, req->r_osd);
1462
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001463 ceph_con_open(&req->r_osd->o_con,
1464 CEPH_ENTITY_TYPE_OSD, o,
1465 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001466 }
1467
Ilya Dryomovf671b582014-09-02 13:40:33 +04001468 __enqueue_request(req);
Sage Weild85b7052010-05-10 10:24:48 -07001469 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001470
1471out:
Sage Weilf24e9982009-10-06 11:31:10 -07001472 return err;
1473}
1474
1475/*
1476 * caller should hold map_sem (for read) and request_mutex
1477 */
Sage Weil56e925b2012-01-03 12:34:34 -08001478static void __send_request(struct ceph_osd_client *osdc,
1479 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001480{
Sage Weil1b83bef2013-02-25 16:11:12 -08001481 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001482
Sage Weil1b83bef2013-02-25 16:11:12 -08001483 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1484 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1485 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001486
Sage Weil1b83bef2013-02-25 16:11:12 -08001487 /* fill in message content that changes each time we send it */
1488 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1489 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001490 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001491 p = req->r_request_pgid;
1492 ceph_encode_64(&p, req->r_pgid.pool);
1493 ceph_encode_32(&p, req->r_pgid.seed);
1494 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1495 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1496 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001497
Sage Weil3dd72fc2010-03-22 14:42:30 -07001498 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001499 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001500
1501 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001502
Sage Weilf24e9982009-10-06 11:31:10 -07001503 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001504
1505 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001506}
1507
1508/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001509 * Send any requests in the queue (req_unsent).
1510 */
Alex Elderf9d25192013-02-15 11:42:29 -06001511static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001512{
1513 struct ceph_osd_request *req, *tmp;
1514
Alex Elderf9d25192013-02-15 11:42:29 -06001515 dout("__send_queued\n");
1516 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001517 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001518}
1519
1520/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001521 * Caller should hold map_sem for read and request_mutex.
1522 */
1523static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1524 struct ceph_osd_request *req,
1525 bool nofail)
1526{
1527 int rc;
1528
1529 __register_request(osdc, req);
1530 req->r_sent = 0;
1531 req->r_got_reply = 0;
1532 rc = __map_request(osdc, req, 0);
1533 if (rc < 0) {
1534 if (nofail) {
1535 dout("osdc_start_request failed map, "
1536 " will retry %lld\n", req->r_tid);
1537 rc = 0;
1538 } else {
1539 __unregister_request(osdc, req);
1540 }
1541 return rc;
1542 }
1543
1544 if (req->r_osd == NULL) {
1545 dout("send_request %p no up osds in pg\n", req);
1546 ceph_monc_request_next_osdmap(&osdc->client->monc);
1547 } else {
1548 __send_queued(osdc);
1549 }
1550
1551 return 0;
1552}
1553
1554/*
Sage Weilf24e9982009-10-06 11:31:10 -07001555 * Timeout callback, called every N seconds when 1 or more osd
1556 * requests has been active for more than N seconds. When this
1557 * happens, we ping all OSDs with requests who have timed out to
1558 * ensure any communications channel reset is detected. Reset the
1559 * request timeouts another N seconds in the future as we go.
1560 * Reschedule the timeout event another N seconds in future (unless
1561 * there are no open requests).
1562 */
1563static void handle_timeout(struct work_struct *work)
1564{
1565 struct ceph_osd_client *osdc =
1566 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001567 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001568 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001569 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001570 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001571 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001572 dout("timeout\n");
1573 down_read(&osdc->map_sem);
1574
1575 ceph_monc_request_next_osdmap(&osdc->client->monc);
1576
1577 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001578
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001579 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001580 * ping osds that are a bit slow. this ensures that if there
1581 * is a break in the TCP connection we will notice, and reopen
1582 * a connection with that osd (from the fault callback).
1583 */
1584 INIT_LIST_HEAD(&slow_osds);
1585 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001586 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001587 break;
1588
1589 osd = req->r_osd;
1590 BUG_ON(!osd);
1591 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001592 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001593 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1594 }
1595 while (!list_empty(&slow_osds)) {
1596 osd = list_entry(slow_osds.next, struct ceph_osd,
1597 o_keepalive_item);
1598 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001599 ceph_con_keepalive(&osd->o_con);
1600 }
1601
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001602 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001603 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001604 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001605 up_read(&osdc->map_sem);
1606}
1607
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001608static void handle_osds_timeout(struct work_struct *work)
1609{
1610 struct ceph_osd_client *osdc =
1611 container_of(work, struct ceph_osd_client,
1612 osds_timeout_work.work);
1613 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001614 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001615
1616 dout("osds timeout\n");
1617 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001618 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001619 up_read(&osdc->map_sem);
1620
1621 schedule_delayed_work(&osdc->osds_timeout_work,
1622 round_jiffies_relative(delay));
1623}
1624
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001625static int ceph_oloc_decode(void **p, void *end,
1626 struct ceph_object_locator *oloc)
1627{
1628 u8 struct_v, struct_cv;
1629 u32 len;
1630 void *struct_end;
1631 int ret = 0;
1632
1633 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1634 struct_v = ceph_decode_8(p);
1635 struct_cv = ceph_decode_8(p);
1636 if (struct_v < 3) {
1637 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1638 struct_v, struct_cv);
1639 goto e_inval;
1640 }
1641 if (struct_cv > 6) {
1642 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1643 struct_v, struct_cv);
1644 goto e_inval;
1645 }
1646 len = ceph_decode_32(p);
1647 ceph_decode_need(p, end, len, e_inval);
1648 struct_end = *p + len;
1649
1650 oloc->pool = ceph_decode_64(p);
1651 *p += 4; /* skip preferred */
1652
1653 len = ceph_decode_32(p);
1654 if (len > 0) {
1655 pr_warn("ceph_object_locator::key is set\n");
1656 goto e_inval;
1657 }
1658
1659 if (struct_v >= 5) {
1660 len = ceph_decode_32(p);
1661 if (len > 0) {
1662 pr_warn("ceph_object_locator::nspace is set\n");
1663 goto e_inval;
1664 }
1665 }
1666
1667 if (struct_v >= 6) {
1668 s64 hash = ceph_decode_64(p);
1669 if (hash != -1) {
1670 pr_warn("ceph_object_locator::hash is set\n");
1671 goto e_inval;
1672 }
1673 }
1674
1675 /* skip the rest */
1676 *p = struct_end;
1677out:
1678 return ret;
1679
1680e_inval:
1681 ret = -EINVAL;
1682 goto out;
1683}
1684
1685static int ceph_redirect_decode(void **p, void *end,
1686 struct ceph_request_redirect *redir)
1687{
1688 u8 struct_v, struct_cv;
1689 u32 len;
1690 void *struct_end;
1691 int ret;
1692
1693 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1694 struct_v = ceph_decode_8(p);
1695 struct_cv = ceph_decode_8(p);
1696 if (struct_cv > 1) {
1697 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1698 struct_v, struct_cv);
1699 goto e_inval;
1700 }
1701 len = ceph_decode_32(p);
1702 ceph_decode_need(p, end, len, e_inval);
1703 struct_end = *p + len;
1704
1705 ret = ceph_oloc_decode(p, end, &redir->oloc);
1706 if (ret)
1707 goto out;
1708
1709 len = ceph_decode_32(p);
1710 if (len > 0) {
1711 pr_warn("ceph_request_redirect::object_name is set\n");
1712 goto e_inval;
1713 }
1714
1715 len = ceph_decode_32(p);
1716 *p += len; /* skip osd_instructions */
1717
1718 /* skip the rest */
1719 *p = struct_end;
1720out:
1721 return ret;
1722
1723e_inval:
1724 ret = -EINVAL;
1725 goto out;
1726}
1727
Sage Weil25845472011-06-03 09:37:09 -07001728static void complete_request(struct ceph_osd_request *req)
1729{
Sage Weil25845472011-06-03 09:37:09 -07001730 complete_all(&req->r_safe_completion); /* fsync waiter */
1731}
1732
Sage Weilf24e9982009-10-06 11:31:10 -07001733/*
1734 * handle osd op reply. either call the callback if it is specified,
1735 * or do the completion to wake up the waiting thread.
1736 */
Sage Weil350b1c32009-12-22 10:45:45 -08001737static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1738 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001739{
Sage Weil1b83bef2013-02-25 16:11:12 -08001740 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001741 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001742 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001743 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001744 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001745 unsigned int numops;
1746 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001747 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001748 s32 retry_attempt;
1749 struct ceph_pg pg;
1750 int err;
1751 u32 reassert_epoch;
1752 u64 reassert_version;
1753 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001754 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001755 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001756 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001757
Sage Weil6df058c2009-12-22 11:24:33 -08001758 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001759 dout("handle_reply %p tid %llu\n", msg, tid);
1760
1761 p = msg->front.iov_base;
1762 end = p + msg->front.iov_len;
1763
1764 ceph_decode_need(&p, end, 4, bad);
1765 object_len = ceph_decode_32(&p);
1766 ceph_decode_need(&p, end, object_len, bad);
1767 p += object_len;
1768
Alex Elderef4859d2013-04-01 18:58:26 -05001769 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001770 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001771 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001772
1773 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1774 flags = ceph_decode_64(&p);
1775 result = ceph_decode_32(&p);
1776 reassert_epoch = ceph_decode_32(&p);
1777 reassert_version = ceph_decode_64(&p);
1778 osdmap_epoch = ceph_decode_32(&p);
1779
Sage Weilf24e9982009-10-06 11:31:10 -07001780 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001781 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001782 mutex_lock(&osdc->request_mutex);
1783 req = __lookup_request(osdc, tid);
1784 if (req == NULL) {
1785 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001786 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001787 }
1788 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001789
1790 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1791 req, result);
1792
Dan Carpenter18741192013-08-15 08:51:58 +03001793 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001794 numops = ceph_decode_32(&p);
1795 if (numops > CEPH_OSD_MAX_OP)
1796 goto bad_put;
1797 if (numops != req->r_num_ops)
1798 goto bad_put;
1799 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001800 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001801 for (i = 0; i < numops; i++) {
1802 struct ceph_osd_op *op = p;
1803 int len;
1804
1805 len = le32_to_cpu(op->payload_len);
1806 req->r_reply_op_len[i] = len;
1807 dout(" op %d has %d bytes\n", i, len);
1808 payload_len += len;
1809 p += sizeof(*op);
1810 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001811 bytes = le32_to_cpu(msg->hdr.data_len);
1812 if (payload_len != bytes) {
Joe Perchesb9a67892014-09-09 21:17:29 -07001813 pr_warn("sum of op payload lens %d != data_len %d\n",
1814 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001815 goto bad_put;
1816 }
1817
Dan Carpenter18741192013-08-15 08:51:58 +03001818 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001819 retry_attempt = ceph_decode_32(&p);
1820 for (i = 0; i < numops; i++)
1821 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001822
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001823 if (le16_to_cpu(msg->hdr.version) >= 6) {
1824 p += 8 + 4; /* skip replay_version */
1825 p += 8; /* skip user_version */
1826
1827 err = ceph_redirect_decode(&p, end, &redir);
1828 if (err)
1829 goto bad_put;
1830 } else {
1831 redir.oloc.pool = -1;
1832 }
1833
1834 if (redir.oloc.pool != -1) {
1835 dout("redirect pool %lld\n", redir.oloc.pool);
1836
1837 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001838
1839 req->r_target_oloc = redir.oloc; /* struct */
1840
1841 /*
1842 * Start redirect requests with nofail=true. If
1843 * mapping fails, request will end up on the notarget
1844 * list, waiting for the new osdmap (which can take
1845 * a while), even though the original request mapped
1846 * successfully. In the future we might want to follow
1847 * original request's nofail setting here.
1848 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001849 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001850 BUG_ON(err);
1851
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001852 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001853 }
1854
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001855 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001856 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001857 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001858 dout("handle_reply result %d bytes %d\n", req->r_result,
1859 bytes);
1860 if (req->r_result == 0)
1861 req->r_result = bytes;
1862
1863 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001864 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1865 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001866
1867 req->r_got_reply = 1;
1868 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1869 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001870 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001871 }
1872
1873 dout("handle_reply tid %llu flags %d\n", tid, flags);
1874
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001875 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1876 __register_linger_request(osdc, req);
1877
Sage Weilf24e9982009-10-06 11:31:10 -07001878 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001879 if (result < 0 ||
1880 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001881 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1882 __unregister_request(osdc, req);
1883
1884 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001885 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001886
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001887 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001888 if (req->r_unsafe_callback &&
1889 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1890 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001891 if (req->r_callback)
1892 req->r_callback(req, msg);
1893 else
1894 complete_all(&req->r_completion);
1895 }
Sage Weilf24e9982009-10-06 11:31:10 -07001896
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001897 if (flags & CEPH_OSD_FLAG_ONDISK) {
1898 if (req->r_unsafe_callback && already_completed)
1899 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001900 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001901 }
Sage Weilf24e9982009-10-06 11:31:10 -07001902
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001903out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001904 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001905 ceph_osdc_put_request(req);
1906 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001907out_unlock:
1908 mutex_unlock(&osdc->request_mutex);
1909 up_read(&osdc->map_sem);
1910 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001911
Sage Weil1b83bef2013-02-25 16:11:12 -08001912bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001913 req->r_result = -EIO;
1914 __unregister_request(osdc, req);
1915 if (req->r_callback)
1916 req->r_callback(req, msg);
1917 else
1918 complete_all(&req->r_completion);
1919 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001920 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001921bad_mutex:
1922 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001923 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001924bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001925 pr_err("corrupt osd_op_reply got %d %d\n",
1926 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001927 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001928}
1929
Sage Weil6f6c7002011-01-17 20:34:08 -08001930static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001931{
Sage Weilf24e9982009-10-06 11:31:10 -07001932 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001933
Sage Weil6f6c7002011-01-17 20:34:08 -08001934 for (p = rb_first(&osdc->osds); p; p = n) {
1935 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001936
Sage Weil6f6c7002011-01-17 20:34:08 -08001937 n = rb_next(p);
1938 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1939 memcmp(&osd->o_con.peer_addr,
1940 ceph_osd_addr(osdc->osdmap,
1941 osd->o_osd),
1942 sizeof(struct ceph_entity_addr)) != 0)
1943 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001944 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001945}
1946
1947/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001948 * Requeue requests whose mapping to an OSD has changed. If requests map to
1949 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001950 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001951 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001952 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001953static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1954 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001955{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001956 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001957 struct rb_node *p;
1958 int needmap = 0;
1959 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001960 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001961
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001962 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1963 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001964 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001965 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001966 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001967 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001968
1969 /*
1970 * For linger requests that have not yet been
1971 * registered, move them to the linger list; they'll
1972 * be sent to the osd in the loop below. Unregister
1973 * the request before re-registering it as a linger
1974 * request to ensure the __map_request() below
1975 * will decide it needs to be sent.
1976 */
1977 if (req->r_linger && list_empty(&req->r_linger_item)) {
1978 dout("%p tid %llu restart on osd%d\n",
1979 req, req->r_tid,
1980 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001981 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001982 __unregister_request(osdc, req);
1983 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001984 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001985 continue;
1986 }
1987
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001988 force_resend_req = force_resend ||
1989 (force_resend_writes &&
1990 req->r_flags & CEPH_OSD_FLAG_WRITE);
1991 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001992 if (err < 0)
1993 continue; /* error */
1994 if (req->r_osd == NULL) {
1995 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1996 needmap++; /* request a newer map */
1997 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001998 if (!req->r_linger) {
1999 dout("%p tid %llu requeued on osd%d\n", req,
2000 req->r_tid,
2001 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002002 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07002003 }
2004 }
Sage Weil6f6c7002011-01-17 20:34:08 -08002005 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002006
2007 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
2008 r_linger_item) {
2009 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
2010
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002011 err = __map_request(osdc, req,
2012 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06002013 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002014 if (err == 0)
2015 continue; /* no change and no osd was specified */
2016 if (err < 0)
2017 continue; /* hrm! */
2018 if (req->r_osd == NULL) {
2019 dout("tid %llu maps to no valid osd\n", req->r_tid);
2020 needmap++; /* request a newer map */
2021 continue;
2022 }
2023
2024 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
2025 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002026 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06002027 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002028 }
Alex Elder14d2f382013-05-15 16:28:33 -05002029 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002030 mutex_unlock(&osdc->request_mutex);
2031
2032 if (needmap) {
2033 dout("%d requests for down osds, need new map\n", needmap);
2034 ceph_monc_request_next_osdmap(&osdc->client->monc);
2035 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002036}
Sage Weil6f6c7002011-01-17 20:34:08 -08002037
2038
Sage Weilf24e9982009-10-06 11:31:10 -07002039/*
2040 * Process updated osd map.
2041 *
2042 * The message contains any number of incremental and full maps, normally
2043 * indicating some sort of topology change in the cluster. Kick requests
2044 * off to different OSDs as needed.
2045 */
2046void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
2047{
2048 void *p, *end, *next;
2049 u32 nr_maps, maplen;
2050 u32 epoch;
2051 struct ceph_osdmap *newmap = NULL, *oldmap;
2052 int err;
2053 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002054 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07002055
2056 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
2057 p = msg->front.iov_base;
2058 end = p + msg->front.iov_len;
2059
2060 /* verify fsid */
2061 ceph_decode_need(&p, end, sizeof(fsid), bad);
2062 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002063 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2064 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002065
2066 down_write(&osdc->map_sem);
2067
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002068 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2069
Sage Weilf24e9982009-10-06 11:31:10 -07002070 /* incremental maps */
2071 ceph_decode_32_safe(&p, end, nr_maps, bad);
2072 dout(" %d inc maps\n", nr_maps);
2073 while (nr_maps > 0) {
2074 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002075 epoch = ceph_decode_32(&p);
2076 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002077 ceph_decode_need(&p, end, maplen, bad);
2078 next = p + maplen;
2079 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2080 dout("applying incremental map %u len %d\n",
2081 epoch, maplen);
2082 newmap = osdmap_apply_incremental(&p, next,
2083 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002084 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002085 if (IS_ERR(newmap)) {
2086 err = PTR_ERR(newmap);
2087 goto bad;
2088 }
Sage Weil30dc6382009-12-21 14:49:37 -08002089 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002090 if (newmap != osdc->osdmap) {
2091 ceph_osdmap_destroy(osdc->osdmap);
2092 osdc->osdmap = newmap;
2093 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002094 was_full = was_full ||
2095 ceph_osdmap_flag(osdc->osdmap,
2096 CEPH_OSDMAP_FULL);
2097 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002098 } else {
2099 dout("ignoring incremental map %u len %d\n",
2100 epoch, maplen);
2101 }
2102 p = next;
2103 nr_maps--;
2104 }
2105 if (newmap)
2106 goto done;
2107
2108 /* full maps */
2109 ceph_decode_32_safe(&p, end, nr_maps, bad);
2110 dout(" %d full maps\n", nr_maps);
2111 while (nr_maps) {
2112 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002113 epoch = ceph_decode_32(&p);
2114 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002115 ceph_decode_need(&p, end, maplen, bad);
2116 if (nr_maps > 1) {
2117 dout("skipping non-latest full map %u len %d\n",
2118 epoch, maplen);
2119 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2120 dout("skipping full map %u len %d, "
2121 "older than our %u\n", epoch, maplen,
2122 osdc->osdmap->epoch);
2123 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002124 int skipped_map = 0;
2125
Sage Weilf24e9982009-10-06 11:31:10 -07002126 dout("taking full map %u len %d\n", epoch, maplen);
Ilya Dryomova2505d62014-03-13 16:36:13 +02002127 newmap = ceph_osdmap_decode(&p, p+maplen);
Sage Weilf24e9982009-10-06 11:31:10 -07002128 if (IS_ERR(newmap)) {
2129 err = PTR_ERR(newmap);
2130 goto bad;
2131 }
Sage Weil30dc6382009-12-21 14:49:37 -08002132 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002133 oldmap = osdc->osdmap;
2134 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002135 if (oldmap) {
2136 if (oldmap->epoch + 1 < newmap->epoch)
2137 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002138 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002139 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002140 was_full = was_full ||
2141 ceph_osdmap_flag(osdc->osdmap,
2142 CEPH_OSDMAP_FULL);
2143 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002144 }
2145 p += maplen;
2146 nr_maps--;
2147 }
2148
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002149 if (!osdc->osdmap)
2150 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002151done:
2152 downgrade_write(&osdc->map_sem);
2153 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002154
2155 /*
2156 * subscribe to subsequent osdmap updates if full to ensure
2157 * we find out when we are no longer full and stop returning
2158 * ENOSPC.
2159 */
Josh Durgind29adb32013-12-02 19:11:48 -08002160 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2161 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2162 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002163 ceph_monc_request_next_osdmap(&osdc->client->monc);
2164
Alex Elderf9d25192013-02-15 11:42:29 -06002165 mutex_lock(&osdc->request_mutex);
2166 __send_queued(osdc);
2167 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002168 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002169 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002170 return;
2171
2172bad:
2173 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002174 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002175 up_write(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07002176}
2177
Sage Weilf24e9982009-10-06 11:31:10 -07002178/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002179 * watch/notify callback event infrastructure
2180 *
2181 * These callbacks are used both for watch and notify operations.
2182 */
2183static void __release_event(struct kref *kref)
2184{
2185 struct ceph_osd_event *event =
2186 container_of(kref, struct ceph_osd_event, kref);
2187
2188 dout("__release_event %p\n", event);
2189 kfree(event);
2190}
2191
2192static void get_event(struct ceph_osd_event *event)
2193{
2194 kref_get(&event->kref);
2195}
2196
2197void ceph_osdc_put_event(struct ceph_osd_event *event)
2198{
2199 kref_put(&event->kref, __release_event);
2200}
2201EXPORT_SYMBOL(ceph_osdc_put_event);
2202
2203static void __insert_event(struct ceph_osd_client *osdc,
2204 struct ceph_osd_event *new)
2205{
2206 struct rb_node **p = &osdc->event_tree.rb_node;
2207 struct rb_node *parent = NULL;
2208 struct ceph_osd_event *event = NULL;
2209
2210 while (*p) {
2211 parent = *p;
2212 event = rb_entry(parent, struct ceph_osd_event, node);
2213 if (new->cookie < event->cookie)
2214 p = &(*p)->rb_left;
2215 else if (new->cookie > event->cookie)
2216 p = &(*p)->rb_right;
2217 else
2218 BUG();
2219 }
2220
2221 rb_link_node(&new->node, parent, p);
2222 rb_insert_color(&new->node, &osdc->event_tree);
2223}
2224
2225static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2226 u64 cookie)
2227{
2228 struct rb_node **p = &osdc->event_tree.rb_node;
2229 struct rb_node *parent = NULL;
2230 struct ceph_osd_event *event = NULL;
2231
2232 while (*p) {
2233 parent = *p;
2234 event = rb_entry(parent, struct ceph_osd_event, node);
2235 if (cookie < event->cookie)
2236 p = &(*p)->rb_left;
2237 else if (cookie > event->cookie)
2238 p = &(*p)->rb_right;
2239 else
2240 return event;
2241 }
2242 return NULL;
2243}
2244
2245static void __remove_event(struct ceph_osd_event *event)
2246{
2247 struct ceph_osd_client *osdc = event->osdc;
2248
2249 if (!RB_EMPTY_NODE(&event->node)) {
2250 dout("__remove_event removed %p\n", event);
2251 rb_erase(&event->node, &osdc->event_tree);
2252 ceph_osdc_put_event(event);
2253 } else {
2254 dout("__remove_event didn't remove %p\n", event);
2255 }
2256}
2257
2258int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2259 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002260 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002261{
2262 struct ceph_osd_event *event;
2263
2264 event = kmalloc(sizeof(*event), GFP_NOIO);
2265 if (!event)
2266 return -ENOMEM;
2267
2268 dout("create_event %p\n", event);
2269 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002270 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002271 event->data = data;
2272 event->osdc = osdc;
2273 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002274 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002275 kref_init(&event->kref); /* one ref for us */
2276 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002277
2278 spin_lock(&osdc->event_lock);
2279 event->cookie = ++osdc->event_count;
2280 __insert_event(osdc, event);
2281 spin_unlock(&osdc->event_lock);
2282
2283 *pevent = event;
2284 return 0;
2285}
2286EXPORT_SYMBOL(ceph_osdc_create_event);
2287
2288void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2289{
2290 struct ceph_osd_client *osdc = event->osdc;
2291
2292 dout("cancel_event %p\n", event);
2293 spin_lock(&osdc->event_lock);
2294 __remove_event(event);
2295 spin_unlock(&osdc->event_lock);
2296 ceph_osdc_put_event(event); /* caller's */
2297}
2298EXPORT_SYMBOL(ceph_osdc_cancel_event);
2299
2300
2301static void do_event_work(struct work_struct *work)
2302{
2303 struct ceph_osd_event_work *event_work =
2304 container_of(work, struct ceph_osd_event_work, work);
2305 struct ceph_osd_event *event = event_work->event;
2306 u64 ver = event_work->ver;
2307 u64 notify_id = event_work->notify_id;
2308 u8 opcode = event_work->opcode;
2309
2310 dout("do_event_work completing %p\n", event);
2311 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002312 dout("do_event_work completed %p\n", event);
2313 ceph_osdc_put_event(event);
2314 kfree(event_work);
2315}
2316
2317
2318/*
2319 * Process osd watch notifications
2320 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002321static void handle_watch_notify(struct ceph_osd_client *osdc,
2322 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002323{
2324 void *p, *end;
2325 u8 proto_ver;
2326 u64 cookie, ver, notify_id;
2327 u8 opcode;
2328 struct ceph_osd_event *event;
2329 struct ceph_osd_event_work *event_work;
2330
2331 p = msg->front.iov_base;
2332 end = p + msg->front.iov_len;
2333
2334 ceph_decode_8_safe(&p, end, proto_ver, bad);
2335 ceph_decode_8_safe(&p, end, opcode, bad);
2336 ceph_decode_64_safe(&p, end, cookie, bad);
2337 ceph_decode_64_safe(&p, end, ver, bad);
2338 ceph_decode_64_safe(&p, end, notify_id, bad);
2339
2340 spin_lock(&osdc->event_lock);
2341 event = __find_event(osdc, cookie);
2342 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002343 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002344 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002345 }
2346 spin_unlock(&osdc->event_lock);
2347 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2348 cookie, ver, event);
2349 if (event) {
2350 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002351 if (!event_work) {
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002352 pr_err("couldn't allocate event_work\n");
2353 ceph_osdc_put_event(event);
2354 return;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002355 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002356 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002357 event_work->event = event;
2358 event_work->ver = ver;
2359 event_work->notify_id = notify_id;
2360 event_work->opcode = opcode;
Ilya Dryomov91883cd2014-09-11 12:18:53 +04002361
2362 queue_work(osdc->notify_wq, &event_work->work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002363 }
2364
2365 return;
2366
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002367bad:
2368 pr_err("osdc handle_watch_notify corrupt msg\n");
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002369}
2370
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002371/*
Alex Eldere65550f2013-04-05 01:27:12 -05002372 * build new request AND message
2373 *
2374 */
2375void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2376 struct ceph_snap_context *snapc, u64 snap_id,
2377 struct timespec *mtime)
2378{
2379 struct ceph_msg *msg = req->r_request;
2380 void *p;
2381 size_t msg_size;
2382 int flags = req->r_flags;
2383 u64 data_len;
2384 unsigned int i;
2385
2386 req->r_snapid = snap_id;
2387 req->r_snapc = ceph_get_snap_context(snapc);
2388
2389 /* encode request */
2390 msg->hdr.version = cpu_to_le16(4);
2391
2392 p = msg->front.iov_base;
2393 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2394 req->r_request_osdmap_epoch = p;
2395 p += 4;
2396 req->r_request_flags = p;
2397 p += 4;
2398 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2399 ceph_encode_timespec(p, mtime);
2400 p += sizeof(struct ceph_timespec);
2401 req->r_request_reassert_version = p;
2402 p += sizeof(struct ceph_eversion); /* will get filled in */
2403
2404 /* oloc */
2405 ceph_encode_8(&p, 4);
2406 ceph_encode_8(&p, 4);
2407 ceph_encode_32(&p, 8 + 4 + 4);
2408 req->r_request_pool = p;
2409 p += 8;
2410 ceph_encode_32(&p, -1); /* preferred */
2411 ceph_encode_32(&p, 0); /* key len */
2412
2413 ceph_encode_8(&p, 1);
2414 req->r_request_pgid = p;
2415 p += 8 + 4;
2416 ceph_encode_32(&p, -1); /* preferred */
2417
2418 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002419 ceph_encode_32(&p, req->r_base_oid.name_len);
2420 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2421 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2422 req->r_base_oid.name, req->r_base_oid.name_len);
2423 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002424
2425 /* ops--can imply data */
2426 ceph_encode_16(&p, (u16)req->r_num_ops);
2427 data_len = 0;
2428 for (i = 0; i < req->r_num_ops; i++) {
2429 data_len += osd_req_encode_op(req, p, i);
2430 p += sizeof(struct ceph_osd_op);
2431 }
2432
2433 /* snaps */
2434 ceph_encode_64(&p, req->r_snapid);
2435 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2436 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2437 if (req->r_snapc) {
2438 for (i = 0; i < snapc->num_snaps; i++) {
2439 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2440 }
2441 }
2442
2443 req->r_request_attempts = p;
2444 p += 4;
2445
2446 /* data */
2447 if (flags & CEPH_OSD_FLAG_WRITE) {
2448 u16 data_off;
2449
2450 /*
2451 * The header "data_off" is a hint to the receiver
2452 * allowing it to align received data into its
2453 * buffers such that there's no need to re-copy
2454 * it before writing it to disk (direct I/O).
2455 */
2456 data_off = (u16) (off & 0xffff);
2457 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2458 }
2459 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2460
2461 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2462 msg_size = p - msg->front.iov_base;
2463 msg->front.iov_len = msg_size;
2464 msg->hdr.front_len = cpu_to_le32(msg_size);
2465
2466 dout("build_request msg_size was %d\n", (int)msg_size);
2467}
2468EXPORT_SYMBOL(ceph_osdc_build_request);
2469
2470/*
Sage Weilf24e9982009-10-06 11:31:10 -07002471 * Register request, send initial attempt.
2472 */
2473int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2474 struct ceph_osd_request *req,
2475 bool nofail)
2476{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002477 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002478
Sage Weilf24e9982009-10-06 11:31:10 -07002479 down_read(&osdc->map_sem);
2480 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002481
2482 rc = __ceph_osdc_start_request(osdc, req, nofail);
2483
Sage Weilf24e9982009-10-06 11:31:10 -07002484 mutex_unlock(&osdc->request_mutex);
2485 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002486
Sage Weilf24e9982009-10-06 11:31:10 -07002487 return rc;
2488}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002489EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002490
2491/*
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002492 * Unregister a registered request. The request is not completed (i.e.
2493 * no callbacks or wakeups) - higher layers are supposed to know what
2494 * they are canceling.
2495 */
2496void ceph_osdc_cancel_request(struct ceph_osd_request *req)
2497{
2498 struct ceph_osd_client *osdc = req->r_osdc;
2499
2500 mutex_lock(&osdc->request_mutex);
2501 if (req->r_linger)
2502 __unregister_linger_request(osdc, req);
2503 __unregister_request(osdc, req);
2504 mutex_unlock(&osdc->request_mutex);
2505
2506 dout("%s %p tid %llu canceled\n", __func__, req, req->r_tid);
2507}
2508EXPORT_SYMBOL(ceph_osdc_cancel_request);
2509
2510/*
Sage Weilf24e9982009-10-06 11:31:10 -07002511 * wait for a request to complete
2512 */
2513int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2514 struct ceph_osd_request *req)
2515{
2516 int rc;
2517
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002518 dout("%s %p tid %llu\n", __func__, req, req->r_tid);
2519
Sage Weilf24e9982009-10-06 11:31:10 -07002520 rc = wait_for_completion_interruptible(&req->r_completion);
2521 if (rc < 0) {
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002522 dout("%s %p tid %llu interrupted\n", __func__, req, req->r_tid);
2523 ceph_osdc_cancel_request(req);
Sage Weil25845472011-06-03 09:37:09 -07002524 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002525 return rc;
2526 }
2527
Ilya Dryomovc9f9b93d2014-06-19 11:38:13 +04002528 dout("%s %p tid %llu result %d\n", __func__, req, req->r_tid,
2529 req->r_result);
Sage Weilf24e9982009-10-06 11:31:10 -07002530 return req->r_result;
2531}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002532EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002533
2534/*
2535 * sync - wait for all in-flight requests to flush. avoid starvation.
2536 */
2537void ceph_osdc_sync(struct ceph_osd_client *osdc)
2538{
2539 struct ceph_osd_request *req;
2540 u64 last_tid, next_tid = 0;
2541
2542 mutex_lock(&osdc->request_mutex);
2543 last_tid = osdc->last_tid;
2544 while (1) {
2545 req = __lookup_request_ge(osdc, next_tid);
2546 if (!req)
2547 break;
2548 if (req->r_tid > last_tid)
2549 break;
2550
2551 next_tid = req->r_tid + 1;
2552 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2553 continue;
2554
2555 ceph_osdc_get_request(req);
2556 mutex_unlock(&osdc->request_mutex);
2557 dout("sync waiting on tid %llu (last is %llu)\n",
2558 req->r_tid, last_tid);
2559 wait_for_completion(&req->r_safe_completion);
2560 mutex_lock(&osdc->request_mutex);
2561 ceph_osdc_put_request(req);
2562 }
2563 mutex_unlock(&osdc->request_mutex);
2564 dout("sync done (thru tid %llu)\n", last_tid);
2565}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002566EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002567
2568/*
Josh Durgindd935f42013-08-28 21:43:09 -07002569 * Call all pending notify callbacks - for use after a watch is
2570 * unregistered, to make sure no more callbacks for it will be invoked
2571 */
stephen hemmingerf64794492014-06-10 20:30:13 -07002572void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
Josh Durgindd935f42013-08-28 21:43:09 -07002573{
2574 flush_workqueue(osdc->notify_wq);
2575}
2576EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2577
2578
2579/*
Sage Weilf24e9982009-10-06 11:31:10 -07002580 * init, shutdown
2581 */
2582int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2583{
2584 int err;
2585
2586 dout("init\n");
2587 osdc->client = client;
2588 osdc->osdmap = NULL;
2589 init_rwsem(&osdc->map_sem);
2590 init_completion(&osdc->map_waiters);
2591 osdc->last_requested_map = 0;
2592 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002593 osdc->last_tid = 0;
2594 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002595 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002596 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002597 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002598 INIT_LIST_HEAD(&osdc->req_unsent);
2599 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002600 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002601 osdc->num_requests = 0;
2602 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002603 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002604 spin_lock_init(&osdc->event_lock);
2605 osdc->event_tree = RB_ROOT;
2606 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002607
2608 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002609 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002610
Sage Weil5f44f142009-11-18 14:52:18 -08002611 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002612 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2613 sizeof(struct ceph_osd_request));
2614 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002615 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002616
Sage Weild50b4092012-07-09 14:22:34 -07002617 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2618 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002619 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002620 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002621 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002622 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002623 OSD_OPREPLY_FRONT_LEN, 10, true,
2624 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002625 if (err < 0)
2626 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002627
Dan Carpenterdbcae082013-08-15 08:58:59 +03002628 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002629 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002630 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002631 goto out_msgpool_reply;
2632
Sage Weilf24e9982009-10-06 11:31:10 -07002633 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002634
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002635out_msgpool_reply:
2636 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002637out_msgpool:
2638 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002639out_mempool:
2640 mempool_destroy(osdc->req_mempool);
2641out:
2642 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002643}
2644
2645void ceph_osdc_stop(struct ceph_osd_client *osdc)
2646{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002647 flush_workqueue(osdc->notify_wq);
2648 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002649 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002650 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002651 if (osdc->osdmap) {
2652 ceph_osdmap_destroy(osdc->osdmap);
2653 osdc->osdmap = NULL;
2654 }
Sage Weilaca420b2011-08-31 14:45:53 -07002655 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002656 mempool_destroy(osdc->req_mempool);
2657 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002658 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002659}
2660
2661/*
2662 * Read some contiguous pages. If we cross a stripe boundary, shorten
2663 * *plen. Return number of bytes read, or error.
2664 */
2665int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2666 struct ceph_vino vino, struct ceph_file_layout *layout,
2667 u64 off, u64 *plen,
2668 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002669 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002670{
2671 struct ceph_osd_request *req;
2672 int rc = 0;
2673
2674 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2675 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002676 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002677 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002678 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002679 false);
Sage Weil68162822012-09-24 21:01:02 -07002680 if (IS_ERR(req))
2681 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002682
2683 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002684
Alex Elder406e2c92013-04-15 14:50:36 -05002685 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002686 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002687
Alex Eldere0c59482013-03-07 15:38:25 -06002688 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002689 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002690
Alex Elder79528732013-04-03 21:32:51 -05002691 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002692
Sage Weilf24e9982009-10-06 11:31:10 -07002693 rc = ceph_osdc_start_request(osdc, req, false);
2694 if (!rc)
2695 rc = ceph_osdc_wait_request(osdc, req);
2696
2697 ceph_osdc_put_request(req);
2698 dout("readpages result %d\n", rc);
2699 return rc;
2700}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002701EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002702
2703/*
2704 * do a synchronous write on N pages
2705 */
2706int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2707 struct ceph_file_layout *layout,
2708 struct ceph_snap_context *snapc,
2709 u64 off, u64 len,
2710 u32 truncate_seq, u64 truncate_size,
2711 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002712 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002713{
2714 struct ceph_osd_request *req;
2715 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002716 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002717
Alex Elderacead002013-03-14 14:09:05 -05002718 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002719 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002720 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002721 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002722 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002723 true);
Sage Weil68162822012-09-24 21:01:02 -07002724 if (IS_ERR(req))
2725 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002726
2727 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002728 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002729 false, false);
2730 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002731
Alex Elder79528732013-04-03 21:32:51 -05002732 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002733
Alex Elder87f979d2013-02-15 11:42:29 -06002734 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002735 if (!rc)
2736 rc = ceph_osdc_wait_request(osdc, req);
2737
2738 ceph_osdc_put_request(req);
2739 if (rc == 0)
2740 rc = len;
2741 dout("writepages result %d\n", rc);
2742 return rc;
2743}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002744EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002745
Alex Elder5522ae02013-05-01 12:43:04 -05002746int ceph_osdc_setup(void)
2747{
2748 BUG_ON(ceph_osd_request_cache);
2749 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2750 sizeof (struct ceph_osd_request),
2751 __alignof__(struct ceph_osd_request),
2752 0, NULL);
2753
2754 return ceph_osd_request_cache ? 0 : -ENOMEM;
2755}
2756EXPORT_SYMBOL(ceph_osdc_setup);
2757
2758void ceph_osdc_cleanup(void)
2759{
2760 BUG_ON(!ceph_osd_request_cache);
2761 kmem_cache_destroy(ceph_osd_request_cache);
2762 ceph_osd_request_cache = NULL;
2763}
2764EXPORT_SYMBOL(ceph_osdc_cleanup);
2765
Sage Weilf24e9982009-10-06 11:31:10 -07002766/*
2767 * handle incoming message
2768 */
2769static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2770{
2771 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002772 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002773 int type = le16_to_cpu(msg->hdr.type);
2774
2775 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002776 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002777 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002778
2779 switch (type) {
2780 case CEPH_MSG_OSD_MAP:
2781 ceph_osdc_handle_map(osdc, msg);
2782 break;
2783 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002784 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002785 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002786 case CEPH_MSG_WATCH_NOTIFY:
2787 handle_watch_notify(osdc, msg);
2788 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002789
2790 default:
2791 pr_err("received unknown message type %d %s\n", type,
2792 ceph_msg_type_name(type));
2793 }
Sage Weil4a32f932010-06-13 10:27:53 -07002794out:
Sage Weilf24e9982009-10-06 11:31:10 -07002795 ceph_msg_put(msg);
2796}
2797
Sage Weil5b3a4db2010-02-19 21:43:23 -08002798/*
Sage Weil21b667f2010-03-04 10:22:59 -08002799 * lookup and return message for incoming reply. set up reply message
2800 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002801 */
2802static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002803 struct ceph_msg_header *hdr,
2804 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002805{
2806 struct ceph_osd *osd = con->private;
2807 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002808 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002809 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002810 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002811 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002812 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002813
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002814 tid = le64_to_cpu(hdr->tid);
2815 mutex_lock(&osdc->request_mutex);
2816 req = __lookup_request(osdc, tid);
2817 if (!req) {
2818 *skip = 1;
2819 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002820 dout("get_reply unknown tid %llu from osd%d\n", tid,
2821 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002822 goto out;
2823 }
Sage Weilc16e7862010-03-01 13:02:00 -08002824
Alex Elderace6d3a2013-04-01 16:12:14 -05002825 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002826 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002827 req->r_reply, req->r_reply->con);
2828 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002829
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002830 if (front_len > req->r_reply->front_alloc_len) {
Joe Perchesb9a67892014-09-09 21:17:29 -07002831 pr_warn("get_reply front %d > preallocated %d (%u#%llu)\n",
2832 front_len, req->r_reply->front_alloc_len,
2833 (unsigned int)con->peer_name.type,
2834 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002835 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2836 false);
Sage Weila79832f2010-04-01 16:06:19 -07002837 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002838 goto out;
2839 ceph_msg_put(req->r_reply);
2840 req->r_reply = m;
2841 }
2842 m = ceph_msg_get(req->r_reply);
2843
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002844 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002845 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002846
Alex Eldera4ce40a2013-04-05 01:27:12 -05002847 /*
2848 * XXX This is assuming there is only one op containing
2849 * XXX page data. Probably OK for reads, but this
2850 * XXX ought to be done more generally.
2851 */
Alex Elder406e2c92013-04-15 14:50:36 -05002852 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002853 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002854 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002855 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002856
Joe Perchesb9a67892014-09-09 21:17:29 -07002857 pr_warn("tid %lld reply has %d bytes we had only %llu bytes ready\n",
Alex Eldere0c59482013-03-07 15:38:25 -06002858 tid, data_len, osd_data->length);
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002859 *skip = 1;
2860 ceph_msg_put(m);
2861 m = NULL;
2862 goto out;
2863 }
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002864 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002865 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002866 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002867 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002868
2869out:
2870 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002871 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002872
2873}
2874
2875static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2876 struct ceph_msg_header *hdr,
2877 int *skip)
2878{
2879 struct ceph_osd *osd = con->private;
2880 int type = le16_to_cpu(hdr->type);
2881 int front = le32_to_cpu(hdr->front_len);
2882
Alex Elder1c20f2d2012-06-04 14:43:32 -05002883 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002884 switch (type) {
2885 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002886 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002887 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002888 case CEPH_MSG_OSD_OPREPLY:
2889 return get_reply(con, hdr, skip);
2890 default:
2891 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2892 osd->o_osd);
2893 *skip = 1;
2894 return NULL;
2895 }
Sage Weilf24e9982009-10-06 11:31:10 -07002896}
2897
2898/*
2899 * Wrappers to refcount containing ceph_osd struct
2900 */
2901static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2902{
2903 struct ceph_osd *osd = con->private;
2904 if (get_osd(osd))
2905 return con;
2906 return NULL;
2907}
2908
2909static void put_osd_con(struct ceph_connection *con)
2910{
2911 struct ceph_osd *osd = con->private;
2912 put_osd(osd);
2913}
2914
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002915/*
2916 * authentication
2917 */
Alex Eldera3530df2012-05-16 15:16:39 -05002918/*
2919 * Note: returned pointer is the address of a structure that's
2920 * managed separately. Caller must *not* attempt to free it.
2921 */
2922static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002923 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002924{
2925 struct ceph_osd *o = con->private;
2926 struct ceph_osd_client *osdc = o->o_osdc;
2927 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002928 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002929
Alex Elder74f18692012-05-16 15:16:39 -05002930 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002931 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002932 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002933 }
Sage Weil27859f92013-03-25 10:26:14 -07002934 if (!auth->authorizer) {
2935 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2936 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002937 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002938 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002939 } else {
2940 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002941 auth);
2942 if (ret)
2943 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002944 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002945 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002946
Alex Eldera3530df2012-05-16 15:16:39 -05002947 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002948}
2949
2950
2951static int verify_authorizer_reply(struct ceph_connection *con, int len)
2952{
2953 struct ceph_osd *o = con->private;
2954 struct ceph_osd_client *osdc = o->o_osdc;
2955 struct ceph_auth_client *ac = osdc->client->monc.auth;
2956
Sage Weil27859f92013-03-25 10:26:14 -07002957 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002958}
2959
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002960static int invalidate_authorizer(struct ceph_connection *con)
2961{
2962 struct ceph_osd *o = con->private;
2963 struct ceph_osd_client *osdc = o->o_osdc;
2964 struct ceph_auth_client *ac = osdc->client->monc.auth;
2965
Sage Weil27859f92013-03-25 10:26:14 -07002966 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002967 return ceph_monc_validate_auth(&osdc->client->monc);
2968}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002969
Yan, Zheng33d07332014-11-04 16:33:37 +08002970static int sign_message(struct ceph_connection *con, struct ceph_msg *msg)
2971{
2972 struct ceph_osd *o = con->private;
2973 struct ceph_auth_handshake *auth = &o->o_auth;
2974 return ceph_auth_sign_message(auth, msg);
2975}
2976
2977static int check_message_signature(struct ceph_connection *con, struct ceph_msg *msg)
2978{
2979 struct ceph_osd *o = con->private;
2980 struct ceph_auth_handshake *auth = &o->o_auth;
2981 return ceph_auth_check_message_signature(auth, msg);
2982}
2983
Tobias Klauser9e327892010-05-20 10:40:19 +02002984static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002985 .get = get_osd_con,
2986 .put = put_osd_con,
2987 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002988 .get_authorizer = get_authorizer,
2989 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002990 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002991 .alloc_msg = alloc_msg,
Yan, Zheng33d07332014-11-04 16:33:37 +08002992 .sign_message = sign_message,
2993 .check_message_signature = check_message_signature,
Sage Weil81b024e2009-10-09 10:29:18 -07002994 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002995};