blob: 5d7fd0b8c1c8024c665df1a3e3ad6bd06f01ab06 [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);
33static void __unregister_linger_request(struct ceph_osd_client *osdc,
34 struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080035static void __send_request(struct ceph_osd_client *osdc,
36 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070037
38/*
39 * Implement client access to distributed object storage cluster.
40 *
41 * All data objects are stored within a cluster/cloud of OSDs, or
42 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
43 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
44 * remote daemons serving up and coordinating consistent and safe
45 * access to storage.
46 *
47 * Cluster membership and the mapping of data objects onto storage devices
48 * are described by the osd map.
49 *
50 * We keep track of pending OSD requests (read, write), resubmit
51 * requests to different OSDs when the cluster topology/data layout
52 * change, or retry the affected requests when the communications
53 * channel with an OSD is reset.
54 */
55
56/*
57 * calculate the mapping of a file extent onto an object, and fill out the
58 * request accordingly. shorten extent as necessary if it crosses an
59 * object boundary.
60 *
61 * fill osd op in request message.
62 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060063static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050064 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070065{
Alex Elder60e56f12013-02-15 11:42:29 -060066 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070067 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070068
Alex Elder60e56f12013-02-15 11:42:29 -060069 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050070 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
71 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070072 if (r < 0)
73 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050074 if (*objlen < orig_len) {
75 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060076 dout(" skipping last %llu, final file extent %llu~%llu\n",
77 orig_len - *plen, off, *plen);
78 }
79
Alex Elder75d1c942013-03-13 20:50:00 -050080 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070081
Alex Elder3ff5f382013-02-15 22:10:17 -060082 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070083}
84
Alex Elderc54d47b2013-04-03 01:28:57 -050085static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
86{
87 memset(osd_data, 0, sizeof (*osd_data));
88 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
89}
90
Alex Eldera4ce40a2013-04-05 01:27:12 -050091static void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -050092 struct page **pages, u64 length, u32 alignment,
93 bool pages_from_pool, bool own_pages)
94{
95 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
96 osd_data->pages = pages;
97 osd_data->length = length;
98 osd_data->alignment = alignment;
99 osd_data->pages_from_pool = pages_from_pool;
100 osd_data->own_pages = own_pages;
101}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500102
Alex Eldera4ce40a2013-04-05 01:27:12 -0500103static void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500104 struct ceph_pagelist *pagelist)
105{
106 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
107 osd_data->pagelist = pagelist;
108}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500109
110#ifdef CONFIG_BLOCK
Alex Eldera4ce40a2013-04-05 01:27:12 -0500111static void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
Alex Elder43bfe5d2013-04-03 01:28:57 -0500112 struct bio *bio, size_t bio_length)
113{
114 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
115 osd_data->bio = bio;
116 osd_data->bio_length = bio_length;
117}
Alex Elder43bfe5d2013-04-03 01:28:57 -0500118#endif /* CONFIG_BLOCK */
119
Alex Elder863c7eb2013-04-15 14:50:36 -0500120#define osd_req_op_data(oreq, whch, typ, fld) \
121 ({ \
122 BUG_ON(whch >= (oreq)->r_num_ops); \
123 &(oreq)->r_ops[whch].typ.fld; \
124 })
125
Alex Elder49719772013-02-11 12:33:24 -0600126static struct ceph_osd_data *
127osd_req_op_raw_data_in(struct ceph_osd_request *osd_req, unsigned int which)
128{
129 BUG_ON(which >= osd_req->r_num_ops);
130
131 return &osd_req->r_ops[which].raw_data_in;
132}
133
Alex Eldera4ce40a2013-04-05 01:27:12 -0500134struct ceph_osd_data *
135osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500136 unsigned int which)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500137{
Alex Elder863c7eb2013-04-15 14:50:36 -0500138 return osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500139}
140EXPORT_SYMBOL(osd_req_op_extent_osd_data);
141
142struct ceph_osd_data *
Alex Eldera4ce40a2013-04-05 01:27:12 -0500143osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
144 unsigned int which)
145{
Alex Elder863c7eb2013-04-15 14:50:36 -0500146 return osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500147}
148EXPORT_SYMBOL(osd_req_op_cls_response_data); /* ??? */
149
Alex Elder49719772013-02-11 12:33:24 -0600150void osd_req_op_raw_data_in_pages(struct ceph_osd_request *osd_req,
151 unsigned int which, struct page **pages,
152 u64 length, u32 alignment,
153 bool pages_from_pool, bool own_pages)
154{
155 struct ceph_osd_data *osd_data;
156
157 osd_data = osd_req_op_raw_data_in(osd_req, which);
158 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
159 pages_from_pool, own_pages);
160}
161EXPORT_SYMBOL(osd_req_op_raw_data_in_pages);
162
Alex Eldera4ce40a2013-04-05 01:27:12 -0500163void osd_req_op_extent_osd_data_pages(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500164 unsigned int which, struct page **pages,
165 u64 length, u32 alignment,
Alex Eldera4ce40a2013-04-05 01:27:12 -0500166 bool pages_from_pool, bool own_pages)
167{
168 struct ceph_osd_data *osd_data;
169
Alex Elder863c7eb2013-04-15 14:50:36 -0500170 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500171 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
172 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500173}
174EXPORT_SYMBOL(osd_req_op_extent_osd_data_pages);
175
176void osd_req_op_extent_osd_data_pagelist(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500177 unsigned int which, struct ceph_pagelist *pagelist)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500178{
179 struct ceph_osd_data *osd_data;
180
Alex Elder863c7eb2013-04-15 14:50:36 -0500181 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500182 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500183}
184EXPORT_SYMBOL(osd_req_op_extent_osd_data_pagelist);
185
186#ifdef CONFIG_BLOCK
187void osd_req_op_extent_osd_data_bio(struct ceph_osd_request *osd_req,
Alex Elder406e2c92013-04-15 14:50:36 -0500188 unsigned int which, struct bio *bio, size_t bio_length)
Alex Eldera4ce40a2013-04-05 01:27:12 -0500189{
190 struct ceph_osd_data *osd_data;
Alex Elder863c7eb2013-04-15 14:50:36 -0500191
192 osd_data = osd_req_op_data(osd_req, which, extent, osd_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500193 ceph_osd_data_bio_init(osd_data, bio, bio_length);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500194}
195EXPORT_SYMBOL(osd_req_op_extent_osd_data_bio);
196#endif /* CONFIG_BLOCK */
197
198static void osd_req_op_cls_request_info_pagelist(
199 struct ceph_osd_request *osd_req,
200 unsigned int which, struct ceph_pagelist *pagelist)
201{
202 struct ceph_osd_data *osd_data;
203
Alex Elder863c7eb2013-04-15 14:50:36 -0500204 osd_data = osd_req_op_data(osd_req, which, cls, request_info);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500205 ceph_osd_data_pagelist_init(osd_data, pagelist);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500206}
207
Alex Elder04017e22013-04-05 14:46:02 -0500208void osd_req_op_cls_request_data_pagelist(
209 struct ceph_osd_request *osd_req,
210 unsigned int which, struct ceph_pagelist *pagelist)
211{
212 struct ceph_osd_data *osd_data;
213
Alex Elder863c7eb2013-04-15 14:50:36 -0500214 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
Alex Elder04017e22013-04-05 14:46:02 -0500215 ceph_osd_data_pagelist_init(osd_data, pagelist);
216}
217EXPORT_SYMBOL(osd_req_op_cls_request_data_pagelist);
218
Alex Elder6c57b552013-04-19 15:34:49 -0500219void osd_req_op_cls_request_data_pages(struct ceph_osd_request *osd_req,
220 unsigned int which, struct page **pages, u64 length,
221 u32 alignment, bool pages_from_pool, bool own_pages)
222{
223 struct ceph_osd_data *osd_data;
224
225 osd_data = osd_req_op_data(osd_req, which, cls, request_data);
226 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
227 pages_from_pool, own_pages);
228}
229EXPORT_SYMBOL(osd_req_op_cls_request_data_pages);
230
Alex Eldera4ce40a2013-04-05 01:27:12 -0500231void osd_req_op_cls_response_data_pages(struct ceph_osd_request *osd_req,
232 unsigned int which, struct page **pages, u64 length,
233 u32 alignment, bool pages_from_pool, bool own_pages)
234{
235 struct ceph_osd_data *osd_data;
236
Alex Elder863c7eb2013-04-15 14:50:36 -0500237 osd_data = osd_req_op_data(osd_req, which, cls, response_data);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500238 ceph_osd_data_pages_init(osd_data, pages, length, alignment,
239 pages_from_pool, own_pages);
Alex Eldera4ce40a2013-04-05 01:27:12 -0500240}
241EXPORT_SYMBOL(osd_req_op_cls_response_data_pages);
242
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500243static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
244{
245 switch (osd_data->type) {
246 case CEPH_OSD_DATA_TYPE_NONE:
247 return 0;
248 case CEPH_OSD_DATA_TYPE_PAGES:
249 return osd_data->length;
250 case CEPH_OSD_DATA_TYPE_PAGELIST:
251 return (u64)osd_data->pagelist->length;
252#ifdef CONFIG_BLOCK
253 case CEPH_OSD_DATA_TYPE_BIO:
254 return (u64)osd_data->bio_length;
255#endif /* CONFIG_BLOCK */
256 default:
257 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
258 return 0;
259 }
260}
261
Alex Elderc54d47b2013-04-03 01:28:57 -0500262static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
263{
Alex Elder54764922013-04-05 01:27:12 -0500264 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES && osd_data->own_pages) {
Alex Elderc54d47b2013-04-03 01:28:57 -0500265 int num_pages;
266
267 num_pages = calc_pages_for((u64)osd_data->alignment,
268 (u64)osd_data->length);
269 ceph_release_page_vector(osd_data->pages, num_pages);
270 }
Alex Elder54764922013-04-05 01:27:12 -0500271 ceph_osd_data_init(osd_data);
272}
273
274static void osd_req_op_data_release(struct ceph_osd_request *osd_req,
275 unsigned int which)
276{
277 struct ceph_osd_req_op *op;
278
279 BUG_ON(which >= osd_req->r_num_ops);
280 op = &osd_req->r_ops[which];
281
282 switch (op->op) {
283 case CEPH_OSD_OP_READ:
284 case CEPH_OSD_OP_WRITE:
285 ceph_osd_data_release(&op->extent.osd_data);
286 break;
287 case CEPH_OSD_OP_CALL:
288 ceph_osd_data_release(&op->cls.request_info);
Alex Elder04017e22013-04-05 14:46:02 -0500289 ceph_osd_data_release(&op->cls.request_data);
Alex Elder54764922013-04-05 01:27:12 -0500290 ceph_osd_data_release(&op->cls.response_data);
291 break;
292 default:
293 break;
294 }
Alex Elderc54d47b2013-04-03 01:28:57 -0500295}
296
Sage Weilf24e9982009-10-06 11:31:10 -0700297/*
298 * requests
299 */
Sage Weil415e49a2009-12-07 13:37:03 -0800300void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700301{
Alex Elderc54d47b2013-04-03 01:28:57 -0500302 struct ceph_osd_request *req;
Alex Elder54764922013-04-05 01:27:12 -0500303 unsigned int which;
Sage Weil415e49a2009-12-07 13:37:03 -0800304
Alex Elderc54d47b2013-04-03 01:28:57 -0500305 req = container_of(kref, struct ceph_osd_request, r_kref);
Sage Weil415e49a2009-12-07 13:37:03 -0800306 if (req->r_request)
307 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500308 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500309 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500310 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500311 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600312
Alex Elder54764922013-04-05 01:27:12 -0500313 for (which = 0; which < req->r_num_ops; which++)
314 osd_req_op_data_release(req, which);
Alex Elder0fff87e2013-02-14 12:16:43 -0600315
Sage Weil415e49a2009-12-07 13:37:03 -0800316 ceph_put_snap_context(req->r_snapc);
317 if (req->r_mempool)
318 mempool_free(req, req->r_osdc->req_mempool);
319 else
Alex Elder5522ae02013-05-01 12:43:04 -0500320 kmem_cache_free(ceph_osd_request_cache, req);
321
Sage Weilf24e9982009-10-06 11:31:10 -0700322}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700323EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700324
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700325struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700326 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800327 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700328 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600329 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700330{
331 struct ceph_osd_request *req;
332 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800333 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700334
Alex Elder79528732013-04-03 21:32:51 -0500335 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
336 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
337
Sage Weil1b83bef2013-02-25 16:11:12 -0800338 msg_size = 4 + 4 + 8 + 8 + 4+8;
339 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
340 msg_size += 1 + 8 + 4 + 4; /* pg_t */
Ilya Dryomov2d0ebc52014-01-27 17:40:18 +0200341 msg_size += 4 + CEPH_MAX_OID_NAME_LEN; /* oid */
Sage Weil1b83bef2013-02-25 16:11:12 -0800342 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
343 msg_size += 8; /* snapid */
344 msg_size += 8; /* snap_seq */
345 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
346 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700347
348 if (use_mempool) {
349 req = mempool_alloc(osdc->req_mempool, gfp_flags);
350 memset(req, 0, sizeof(*req));
351 } else {
Alex Elder5522ae02013-05-01 12:43:04 -0500352 req = kmem_cache_zalloc(ceph_osd_request_cache, gfp_flags);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700353 }
354 if (req == NULL)
355 return NULL;
356
357 req->r_osdc = osdc;
358 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500359 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700360
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361 kref_init(&req->r_kref);
362 init_completion(&req->r_completion);
363 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600364 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700366 INIT_LIST_HEAD(&req->r_linger_item);
367 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700368 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700369 INIT_LIST_HEAD(&req->r_osd_item);
370
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200371 req->r_base_oloc.pool = -1;
Ilya Dryomov205ee1182014-01-27 17:40:20 +0200372 req->r_target_oloc.pool = -1;
Ilya Dryomov221165252014-01-27 17:40:18 +0200373
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374 /* create reply message */
375 if (use_mempool)
376 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
377 else
378 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700379 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700380 if (!msg) {
381 ceph_osdc_put_request(req);
382 return NULL;
383 }
384 req->r_reply = msg;
385
386 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387 if (use_mempool)
388 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
389 else
Sage Weilb61c2762011-08-09 15:03:46 -0700390 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700391 if (!msg) {
392 ceph_osdc_put_request(req);
393 return NULL;
394 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700395
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700396 memset(msg->front.iov_base, 0, msg->front.iov_len);
397
398 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700399
400 return req;
401}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700402EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700403
Alex Eldera8dd0a32013-03-13 20:50:00 -0500404static bool osd_req_opcode_valid(u16 opcode)
405{
406 switch (opcode) {
407 case CEPH_OSD_OP_READ:
408 case CEPH_OSD_OP_STAT:
409 case CEPH_OSD_OP_MAPEXT:
410 case CEPH_OSD_OP_MASKTRUNC:
411 case CEPH_OSD_OP_SPARSE_READ:
412 case CEPH_OSD_OP_NOTIFY:
413 case CEPH_OSD_OP_NOTIFY_ACK:
414 case CEPH_OSD_OP_ASSERT_VER:
415 case CEPH_OSD_OP_WRITE:
416 case CEPH_OSD_OP_WRITEFULL:
417 case CEPH_OSD_OP_TRUNCATE:
418 case CEPH_OSD_OP_ZERO:
419 case CEPH_OSD_OP_DELETE:
420 case CEPH_OSD_OP_APPEND:
421 case CEPH_OSD_OP_STARTSYNC:
422 case CEPH_OSD_OP_SETTRUNC:
423 case CEPH_OSD_OP_TRIMTRUNC:
424 case CEPH_OSD_OP_TMAPUP:
425 case CEPH_OSD_OP_TMAPPUT:
426 case CEPH_OSD_OP_TMAPGET:
427 case CEPH_OSD_OP_CREATE:
428 case CEPH_OSD_OP_ROLLBACK:
429 case CEPH_OSD_OP_WATCH:
430 case CEPH_OSD_OP_OMAPGETKEYS:
431 case CEPH_OSD_OP_OMAPGETVALS:
432 case CEPH_OSD_OP_OMAPGETHEADER:
433 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
434 case CEPH_OSD_OP_OMAPSETVALS:
435 case CEPH_OSD_OP_OMAPSETHEADER:
436 case CEPH_OSD_OP_OMAPCLEAR:
437 case CEPH_OSD_OP_OMAPRMKEYS:
438 case CEPH_OSD_OP_OMAP_CMP:
439 case CEPH_OSD_OP_CLONERANGE:
440 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
441 case CEPH_OSD_OP_SRC_CMPXATTR:
442 case CEPH_OSD_OP_GETXATTR:
443 case CEPH_OSD_OP_GETXATTRS:
444 case CEPH_OSD_OP_CMPXATTR:
445 case CEPH_OSD_OP_SETXATTR:
446 case CEPH_OSD_OP_SETXATTRS:
447 case CEPH_OSD_OP_RESETXATTRS:
448 case CEPH_OSD_OP_RMXATTR:
449 case CEPH_OSD_OP_PULL:
450 case CEPH_OSD_OP_PUSH:
451 case CEPH_OSD_OP_BALANCEREADS:
452 case CEPH_OSD_OP_UNBALANCEREADS:
453 case CEPH_OSD_OP_SCRUB:
454 case CEPH_OSD_OP_SCRUB_RESERVE:
455 case CEPH_OSD_OP_SCRUB_UNRESERVE:
456 case CEPH_OSD_OP_SCRUB_STOP:
457 case CEPH_OSD_OP_SCRUB_MAP:
458 case CEPH_OSD_OP_WRLOCK:
459 case CEPH_OSD_OP_WRUNLOCK:
460 case CEPH_OSD_OP_RDLOCK:
461 case CEPH_OSD_OP_RDUNLOCK:
462 case CEPH_OSD_OP_UPLOCK:
463 case CEPH_OSD_OP_DNLOCK:
464 case CEPH_OSD_OP_CALL:
465 case CEPH_OSD_OP_PGLS:
466 case CEPH_OSD_OP_PGLS_FILTER:
467 return true;
468 default:
469 return false;
470 }
471}
472
Alex Elder33803f32013-03-13 20:50:00 -0500473/*
474 * This is an osd op init function for opcodes that have no data or
475 * other information associated with them. It also serves as a
476 * common init routine for all the other init functions, below.
477 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500478static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600479_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500480 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500481{
Alex Elderc99d2d42013-04-05 01:27:11 -0500482 struct ceph_osd_req_op *op;
483
484 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500485 BUG_ON(!osd_req_opcode_valid(opcode));
486
Alex Elderc99d2d42013-04-05 01:27:11 -0500487 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500488 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500489 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500490
491 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500492}
493
Alex Elder49719772013-02-11 12:33:24 -0600494void osd_req_op_init(struct ceph_osd_request *osd_req,
495 unsigned int which, u16 opcode)
496{
497 (void)_osd_req_op_init(osd_req, which, opcode);
498}
499EXPORT_SYMBOL(osd_req_op_init);
500
Alex Elderc99d2d42013-04-05 01:27:11 -0500501void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
502 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500503 u64 offset, u64 length,
504 u64 truncate_size, u32 truncate_seq)
505{
Alex Elder49719772013-02-11 12:33:24 -0600506 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500507 size_t payload_len = 0;
508
Li Wangad7a60d2013-08-15 11:51:44 +0800509 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
510 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
511 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500512
Alex Elder33803f32013-03-13 20:50:00 -0500513 op->extent.offset = offset;
514 op->extent.length = length;
515 op->extent.truncate_size = truncate_size;
516 op->extent.truncate_seq = truncate_seq;
517 if (opcode == CEPH_OSD_OP_WRITE)
518 payload_len += length;
519
520 op->payload_len = payload_len;
521}
522EXPORT_SYMBOL(osd_req_op_extent_init);
523
Alex Elderc99d2d42013-04-05 01:27:11 -0500524void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
525 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500526{
Alex Elderc99d2d42013-04-05 01:27:11 -0500527 struct ceph_osd_req_op *op;
528 u64 previous;
529
530 BUG_ON(which >= osd_req->r_num_ops);
531 op = &osd_req->r_ops[which];
532 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500533
534 if (length == previous)
535 return; /* Nothing to do */
536 BUG_ON(length > previous);
537
538 op->extent.length = length;
539 op->payload_len -= previous - length;
540}
541EXPORT_SYMBOL(osd_req_op_extent_update);
542
Alex Elderc99d2d42013-04-05 01:27:11 -0500543void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500544 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500545{
Alex Elder49719772013-02-11 12:33:24 -0600546 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500547 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500548 size_t payload_len = 0;
549 size_t size;
550
551 BUG_ON(opcode != CEPH_OSD_OP_CALL);
552
Alex Elder5f562df2013-04-05 01:27:12 -0500553 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
554 BUG_ON(!pagelist);
555 ceph_pagelist_init(pagelist);
556
Alex Elder33803f32013-03-13 20:50:00 -0500557 op->cls.class_name = class;
558 size = strlen(class);
559 BUG_ON(size > (size_t) U8_MAX);
560 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500561 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500562 payload_len += size;
563
564 op->cls.method_name = method;
565 size = strlen(method);
566 BUG_ON(size > (size_t) U8_MAX);
567 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500568 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500569 payload_len += size;
570
Alex Eldera4ce40a2013-04-05 01:27:12 -0500571 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500572
Alex Elder33803f32013-03-13 20:50:00 -0500573 op->cls.argc = 0; /* currently unused */
574
575 op->payload_len = payload_len;
576}
577EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500578
Alex Elderc99d2d42013-04-05 01:27:11 -0500579void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
580 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500581 u64 cookie, u64 version, int flag)
582{
Alex Elder49719772013-02-11 12:33:24 -0600583 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500584
Alex Elderc99d2d42013-04-05 01:27:11 -0500585 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500586
587 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500588 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500589 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500590 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500591}
592EXPORT_SYMBOL(osd_req_op_watch_init);
593
Alex Elder90af3602013-04-05 14:46:01 -0500594static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500595 struct ceph_osd_data *osd_data)
596{
597 u64 length = ceph_osd_data_length(osd_data);
598
599 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
600 BUG_ON(length > (u64) SIZE_MAX);
601 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500602 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500603 length, osd_data->alignment);
604 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
605 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500606 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500607#ifdef CONFIG_BLOCK
608 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500609 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500610#endif
611 } else {
612 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
613 }
614}
615
Alex Elder175face2013-03-08 13:35:36 -0600616static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500617 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700618{
Alex Elder79528732013-04-03 21:32:51 -0500619 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500620 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500621 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500622 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600623
Alex Elder79528732013-04-03 21:32:51 -0500624 BUG_ON(which >= req->r_num_ops);
625 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500626 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
627 pr_err("unrecognized osd opcode %d\n", src->op);
628
629 return 0;
630 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700631
Alex Elder065a68f2012-04-20 15:49:43 -0500632 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600633 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600634 osd_data = &src->raw_data_in;
635 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600636 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700637 case CEPH_OSD_OP_READ:
638 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800639 case CEPH_OSD_OP_ZERO:
640 case CEPH_OSD_OP_DELETE:
641 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600642 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500643 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600644 dst->extent.offset = cpu_to_le64(src->extent.offset);
645 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700646 dst->extent.truncate_size =
647 cpu_to_le64(src->extent.truncate_size);
648 dst->extent.truncate_seq =
649 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500650 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500651 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500652 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500653 else
Alex Elder04017e22013-04-05 14:46:02 -0500654 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700655 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700656 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700657 dst->cls.class_len = src->cls.class_len;
658 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500659 osd_data = &src->cls.request_info;
660 ceph_osdc_msg_data_add(req->r_request, osd_data);
661 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
662 request_data_len = osd_data->pagelist->length;
663
664 osd_data = &src->cls.request_data;
665 data_length = ceph_osd_data_length(osd_data);
666 if (data_length) {
667 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
668 dst->cls.indata_len = cpu_to_le32(data_length);
669 ceph_osdc_msg_data_add(req->r_request, osd_data);
670 src->payload_len += data_length;
671 request_data_len += data_length;
672 }
673 osd_data = &src->cls.response_data;
674 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700675 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700676 case CEPH_OSD_OP_STARTSYNC:
677 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700678 case CEPH_OSD_OP_NOTIFY_ACK:
679 case CEPH_OSD_OP_WATCH:
680 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
681 dst->watch.ver = cpu_to_le64(src->watch.ver);
682 dst->watch.flag = src->watch.flag;
683 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700684 default:
Alex Elder4c464592013-02-15 11:42:30 -0600685 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600686 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600687 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500688
689 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700690 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200691
Alex Eldera8dd0a32013-03-13 20:50:00 -0500692 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200693 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700694 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600695
Alex Elder54d50642013-04-03 01:28:58 -0500696 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700697}
698
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700699/*
Sage Weilf24e9982009-10-06 11:31:10 -0700700 * build new request AND message, calculate layout, and adjust file
701 * extent as needed.
702 *
703 * if the file was recently truncated, we include information about its
704 * old and new size so that the object can be updated appropriately. (we
705 * avoid synchronously deleting truncated objects because it's slow.)
706 *
707 * if @do_sync, include a 'startsync' command so that the osd will flush
708 * data quickly.
709 */
710struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
711 struct ceph_file_layout *layout,
712 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500713 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700714 int opcode, int flags,
715 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700716 u32 truncate_seq,
717 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600718 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700719{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700720 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500721 u64 objnum = 0;
722 u64 objoff = 0;
723 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500724 u32 object_size;
725 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700726 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700727
Li Wangad7a60d2013-08-15 11:51:44 +0800728 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
729 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
730 opcode != CEPH_OSD_OP_TRUNCATE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700731
Alex Elderacead002013-03-14 14:09:05 -0500732 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600733 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700734 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700735 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500736
Alex Elderd178a9e2012-11-13 21:11:15 -0600737 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700738
739 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500740 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600741 if (r < 0) {
742 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700743 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600744 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500745
Alex Elderd18d1e22013-03-13 20:50:01 -0500746 object_size = le32_to_cpu(layout->fl_object_size);
747 object_base = off - objoff;
Yan, Zhengccca4e32013-06-02 18:40:23 +0800748 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
749 if (truncate_size <= object_base) {
750 truncate_size = 0;
751 } else {
752 truncate_size -= object_base;
753 if (truncate_size > object_size)
754 truncate_size = object_size;
755 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500756 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500757
Alex Elderc99d2d42013-04-05 01:27:11 -0500758 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500759 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500760
Alex Elderacead002013-03-14 14:09:05 -0500761 /*
762 * A second op in the ops array means the caller wants to
763 * also issue a include a 'startsync' command so that the
764 * osd will flush data quickly.
765 */
766 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500767 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500768
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200769 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700770
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200771 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200772 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200773 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600774
Sage Weilf24e9982009-10-06 11:31:10 -0700775 return req;
776}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700777EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700778
779/*
780 * We keep osd requests in an rbtree, sorted by ->r_tid.
781 */
782static void __insert_request(struct ceph_osd_client *osdc,
783 struct ceph_osd_request *new)
784{
785 struct rb_node **p = &osdc->requests.rb_node;
786 struct rb_node *parent = NULL;
787 struct ceph_osd_request *req = NULL;
788
789 while (*p) {
790 parent = *p;
791 req = rb_entry(parent, struct ceph_osd_request, r_node);
792 if (new->r_tid < req->r_tid)
793 p = &(*p)->rb_left;
794 else if (new->r_tid > req->r_tid)
795 p = &(*p)->rb_right;
796 else
797 BUG();
798 }
799
800 rb_link_node(&new->r_node, parent, p);
801 rb_insert_color(&new->r_node, &osdc->requests);
802}
803
804static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
805 u64 tid)
806{
807 struct ceph_osd_request *req;
808 struct rb_node *n = osdc->requests.rb_node;
809
810 while (n) {
811 req = rb_entry(n, struct ceph_osd_request, r_node);
812 if (tid < req->r_tid)
813 n = n->rb_left;
814 else if (tid > req->r_tid)
815 n = n->rb_right;
816 else
817 return req;
818 }
819 return NULL;
820}
821
822static struct ceph_osd_request *
823__lookup_request_ge(struct ceph_osd_client *osdc,
824 u64 tid)
825{
826 struct ceph_osd_request *req;
827 struct rb_node *n = osdc->requests.rb_node;
828
829 while (n) {
830 req = rb_entry(n, struct ceph_osd_request, r_node);
831 if (tid < req->r_tid) {
832 if (!n->rb_left)
833 return req;
834 n = n->rb_left;
835 } else if (tid > req->r_tid) {
836 n = n->rb_right;
837 } else {
838 return req;
839 }
840 }
841 return NULL;
842}
843
Sage Weil6f6c7002011-01-17 20:34:08 -0800844/*
845 * Resubmit requests pending on the given osd.
846 */
847static void __kick_osd_requests(struct ceph_osd_client *osdc,
848 struct ceph_osd *osd)
849{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700850 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500851 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800852 int err;
853
854 dout("__kick_osd_requests osd%d\n", osd->o_osd);
855 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600856 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800857 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500858 /*
859 * Build up a list of requests to resend by traversing the
860 * osd's list of requests. Requests for a given object are
861 * sent in tid order, and that is also the order they're
862 * kept on this list. Therefore all requests that are in
863 * flight will be found first, followed by all requests that
864 * have not yet been sent. And to resend requests while
865 * preserving this order we will want to put any sent
866 * requests back on the front of the osd client's unsent
867 * list.
868 *
869 * So we build a separate ordered list of already-sent
870 * requests for the affected osd and splice it onto the
871 * front of the osd client's unsent list. Once we've seen a
872 * request that has not yet been sent we're done. Those
873 * requests are already sitting right where they belong.
874 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800875 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500876 if (!req->r_sent)
877 break;
878 list_move_tail(&req->r_req_lru_item, &resend);
879 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800880 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700881 if (!req->r_linger)
882 req->r_flags |= CEPH_OSD_FLAG_RETRY;
883 }
Alex Eldere02493c2013-03-25 18:16:11 -0500884 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700885
Alex Eldere02493c2013-03-25 18:16:11 -0500886 /*
887 * Linger requests are re-registered before sending, which
888 * sets up a new tid for each. We add them to the unsent
889 * list at the end to keep things in tid order.
890 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700891 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
892 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700893 /*
894 * reregister request prior to unregistering linger so
895 * that r_osd is preserved.
896 */
897 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700898 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500899 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500900 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700901 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700902 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
903 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800904 }
905}
906
Sage Weilf24e9982009-10-06 11:31:10 -0700907/*
Sage Weil81b024e2009-10-09 10:29:18 -0700908 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700909 */
910static void osd_reset(struct ceph_connection *con)
911{
912 struct ceph_osd *osd = con->private;
913 struct ceph_osd_client *osdc;
914
915 if (!osd)
916 return;
917 dout("osd_reset osd%d\n", osd->o_osd);
918 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700919 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800920 mutex_lock(&osdc->request_mutex);
921 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600922 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800923 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700924 up_read(&osdc->map_sem);
925}
926
927/*
928 * Track open sessions with osds.
929 */
Alex Eldere10006f2012-05-26 23:26:43 -0500930static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700931{
932 struct ceph_osd *osd;
933
934 osd = kzalloc(sizeof(*osd), GFP_NOFS);
935 if (!osd)
936 return NULL;
937
938 atomic_set(&osd->o_ref, 1);
939 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500940 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600941 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700942 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700943 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800944 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700945 osd->o_incarnation = 1;
946
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700947 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800948
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800949 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700950 return osd;
951}
952
953static struct ceph_osd *get_osd(struct ceph_osd *osd)
954{
955 if (atomic_inc_not_zero(&osd->o_ref)) {
956 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
957 atomic_read(&osd->o_ref));
958 return osd;
959 } else {
960 dout("get_osd %p FAIL\n", osd);
961 return NULL;
962 }
963}
964
965static void put_osd(struct ceph_osd *osd)
966{
967 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
968 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500969 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700970 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
971
Sage Weil27859f92013-03-25 10:26:14 -0700972 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700973 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700974 }
Sage Weilf24e9982009-10-06 11:31:10 -0700975}
976
977/*
978 * remove an osd from our map
979 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800980static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700981{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800982 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700983 BUG_ON(!list_empty(&osd->o_requests));
984 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800985 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700986 ceph_con_close(&osd->o_con);
987 put_osd(osd);
988}
989
Sage Weilaca420b2011-08-31 14:45:53 -0700990static void remove_all_osds(struct ceph_osd_client *osdc)
991{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500992 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700993 mutex_lock(&osdc->request_mutex);
994 while (!RB_EMPTY_ROOT(&osdc->osds)) {
995 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
996 struct ceph_osd, o_node);
997 __remove_osd(osdc, osd);
998 }
999 mutex_unlock(&osdc->request_mutex);
1000}
1001
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001002static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1003 struct ceph_osd *osd)
1004{
1005 dout("__move_osd_to_lru %p\n", osd);
1006 BUG_ON(!list_empty(&osd->o_osd_lru));
1007 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001008 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001009}
1010
1011static void __remove_osd_from_lru(struct ceph_osd *osd)
1012{
1013 dout("__remove_osd_from_lru %p\n", osd);
1014 if (!list_empty(&osd->o_osd_lru))
1015 list_del_init(&osd->o_osd_lru);
1016}
1017
Sage Weilaca420b2011-08-31 14:45:53 -07001018static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001019{
1020 struct ceph_osd *osd, *nosd;
1021
1022 dout("__remove_old_osds %p\n", osdc);
1023 mutex_lock(&osdc->request_mutex);
1024 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001025 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001026 break;
1027 __remove_osd(osdc, osd);
1028 }
1029 mutex_unlock(&osdc->request_mutex);
1030}
1031
Sage Weilf24e9982009-10-06 11:31:10 -07001032/*
1033 * reset osd connect
1034 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001035static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001036{
Alex Elderc3acb182012-12-07 09:57:58 -06001037 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001038
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001039 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001040 if (list_empty(&osd->o_requests) &&
1041 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001042 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001043
1044 return -ENODEV;
1045 }
1046
1047 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1048 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1049 !ceph_con_opened(&osd->o_con)) {
1050 struct ceph_osd_request *req;
1051
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001052 dout("osd addr hasn't changed and connection never opened, "
1053 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001054 /* touch each r_stamp for handle_timeout()'s benfit */
1055 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1056 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001057
1058 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001059 }
Alex Elderc3acb182012-12-07 09:57:58 -06001060
1061 ceph_con_close(&osd->o_con);
1062 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1063 osd->o_incarnation++;
1064
1065 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001066}
1067
1068static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1069{
1070 struct rb_node **p = &osdc->osds.rb_node;
1071 struct rb_node *parent = NULL;
1072 struct ceph_osd *osd = NULL;
1073
Sage Weilaca420b2011-08-31 14:45:53 -07001074 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001075 while (*p) {
1076 parent = *p;
1077 osd = rb_entry(parent, struct ceph_osd, o_node);
1078 if (new->o_osd < osd->o_osd)
1079 p = &(*p)->rb_left;
1080 else if (new->o_osd > osd->o_osd)
1081 p = &(*p)->rb_right;
1082 else
1083 BUG();
1084 }
1085
1086 rb_link_node(&new->o_node, parent, p);
1087 rb_insert_color(&new->o_node, &osdc->osds);
1088}
1089
1090static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1091{
1092 struct ceph_osd *osd;
1093 struct rb_node *n = osdc->osds.rb_node;
1094
1095 while (n) {
1096 osd = rb_entry(n, struct ceph_osd, o_node);
1097 if (o < osd->o_osd)
1098 n = n->rb_left;
1099 else if (o > osd->o_osd)
1100 n = n->rb_right;
1101 else
1102 return osd;
1103 }
1104 return NULL;
1105}
1106
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001107static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1108{
1109 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001110 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001111}
1112
1113static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1114{
1115 cancel_delayed_work(&osdc->timeout_work);
1116}
Sage Weilf24e9982009-10-06 11:31:10 -07001117
1118/*
1119 * Register request, assign tid. If this is the first request, set up
1120 * the timeout event.
1121 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001122static void __register_request(struct ceph_osd_client *osdc,
1123 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001124{
Sage Weilf24e9982009-10-06 11:31:10 -07001125 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001126 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001127 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001128 __insert_request(osdc, req);
1129 ceph_osdc_get_request(req);
1130 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001131 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001132 dout(" first request, scheduling timeout\n");
1133 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001134 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001135}
1136
Sage Weilf24e9982009-10-06 11:31:10 -07001137/*
1138 * called under osdc->request_mutex
1139 */
1140static void __unregister_request(struct ceph_osd_client *osdc,
1141 struct ceph_osd_request *req)
1142{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001143 if (RB_EMPTY_NODE(&req->r_node)) {
1144 dout("__unregister_request %p tid %lld not registered\n",
1145 req, req->r_tid);
1146 return;
1147 }
1148
Sage Weilf24e9982009-10-06 11:31:10 -07001149 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1150 rb_erase(&req->r_node, &osdc->requests);
1151 osdc->num_requests--;
1152
Sage Weil0ba64782009-10-08 16:57:16 -07001153 if (req->r_osd) {
1154 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001155 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001156
1157 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001158 if (list_empty(&req->r_osd->o_requests) &&
1159 list_empty(&req->r_osd->o_linger_requests)) {
1160 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001161 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001162 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001163 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001164 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001165 }
Sage Weilf24e9982009-10-06 11:31:10 -07001166
Alex Elder7d5f2482012-11-29 08:37:03 -06001167 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001168 ceph_osdc_put_request(req);
1169
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001170 if (osdc->num_requests == 0) {
1171 dout(" no requests, canceling timeout\n");
1172 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001173 }
1174}
1175
1176/*
1177 * Cancel a previously queued request message
1178 */
1179static void __cancel_request(struct ceph_osd_request *req)
1180{
Sage Weil6bc18872010-09-27 10:18:52 -07001181 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001182 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001183 req->r_sent = 0;
1184 }
1185}
1186
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001187static void __register_linger_request(struct ceph_osd_client *osdc,
1188 struct ceph_osd_request *req)
1189{
1190 dout("__register_linger_request %p\n", req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001191 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001192 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001193 if (req->r_osd)
1194 list_add_tail(&req->r_linger_osd,
1195 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001196}
1197
1198static void __unregister_linger_request(struct ceph_osd_client *osdc,
1199 struct ceph_osd_request *req)
1200{
1201 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001202 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001203 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001204 list_del_init(&req->r_linger_osd);
1205
1206 if (list_empty(&req->r_osd->o_requests) &&
1207 list_empty(&req->r_osd->o_linger_requests)) {
1208 dout("moving osd to %p lru\n", req->r_osd);
1209 __move_osd_to_lru(osdc, req->r_osd);
1210 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001211 if (list_empty(&req->r_osd_item))
1212 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001213 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001214 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001215}
1216
1217void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1218 struct ceph_osd_request *req)
1219{
1220 mutex_lock(&osdc->request_mutex);
1221 if (req->r_linger) {
Alex Elderc10ebbf2013-05-09 14:56:32 -05001222 req->r_linger = 0;
Alex Elder96e4dac2013-05-22 20:54:25 -05001223 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001224 }
1225 mutex_unlock(&osdc->request_mutex);
1226}
1227EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1228
1229void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1230 struct ceph_osd_request *req)
1231{
1232 if (!req->r_linger) {
1233 dout("set_request_linger %p\n", req);
1234 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001235 }
1236}
1237EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1238
Sage Weilf24e9982009-10-06 11:31:10 -07001239/*
Josh Durgind29adb32013-12-02 19:11:48 -08001240 * Returns whether a request should be blocked from being sent
1241 * based on the current osdmap and osd_client settings.
1242 *
1243 * Caller should hold map_sem for read.
1244 */
1245static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1246 struct ceph_osd_request *req)
1247{
1248 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1249 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1250 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1251 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1252 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1253}
1254
1255/*
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001256 * Calculate mapping of a request to a PG. Takes tiering into account.
1257 */
1258static int __calc_request_pg(struct ceph_osdmap *osdmap,
1259 struct ceph_osd_request *req,
1260 struct ceph_pg *pg_out)
1261{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001262 bool need_check_tiering;
1263
1264 need_check_tiering = false;
1265 if (req->r_target_oloc.pool == -1) {
1266 req->r_target_oloc = req->r_base_oloc; /* struct */
1267 need_check_tiering = true;
1268 }
1269 if (req->r_target_oid.name_len == 0) {
1270 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1271 need_check_tiering = true;
1272 }
1273
1274 if (need_check_tiering &&
1275 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001276 struct ceph_pg_pool_info *pi;
1277
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001278 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001279 if (pi) {
1280 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1281 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001282 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001283 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1284 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001285 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001286 }
1287 /* !pi is caught in ceph_oloc_oid_to_pg() */
1288 }
1289
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001290 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1291 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001292}
1293
1294/*
Sage Weilf24e9982009-10-06 11:31:10 -07001295 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1296 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001297 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001298 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001299 *
1300 * Return 0 if unchanged, 1 if changed, or negative on error.
1301 *
1302 * Caller should hold map_sem for read and request_mutex.
1303 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001304static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001305 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001306{
Sage Weil5b191d92013-02-23 10:38:16 -08001307 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001308 int acting[CEPH_PG_MAX_SIZE];
1309 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001310 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001311 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001312
Sage Weil6f6c7002011-01-17 20:34:08 -08001313 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001314
1315 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001316 if (err) {
1317 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001318 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001319 }
Sage Weil7740a422010-01-08 15:58:25 -08001320 req->r_pgid = pgid;
1321
Sage Weild85b7052010-05-10 10:24:48 -07001322 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1323 if (err > 0) {
1324 o = acting[0];
1325 num = err;
1326 }
Sage Weilf24e9982009-10-06 11:31:10 -07001327
Josh Durgind29adb32013-12-02 19:11:48 -08001328 was_paused = req->r_paused;
1329 req->r_paused = __req_should_be_paused(osdc, req);
1330 if (was_paused && !req->r_paused)
1331 force_resend = 1;
1332
Sage Weil38d64532011-10-14 13:33:55 -07001333 if ((!force_resend &&
1334 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001335 req->r_sent >= req->r_osd->o_incarnation &&
1336 req->r_num_pg_osds == num &&
1337 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001338 (req->r_osd == NULL && o == -1) ||
1339 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001340 return 0; /* no change */
1341
Sage Weil5b191d92013-02-23 10:38:16 -08001342 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1343 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001344 req->r_osd ? req->r_osd->o_osd : -1);
1345
Sage Weild85b7052010-05-10 10:24:48 -07001346 /* record full pg acting set */
1347 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1348 req->r_num_pg_osds = num;
1349
Sage Weilf24e9982009-10-06 11:31:10 -07001350 if (req->r_osd) {
1351 __cancel_request(req);
1352 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001353 req->r_osd = NULL;
1354 }
1355
1356 req->r_osd = __lookup_osd(osdc, o);
1357 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001358 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001359 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001360 if (!req->r_osd) {
1361 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001362 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001363 }
Sage Weilf24e9982009-10-06 11:31:10 -07001364
Sage Weil6f6c7002011-01-17 20:34:08 -08001365 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001366 __insert_osd(osdc, req->r_osd);
1367
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001368 ceph_con_open(&req->r_osd->o_con,
1369 CEPH_ENTITY_TYPE_OSD, o,
1370 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001371 }
1372
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001373 if (req->r_osd) {
1374 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001375 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1376 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001377 } else {
Alex Elderad885922013-03-25 18:16:11 -05001378 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001379 }
Sage Weild85b7052010-05-10 10:24:48 -07001380 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001381
1382out:
Sage Weilf24e9982009-10-06 11:31:10 -07001383 return err;
1384}
1385
1386/*
1387 * caller should hold map_sem (for read) and request_mutex
1388 */
Sage Weil56e925b2012-01-03 12:34:34 -08001389static void __send_request(struct ceph_osd_client *osdc,
1390 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001391{
Sage Weil1b83bef2013-02-25 16:11:12 -08001392 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001393
Sage Weil1b83bef2013-02-25 16:11:12 -08001394 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1395 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1396 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001397
Sage Weil1b83bef2013-02-25 16:11:12 -08001398 /* fill in message content that changes each time we send it */
1399 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1400 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001401 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001402 p = req->r_request_pgid;
1403 ceph_encode_64(&p, req->r_pgid.pool);
1404 ceph_encode_32(&p, req->r_pgid.seed);
1405 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1406 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1407 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001408
Sage Weil3dd72fc2010-03-22 14:42:30 -07001409 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001410 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001411
1412 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001413
Sage Weilf24e9982009-10-06 11:31:10 -07001414 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001415
1416 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001417}
1418
1419/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001420 * Send any requests in the queue (req_unsent).
1421 */
Alex Elderf9d25192013-02-15 11:42:29 -06001422static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001423{
1424 struct ceph_osd_request *req, *tmp;
1425
Alex Elderf9d25192013-02-15 11:42:29 -06001426 dout("__send_queued\n");
1427 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001428 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001429}
1430
1431/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001432 * Caller should hold map_sem for read and request_mutex.
1433 */
1434static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1435 struct ceph_osd_request *req,
1436 bool nofail)
1437{
1438 int rc;
1439
1440 __register_request(osdc, req);
1441 req->r_sent = 0;
1442 req->r_got_reply = 0;
1443 rc = __map_request(osdc, req, 0);
1444 if (rc < 0) {
1445 if (nofail) {
1446 dout("osdc_start_request failed map, "
1447 " will retry %lld\n", req->r_tid);
1448 rc = 0;
1449 } else {
1450 __unregister_request(osdc, req);
1451 }
1452 return rc;
1453 }
1454
1455 if (req->r_osd == NULL) {
1456 dout("send_request %p no up osds in pg\n", req);
1457 ceph_monc_request_next_osdmap(&osdc->client->monc);
1458 } else {
1459 __send_queued(osdc);
1460 }
1461
1462 return 0;
1463}
1464
1465/*
Sage Weilf24e9982009-10-06 11:31:10 -07001466 * Timeout callback, called every N seconds when 1 or more osd
1467 * requests has been active for more than N seconds. When this
1468 * happens, we ping all OSDs with requests who have timed out to
1469 * ensure any communications channel reset is detected. Reset the
1470 * request timeouts another N seconds in the future as we go.
1471 * Reschedule the timeout event another N seconds in future (unless
1472 * there are no open requests).
1473 */
1474static void handle_timeout(struct work_struct *work)
1475{
1476 struct ceph_osd_client *osdc =
1477 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001478 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001479 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001480 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001481 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001482 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001483 dout("timeout\n");
1484 down_read(&osdc->map_sem);
1485
1486 ceph_monc_request_next_osdmap(&osdc->client->monc);
1487
1488 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001489
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001490 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001491 * ping osds that are a bit slow. this ensures that if there
1492 * is a break in the TCP connection we will notice, and reopen
1493 * a connection with that osd (from the fault callback).
1494 */
1495 INIT_LIST_HEAD(&slow_osds);
1496 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001497 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001498 break;
1499
1500 osd = req->r_osd;
1501 BUG_ON(!osd);
1502 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001503 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001504 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1505 }
1506 while (!list_empty(&slow_osds)) {
1507 osd = list_entry(slow_osds.next, struct ceph_osd,
1508 o_keepalive_item);
1509 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001510 ceph_con_keepalive(&osd->o_con);
1511 }
1512
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001513 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001514 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001515 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001516 up_read(&osdc->map_sem);
1517}
1518
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001519static void handle_osds_timeout(struct work_struct *work)
1520{
1521 struct ceph_osd_client *osdc =
1522 container_of(work, struct ceph_osd_client,
1523 osds_timeout_work.work);
1524 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001525 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001526
1527 dout("osds timeout\n");
1528 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001529 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001530 up_read(&osdc->map_sem);
1531
1532 schedule_delayed_work(&osdc->osds_timeout_work,
1533 round_jiffies_relative(delay));
1534}
1535
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001536static int ceph_oloc_decode(void **p, void *end,
1537 struct ceph_object_locator *oloc)
1538{
1539 u8 struct_v, struct_cv;
1540 u32 len;
1541 void *struct_end;
1542 int ret = 0;
1543
1544 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1545 struct_v = ceph_decode_8(p);
1546 struct_cv = ceph_decode_8(p);
1547 if (struct_v < 3) {
1548 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1549 struct_v, struct_cv);
1550 goto e_inval;
1551 }
1552 if (struct_cv > 6) {
1553 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1554 struct_v, struct_cv);
1555 goto e_inval;
1556 }
1557 len = ceph_decode_32(p);
1558 ceph_decode_need(p, end, len, e_inval);
1559 struct_end = *p + len;
1560
1561 oloc->pool = ceph_decode_64(p);
1562 *p += 4; /* skip preferred */
1563
1564 len = ceph_decode_32(p);
1565 if (len > 0) {
1566 pr_warn("ceph_object_locator::key is set\n");
1567 goto e_inval;
1568 }
1569
1570 if (struct_v >= 5) {
1571 len = ceph_decode_32(p);
1572 if (len > 0) {
1573 pr_warn("ceph_object_locator::nspace is set\n");
1574 goto e_inval;
1575 }
1576 }
1577
1578 if (struct_v >= 6) {
1579 s64 hash = ceph_decode_64(p);
1580 if (hash != -1) {
1581 pr_warn("ceph_object_locator::hash is set\n");
1582 goto e_inval;
1583 }
1584 }
1585
1586 /* skip the rest */
1587 *p = struct_end;
1588out:
1589 return ret;
1590
1591e_inval:
1592 ret = -EINVAL;
1593 goto out;
1594}
1595
1596static int ceph_redirect_decode(void **p, void *end,
1597 struct ceph_request_redirect *redir)
1598{
1599 u8 struct_v, struct_cv;
1600 u32 len;
1601 void *struct_end;
1602 int ret;
1603
1604 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1605 struct_v = ceph_decode_8(p);
1606 struct_cv = ceph_decode_8(p);
1607 if (struct_cv > 1) {
1608 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1609 struct_v, struct_cv);
1610 goto e_inval;
1611 }
1612 len = ceph_decode_32(p);
1613 ceph_decode_need(p, end, len, e_inval);
1614 struct_end = *p + len;
1615
1616 ret = ceph_oloc_decode(p, end, &redir->oloc);
1617 if (ret)
1618 goto out;
1619
1620 len = ceph_decode_32(p);
1621 if (len > 0) {
1622 pr_warn("ceph_request_redirect::object_name is set\n");
1623 goto e_inval;
1624 }
1625
1626 len = ceph_decode_32(p);
1627 *p += len; /* skip osd_instructions */
1628
1629 /* skip the rest */
1630 *p = struct_end;
1631out:
1632 return ret;
1633
1634e_inval:
1635 ret = -EINVAL;
1636 goto out;
1637}
1638
Sage Weil25845472011-06-03 09:37:09 -07001639static void complete_request(struct ceph_osd_request *req)
1640{
Sage Weil25845472011-06-03 09:37:09 -07001641 complete_all(&req->r_safe_completion); /* fsync waiter */
1642}
1643
Sage Weilf24e9982009-10-06 11:31:10 -07001644/*
1645 * handle osd op reply. either call the callback if it is specified,
1646 * or do the completion to wake up the waiting thread.
1647 */
Sage Weil350b1c32009-12-22 10:45:45 -08001648static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1649 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001650{
Sage Weil1b83bef2013-02-25 16:11:12 -08001651 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001652 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001653 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001654 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001655 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001656 unsigned int numops;
1657 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001658 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001659 s32 retry_attempt;
1660 struct ceph_pg pg;
1661 int err;
1662 u32 reassert_epoch;
1663 u64 reassert_version;
1664 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001665 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001666 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001667 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001668
Sage Weil6df058c2009-12-22 11:24:33 -08001669 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001670 dout("handle_reply %p tid %llu\n", msg, tid);
1671
1672 p = msg->front.iov_base;
1673 end = p + msg->front.iov_len;
1674
1675 ceph_decode_need(&p, end, 4, bad);
1676 object_len = ceph_decode_32(&p);
1677 ceph_decode_need(&p, end, object_len, bad);
1678 p += object_len;
1679
Alex Elderef4859d2013-04-01 18:58:26 -05001680 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001681 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001682 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001683
1684 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1685 flags = ceph_decode_64(&p);
1686 result = ceph_decode_32(&p);
1687 reassert_epoch = ceph_decode_32(&p);
1688 reassert_version = ceph_decode_64(&p);
1689 osdmap_epoch = ceph_decode_32(&p);
1690
Sage Weilf24e9982009-10-06 11:31:10 -07001691 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001692 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001693 mutex_lock(&osdc->request_mutex);
1694 req = __lookup_request(osdc, tid);
1695 if (req == NULL) {
1696 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001697 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001698 }
1699 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001700
1701 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1702 req, result);
1703
Dan Carpenter18741192013-08-15 08:51:58 +03001704 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001705 numops = ceph_decode_32(&p);
1706 if (numops > CEPH_OSD_MAX_OP)
1707 goto bad_put;
1708 if (numops != req->r_num_ops)
1709 goto bad_put;
1710 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001711 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001712 for (i = 0; i < numops; i++) {
1713 struct ceph_osd_op *op = p;
1714 int len;
1715
1716 len = le32_to_cpu(op->payload_len);
1717 req->r_reply_op_len[i] = len;
1718 dout(" op %d has %d bytes\n", i, len);
1719 payload_len += len;
1720 p += sizeof(*op);
1721 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001722 bytes = le32_to_cpu(msg->hdr.data_len);
1723 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001724 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001725 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001726 goto bad_put;
1727 }
1728
Dan Carpenter18741192013-08-15 08:51:58 +03001729 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001730 retry_attempt = ceph_decode_32(&p);
1731 for (i = 0; i < numops; i++)
1732 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001733
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001734 if (le16_to_cpu(msg->hdr.version) >= 6) {
1735 p += 8 + 4; /* skip replay_version */
1736 p += 8; /* skip user_version */
1737
1738 err = ceph_redirect_decode(&p, end, &redir);
1739 if (err)
1740 goto bad_put;
1741 } else {
1742 redir.oloc.pool = -1;
1743 }
1744
1745 if (redir.oloc.pool != -1) {
1746 dout("redirect pool %lld\n", redir.oloc.pool);
1747
1748 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001749
1750 req->r_target_oloc = redir.oloc; /* struct */
1751
1752 /*
1753 * Start redirect requests with nofail=true. If
1754 * mapping fails, request will end up on the notarget
1755 * list, waiting for the new osdmap (which can take
1756 * a while), even though the original request mapped
1757 * successfully. In the future we might want to follow
1758 * original request's nofail setting here.
1759 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001760 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001761 BUG_ON(err);
1762
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001763 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001764 }
1765
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001766 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001767 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001768 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001769 dout("handle_reply result %d bytes %d\n", req->r_result,
1770 bytes);
1771 if (req->r_result == 0)
1772 req->r_result = bytes;
1773
1774 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001775 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1776 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001777
1778 req->r_got_reply = 1;
1779 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1780 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001781 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001782 }
1783
1784 dout("handle_reply tid %llu flags %d\n", tid, flags);
1785
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001786 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1787 __register_linger_request(osdc, req);
1788
Sage Weilf24e9982009-10-06 11:31:10 -07001789 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001790 if (result < 0 ||
1791 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001792 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1793 __unregister_request(osdc, req);
1794
1795 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001796 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001797
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001798 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001799 if (req->r_unsafe_callback &&
1800 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1801 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001802 if (req->r_callback)
1803 req->r_callback(req, msg);
1804 else
1805 complete_all(&req->r_completion);
1806 }
Sage Weilf24e9982009-10-06 11:31:10 -07001807
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001808 if (flags & CEPH_OSD_FLAG_ONDISK) {
1809 if (req->r_unsafe_callback && already_completed)
1810 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001811 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001812 }
Sage Weilf24e9982009-10-06 11:31:10 -07001813
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001814out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001815 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001816 ceph_osdc_put_request(req);
1817 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001818out_unlock:
1819 mutex_unlock(&osdc->request_mutex);
1820 up_read(&osdc->map_sem);
1821 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001822
Sage Weil1b83bef2013-02-25 16:11:12 -08001823bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001824 req->r_result = -EIO;
1825 __unregister_request(osdc, req);
1826 if (req->r_callback)
1827 req->r_callback(req, msg);
1828 else
1829 complete_all(&req->r_completion);
1830 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001831 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001832bad_mutex:
1833 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001834 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001835bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001836 pr_err("corrupt osd_op_reply got %d %d\n",
1837 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001838 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001839}
1840
Sage Weil6f6c7002011-01-17 20:34:08 -08001841static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001842{
Sage Weilf24e9982009-10-06 11:31:10 -07001843 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001844
Sage Weil6f6c7002011-01-17 20:34:08 -08001845 for (p = rb_first(&osdc->osds); p; p = n) {
1846 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001847
Sage Weil6f6c7002011-01-17 20:34:08 -08001848 n = rb_next(p);
1849 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1850 memcmp(&osd->o_con.peer_addr,
1851 ceph_osd_addr(osdc->osdmap,
1852 osd->o_osd),
1853 sizeof(struct ceph_entity_addr)) != 0)
1854 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001855 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001856}
1857
1858/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001859 * Requeue requests whose mapping to an OSD has changed. If requests map to
1860 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001861 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001862 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001863 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001864static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1865 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001866{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001867 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001868 struct rb_node *p;
1869 int needmap = 0;
1870 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001871 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001872
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001873 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1874 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001875 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001876 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001877 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001878 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001879
1880 /*
1881 * For linger requests that have not yet been
1882 * registered, move them to the linger list; they'll
1883 * be sent to the osd in the loop below. Unregister
1884 * the request before re-registering it as a linger
1885 * request to ensure the __map_request() below
1886 * will decide it needs to be sent.
1887 */
1888 if (req->r_linger && list_empty(&req->r_linger_item)) {
1889 dout("%p tid %llu restart on osd%d\n",
1890 req, req->r_tid,
1891 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001892 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001893 __unregister_request(osdc, req);
1894 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001895 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001896 continue;
1897 }
1898
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001899 force_resend_req = force_resend ||
1900 (force_resend_writes &&
1901 req->r_flags & CEPH_OSD_FLAG_WRITE);
1902 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001903 if (err < 0)
1904 continue; /* error */
1905 if (req->r_osd == NULL) {
1906 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1907 needmap++; /* request a newer map */
1908 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001909 if (!req->r_linger) {
1910 dout("%p tid %llu requeued on osd%d\n", req,
1911 req->r_tid,
1912 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001913 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001914 }
1915 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001916 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001917
1918 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1919 r_linger_item) {
1920 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1921
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001922 err = __map_request(osdc, req,
1923 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06001924 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001925 if (err == 0)
1926 continue; /* no change and no osd was specified */
1927 if (err < 0)
1928 continue; /* hrm! */
1929 if (req->r_osd == NULL) {
1930 dout("tid %llu maps to no valid osd\n", req->r_tid);
1931 needmap++; /* request a newer map */
1932 continue;
1933 }
1934
1935 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1936 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001937 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001938 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001939 }
Alex Elder14d2f382013-05-15 16:28:33 -05001940 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001941 mutex_unlock(&osdc->request_mutex);
1942
1943 if (needmap) {
1944 dout("%d requests for down osds, need new map\n", needmap);
1945 ceph_monc_request_next_osdmap(&osdc->client->monc);
1946 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001947}
Sage Weil6f6c7002011-01-17 20:34:08 -08001948
1949
Sage Weilf24e9982009-10-06 11:31:10 -07001950/*
1951 * Process updated osd map.
1952 *
1953 * The message contains any number of incremental and full maps, normally
1954 * indicating some sort of topology change in the cluster. Kick requests
1955 * off to different OSDs as needed.
1956 */
1957void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1958{
1959 void *p, *end, *next;
1960 u32 nr_maps, maplen;
1961 u32 epoch;
1962 struct ceph_osdmap *newmap = NULL, *oldmap;
1963 int err;
1964 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001965 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07001966
1967 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1968 p = msg->front.iov_base;
1969 end = p + msg->front.iov_len;
1970
1971 /* verify fsid */
1972 ceph_decode_need(&p, end, sizeof(fsid), bad);
1973 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001974 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1975 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001976
1977 down_write(&osdc->map_sem);
1978
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001979 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1980
Sage Weilf24e9982009-10-06 11:31:10 -07001981 /* incremental maps */
1982 ceph_decode_32_safe(&p, end, nr_maps, bad);
1983 dout(" %d inc maps\n", nr_maps);
1984 while (nr_maps > 0) {
1985 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001986 epoch = ceph_decode_32(&p);
1987 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001988 ceph_decode_need(&p, end, maplen, bad);
1989 next = p + maplen;
1990 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1991 dout("applying incremental map %u len %d\n",
1992 epoch, maplen);
1993 newmap = osdmap_apply_incremental(&p, next,
1994 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001995 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001996 if (IS_ERR(newmap)) {
1997 err = PTR_ERR(newmap);
1998 goto bad;
1999 }
Sage Weil30dc6382009-12-21 14:49:37 -08002000 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002001 if (newmap != osdc->osdmap) {
2002 ceph_osdmap_destroy(osdc->osdmap);
2003 osdc->osdmap = newmap;
2004 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002005 was_full = was_full ||
2006 ceph_osdmap_flag(osdc->osdmap,
2007 CEPH_OSDMAP_FULL);
2008 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002009 } else {
2010 dout("ignoring incremental map %u len %d\n",
2011 epoch, maplen);
2012 }
2013 p = next;
2014 nr_maps--;
2015 }
2016 if (newmap)
2017 goto done;
2018
2019 /* full maps */
2020 ceph_decode_32_safe(&p, end, nr_maps, bad);
2021 dout(" %d full maps\n", nr_maps);
2022 while (nr_maps) {
2023 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002024 epoch = ceph_decode_32(&p);
2025 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002026 ceph_decode_need(&p, end, maplen, bad);
2027 if (nr_maps > 1) {
2028 dout("skipping non-latest full map %u len %d\n",
2029 epoch, maplen);
2030 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2031 dout("skipping full map %u len %d, "
2032 "older than our %u\n", epoch, maplen,
2033 osdc->osdmap->epoch);
2034 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002035 int skipped_map = 0;
2036
Sage Weilf24e9982009-10-06 11:31:10 -07002037 dout("taking full map %u len %d\n", epoch, maplen);
2038 newmap = osdmap_decode(&p, p+maplen);
2039 if (IS_ERR(newmap)) {
2040 err = PTR_ERR(newmap);
2041 goto bad;
2042 }
Sage Weil30dc6382009-12-21 14:49:37 -08002043 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002044 oldmap = osdc->osdmap;
2045 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002046 if (oldmap) {
2047 if (oldmap->epoch + 1 < newmap->epoch)
2048 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002049 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002050 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002051 was_full = was_full ||
2052 ceph_osdmap_flag(osdc->osdmap,
2053 CEPH_OSDMAP_FULL);
2054 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002055 }
2056 p += maplen;
2057 nr_maps--;
2058 }
2059
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002060 if (!osdc->osdmap)
2061 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002062done:
2063 downgrade_write(&osdc->map_sem);
2064 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002065
2066 /*
2067 * subscribe to subsequent osdmap updates if full to ensure
2068 * we find out when we are no longer full and stop returning
2069 * ENOSPC.
2070 */
Josh Durgind29adb32013-12-02 19:11:48 -08002071 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2072 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2073 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002074 ceph_monc_request_next_osdmap(&osdc->client->monc);
2075
Alex Elderf9d25192013-02-15 11:42:29 -06002076 mutex_lock(&osdc->request_mutex);
2077 __send_queued(osdc);
2078 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002079 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002080 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002081 return;
2082
2083bad:
2084 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002085 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002086 up_write(&osdc->map_sem);
2087 return;
2088}
2089
Sage Weilf24e9982009-10-06 11:31:10 -07002090/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002091 * watch/notify callback event infrastructure
2092 *
2093 * These callbacks are used both for watch and notify operations.
2094 */
2095static void __release_event(struct kref *kref)
2096{
2097 struct ceph_osd_event *event =
2098 container_of(kref, struct ceph_osd_event, kref);
2099
2100 dout("__release_event %p\n", event);
2101 kfree(event);
2102}
2103
2104static void get_event(struct ceph_osd_event *event)
2105{
2106 kref_get(&event->kref);
2107}
2108
2109void ceph_osdc_put_event(struct ceph_osd_event *event)
2110{
2111 kref_put(&event->kref, __release_event);
2112}
2113EXPORT_SYMBOL(ceph_osdc_put_event);
2114
2115static void __insert_event(struct ceph_osd_client *osdc,
2116 struct ceph_osd_event *new)
2117{
2118 struct rb_node **p = &osdc->event_tree.rb_node;
2119 struct rb_node *parent = NULL;
2120 struct ceph_osd_event *event = NULL;
2121
2122 while (*p) {
2123 parent = *p;
2124 event = rb_entry(parent, struct ceph_osd_event, node);
2125 if (new->cookie < event->cookie)
2126 p = &(*p)->rb_left;
2127 else if (new->cookie > event->cookie)
2128 p = &(*p)->rb_right;
2129 else
2130 BUG();
2131 }
2132
2133 rb_link_node(&new->node, parent, p);
2134 rb_insert_color(&new->node, &osdc->event_tree);
2135}
2136
2137static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2138 u64 cookie)
2139{
2140 struct rb_node **p = &osdc->event_tree.rb_node;
2141 struct rb_node *parent = NULL;
2142 struct ceph_osd_event *event = NULL;
2143
2144 while (*p) {
2145 parent = *p;
2146 event = rb_entry(parent, struct ceph_osd_event, node);
2147 if (cookie < event->cookie)
2148 p = &(*p)->rb_left;
2149 else if (cookie > event->cookie)
2150 p = &(*p)->rb_right;
2151 else
2152 return event;
2153 }
2154 return NULL;
2155}
2156
2157static void __remove_event(struct ceph_osd_event *event)
2158{
2159 struct ceph_osd_client *osdc = event->osdc;
2160
2161 if (!RB_EMPTY_NODE(&event->node)) {
2162 dout("__remove_event removed %p\n", event);
2163 rb_erase(&event->node, &osdc->event_tree);
2164 ceph_osdc_put_event(event);
2165 } else {
2166 dout("__remove_event didn't remove %p\n", event);
2167 }
2168}
2169
2170int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2171 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002172 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002173{
2174 struct ceph_osd_event *event;
2175
2176 event = kmalloc(sizeof(*event), GFP_NOIO);
2177 if (!event)
2178 return -ENOMEM;
2179
2180 dout("create_event %p\n", event);
2181 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002182 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002183 event->data = data;
2184 event->osdc = osdc;
2185 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002186 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002187 kref_init(&event->kref); /* one ref for us */
2188 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002189
2190 spin_lock(&osdc->event_lock);
2191 event->cookie = ++osdc->event_count;
2192 __insert_event(osdc, event);
2193 spin_unlock(&osdc->event_lock);
2194
2195 *pevent = event;
2196 return 0;
2197}
2198EXPORT_SYMBOL(ceph_osdc_create_event);
2199
2200void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2201{
2202 struct ceph_osd_client *osdc = event->osdc;
2203
2204 dout("cancel_event %p\n", event);
2205 spin_lock(&osdc->event_lock);
2206 __remove_event(event);
2207 spin_unlock(&osdc->event_lock);
2208 ceph_osdc_put_event(event); /* caller's */
2209}
2210EXPORT_SYMBOL(ceph_osdc_cancel_event);
2211
2212
2213static void do_event_work(struct work_struct *work)
2214{
2215 struct ceph_osd_event_work *event_work =
2216 container_of(work, struct ceph_osd_event_work, work);
2217 struct ceph_osd_event *event = event_work->event;
2218 u64 ver = event_work->ver;
2219 u64 notify_id = event_work->notify_id;
2220 u8 opcode = event_work->opcode;
2221
2222 dout("do_event_work completing %p\n", event);
2223 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002224 dout("do_event_work completed %p\n", event);
2225 ceph_osdc_put_event(event);
2226 kfree(event_work);
2227}
2228
2229
2230/*
2231 * Process osd watch notifications
2232 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002233static void handle_watch_notify(struct ceph_osd_client *osdc,
2234 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002235{
2236 void *p, *end;
2237 u8 proto_ver;
2238 u64 cookie, ver, notify_id;
2239 u8 opcode;
2240 struct ceph_osd_event *event;
2241 struct ceph_osd_event_work *event_work;
2242
2243 p = msg->front.iov_base;
2244 end = p + msg->front.iov_len;
2245
2246 ceph_decode_8_safe(&p, end, proto_ver, bad);
2247 ceph_decode_8_safe(&p, end, opcode, bad);
2248 ceph_decode_64_safe(&p, end, cookie, bad);
2249 ceph_decode_64_safe(&p, end, ver, bad);
2250 ceph_decode_64_safe(&p, end, notify_id, bad);
2251
2252 spin_lock(&osdc->event_lock);
2253 event = __find_event(osdc, cookie);
2254 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002255 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002256 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002257 }
2258 spin_unlock(&osdc->event_lock);
2259 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2260 cookie, ver, event);
2261 if (event) {
2262 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002263 if (!event_work) {
2264 dout("ERROR: could not allocate event_work\n");
2265 goto done_err;
2266 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002267 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002268 event_work->event = event;
2269 event_work->ver = ver;
2270 event_work->notify_id = notify_id;
2271 event_work->opcode = opcode;
2272 if (!queue_work(osdc->notify_wq, &event_work->work)) {
2273 dout("WARNING: failed to queue notify event work\n");
2274 goto done_err;
2275 }
2276 }
2277
2278 return;
2279
2280done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002281 ceph_osdc_put_event(event);
2282 return;
2283
2284bad:
2285 pr_err("osdc handle_watch_notify corrupt msg\n");
2286 return;
2287}
2288
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002289/*
Alex Eldere65550f2013-04-05 01:27:12 -05002290 * build new request AND message
2291 *
2292 */
2293void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2294 struct ceph_snap_context *snapc, u64 snap_id,
2295 struct timespec *mtime)
2296{
2297 struct ceph_msg *msg = req->r_request;
2298 void *p;
2299 size_t msg_size;
2300 int flags = req->r_flags;
2301 u64 data_len;
2302 unsigned int i;
2303
2304 req->r_snapid = snap_id;
2305 req->r_snapc = ceph_get_snap_context(snapc);
2306
2307 /* encode request */
2308 msg->hdr.version = cpu_to_le16(4);
2309
2310 p = msg->front.iov_base;
2311 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2312 req->r_request_osdmap_epoch = p;
2313 p += 4;
2314 req->r_request_flags = p;
2315 p += 4;
2316 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2317 ceph_encode_timespec(p, mtime);
2318 p += sizeof(struct ceph_timespec);
2319 req->r_request_reassert_version = p;
2320 p += sizeof(struct ceph_eversion); /* will get filled in */
2321
2322 /* oloc */
2323 ceph_encode_8(&p, 4);
2324 ceph_encode_8(&p, 4);
2325 ceph_encode_32(&p, 8 + 4 + 4);
2326 req->r_request_pool = p;
2327 p += 8;
2328 ceph_encode_32(&p, -1); /* preferred */
2329 ceph_encode_32(&p, 0); /* key len */
2330
2331 ceph_encode_8(&p, 1);
2332 req->r_request_pgid = p;
2333 p += 8 + 4;
2334 ceph_encode_32(&p, -1); /* preferred */
2335
2336 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002337 ceph_encode_32(&p, req->r_base_oid.name_len);
2338 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2339 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2340 req->r_base_oid.name, req->r_base_oid.name_len);
2341 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002342
2343 /* ops--can imply data */
2344 ceph_encode_16(&p, (u16)req->r_num_ops);
2345 data_len = 0;
2346 for (i = 0; i < req->r_num_ops; i++) {
2347 data_len += osd_req_encode_op(req, p, i);
2348 p += sizeof(struct ceph_osd_op);
2349 }
2350
2351 /* snaps */
2352 ceph_encode_64(&p, req->r_snapid);
2353 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2354 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2355 if (req->r_snapc) {
2356 for (i = 0; i < snapc->num_snaps; i++) {
2357 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2358 }
2359 }
2360
2361 req->r_request_attempts = p;
2362 p += 4;
2363
2364 /* data */
2365 if (flags & CEPH_OSD_FLAG_WRITE) {
2366 u16 data_off;
2367
2368 /*
2369 * The header "data_off" is a hint to the receiver
2370 * allowing it to align received data into its
2371 * buffers such that there's no need to re-copy
2372 * it before writing it to disk (direct I/O).
2373 */
2374 data_off = (u16) (off & 0xffff);
2375 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2376 }
2377 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2378
2379 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2380 msg_size = p - msg->front.iov_base;
2381 msg->front.iov_len = msg_size;
2382 msg->hdr.front_len = cpu_to_le32(msg_size);
2383
2384 dout("build_request msg_size was %d\n", (int)msg_size);
2385}
2386EXPORT_SYMBOL(ceph_osdc_build_request);
2387
2388/*
Sage Weilf24e9982009-10-06 11:31:10 -07002389 * Register request, send initial attempt.
2390 */
2391int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2392 struct ceph_osd_request *req,
2393 bool nofail)
2394{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002395 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002396
Sage Weilf24e9982009-10-06 11:31:10 -07002397 down_read(&osdc->map_sem);
2398 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002399
2400 rc = __ceph_osdc_start_request(osdc, req, nofail);
2401
Sage Weilf24e9982009-10-06 11:31:10 -07002402 mutex_unlock(&osdc->request_mutex);
2403 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002404
Sage Weilf24e9982009-10-06 11:31:10 -07002405 return rc;
2406}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002407EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002408
2409/*
2410 * wait for a request to complete
2411 */
2412int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2413 struct ceph_osd_request *req)
2414{
2415 int rc;
2416
2417 rc = wait_for_completion_interruptible(&req->r_completion);
2418 if (rc < 0) {
2419 mutex_lock(&osdc->request_mutex);
2420 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002421 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07002422 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07002423 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002424 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07002425 return rc;
2426 }
2427
2428 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2429 return req->r_result;
2430}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002431EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002432
2433/*
2434 * sync - wait for all in-flight requests to flush. avoid starvation.
2435 */
2436void ceph_osdc_sync(struct ceph_osd_client *osdc)
2437{
2438 struct ceph_osd_request *req;
2439 u64 last_tid, next_tid = 0;
2440
2441 mutex_lock(&osdc->request_mutex);
2442 last_tid = osdc->last_tid;
2443 while (1) {
2444 req = __lookup_request_ge(osdc, next_tid);
2445 if (!req)
2446 break;
2447 if (req->r_tid > last_tid)
2448 break;
2449
2450 next_tid = req->r_tid + 1;
2451 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2452 continue;
2453
2454 ceph_osdc_get_request(req);
2455 mutex_unlock(&osdc->request_mutex);
2456 dout("sync waiting on tid %llu (last is %llu)\n",
2457 req->r_tid, last_tid);
2458 wait_for_completion(&req->r_safe_completion);
2459 mutex_lock(&osdc->request_mutex);
2460 ceph_osdc_put_request(req);
2461 }
2462 mutex_unlock(&osdc->request_mutex);
2463 dout("sync done (thru tid %llu)\n", last_tid);
2464}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002465EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002466
2467/*
Josh Durgindd935f42013-08-28 21:43:09 -07002468 * Call all pending notify callbacks - for use after a watch is
2469 * unregistered, to make sure no more callbacks for it will be invoked
2470 */
2471extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2472{
2473 flush_workqueue(osdc->notify_wq);
2474}
2475EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2476
2477
2478/*
Sage Weilf24e9982009-10-06 11:31:10 -07002479 * init, shutdown
2480 */
2481int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2482{
2483 int err;
2484
2485 dout("init\n");
2486 osdc->client = client;
2487 osdc->osdmap = NULL;
2488 init_rwsem(&osdc->map_sem);
2489 init_completion(&osdc->map_waiters);
2490 osdc->last_requested_map = 0;
2491 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002492 osdc->last_tid = 0;
2493 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002494 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002495 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002496 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002497 INIT_LIST_HEAD(&osdc->req_unsent);
2498 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002499 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002500 osdc->num_requests = 0;
2501 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002502 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002503 spin_lock_init(&osdc->event_lock);
2504 osdc->event_tree = RB_ROOT;
2505 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002506
2507 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002508 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002509
Sage Weil5f44f142009-11-18 14:52:18 -08002510 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002511 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2512 sizeof(struct ceph_osd_request));
2513 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002514 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002515
Sage Weild50b4092012-07-09 14:22:34 -07002516 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2517 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002518 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002519 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002520 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002521 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002522 OSD_OPREPLY_FRONT_LEN, 10, true,
2523 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002524 if (err < 0)
2525 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002526
Dan Carpenterdbcae082013-08-15 08:58:59 +03002527 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002528 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002529 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002530 goto out_msgpool_reply;
2531
Sage Weilf24e9982009-10-06 11:31:10 -07002532 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002533
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002534out_msgpool_reply:
2535 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002536out_msgpool:
2537 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002538out_mempool:
2539 mempool_destroy(osdc->req_mempool);
2540out:
2541 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002542}
2543
2544void ceph_osdc_stop(struct ceph_osd_client *osdc)
2545{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002546 flush_workqueue(osdc->notify_wq);
2547 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002548 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002549 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002550 if (osdc->osdmap) {
2551 ceph_osdmap_destroy(osdc->osdmap);
2552 osdc->osdmap = NULL;
2553 }
Sage Weilaca420b2011-08-31 14:45:53 -07002554 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002555 mempool_destroy(osdc->req_mempool);
2556 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002557 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002558}
2559
2560/*
2561 * Read some contiguous pages. If we cross a stripe boundary, shorten
2562 * *plen. Return number of bytes read, or error.
2563 */
2564int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2565 struct ceph_vino vino, struct ceph_file_layout *layout,
2566 u64 off, u64 *plen,
2567 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002568 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002569{
2570 struct ceph_osd_request *req;
2571 int rc = 0;
2572
2573 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2574 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002575 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002576 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002577 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002578 false);
Sage Weil68162822012-09-24 21:01:02 -07002579 if (IS_ERR(req))
2580 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002581
2582 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002583
Alex Elder406e2c92013-04-15 14:50:36 -05002584 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002585 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002586
Alex Eldere0c59482013-03-07 15:38:25 -06002587 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002588 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002589
Alex Elder79528732013-04-03 21:32:51 -05002590 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002591
Sage Weilf24e9982009-10-06 11:31:10 -07002592 rc = ceph_osdc_start_request(osdc, req, false);
2593 if (!rc)
2594 rc = ceph_osdc_wait_request(osdc, req);
2595
2596 ceph_osdc_put_request(req);
2597 dout("readpages result %d\n", rc);
2598 return rc;
2599}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002600EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002601
2602/*
2603 * do a synchronous write on N pages
2604 */
2605int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2606 struct ceph_file_layout *layout,
2607 struct ceph_snap_context *snapc,
2608 u64 off, u64 len,
2609 u32 truncate_seq, u64 truncate_size,
2610 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002611 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002612{
2613 struct ceph_osd_request *req;
2614 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002615 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002616
Alex Elderacead002013-03-14 14:09:05 -05002617 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002618 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002619 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002620 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002621 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002622 true);
Sage Weil68162822012-09-24 21:01:02 -07002623 if (IS_ERR(req))
2624 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002625
2626 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002627 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002628 false, false);
2629 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002630
Alex Elder79528732013-04-03 21:32:51 -05002631 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002632
Alex Elder87f979d2013-02-15 11:42:29 -06002633 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002634 if (!rc)
2635 rc = ceph_osdc_wait_request(osdc, req);
2636
2637 ceph_osdc_put_request(req);
2638 if (rc == 0)
2639 rc = len;
2640 dout("writepages result %d\n", rc);
2641 return rc;
2642}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002643EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002644
Alex Elder5522ae02013-05-01 12:43:04 -05002645int ceph_osdc_setup(void)
2646{
2647 BUG_ON(ceph_osd_request_cache);
2648 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2649 sizeof (struct ceph_osd_request),
2650 __alignof__(struct ceph_osd_request),
2651 0, NULL);
2652
2653 return ceph_osd_request_cache ? 0 : -ENOMEM;
2654}
2655EXPORT_SYMBOL(ceph_osdc_setup);
2656
2657void ceph_osdc_cleanup(void)
2658{
2659 BUG_ON(!ceph_osd_request_cache);
2660 kmem_cache_destroy(ceph_osd_request_cache);
2661 ceph_osd_request_cache = NULL;
2662}
2663EXPORT_SYMBOL(ceph_osdc_cleanup);
2664
Sage Weilf24e9982009-10-06 11:31:10 -07002665/*
2666 * handle incoming message
2667 */
2668static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2669{
2670 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002671 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002672 int type = le16_to_cpu(msg->hdr.type);
2673
2674 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002675 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002676 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002677
2678 switch (type) {
2679 case CEPH_MSG_OSD_MAP:
2680 ceph_osdc_handle_map(osdc, msg);
2681 break;
2682 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002683 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002684 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002685 case CEPH_MSG_WATCH_NOTIFY:
2686 handle_watch_notify(osdc, msg);
2687 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002688
2689 default:
2690 pr_err("received unknown message type %d %s\n", type,
2691 ceph_msg_type_name(type));
2692 }
Sage Weil4a32f932010-06-13 10:27:53 -07002693out:
Sage Weilf24e9982009-10-06 11:31:10 -07002694 ceph_msg_put(msg);
2695}
2696
Sage Weil5b3a4db2010-02-19 21:43:23 -08002697/*
Sage Weil21b667f2010-03-04 10:22:59 -08002698 * lookup and return message for incoming reply. set up reply message
2699 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002700 */
2701static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002702 struct ceph_msg_header *hdr,
2703 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002704{
2705 struct ceph_osd *osd = con->private;
2706 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002707 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002708 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002709 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002710 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002711 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002712
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002713 tid = le64_to_cpu(hdr->tid);
2714 mutex_lock(&osdc->request_mutex);
2715 req = __lookup_request(osdc, tid);
2716 if (!req) {
2717 *skip = 1;
2718 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002719 dout("get_reply unknown tid %llu from osd%d\n", tid,
2720 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002721 goto out;
2722 }
Sage Weilc16e7862010-03-01 13:02:00 -08002723
Alex Elderace6d3a2013-04-01 16:12:14 -05002724 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002725 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002726 req->r_reply, req->r_reply->con);
2727 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002728
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002729 if (front_len > req->r_reply->front_alloc_len) {
Alex Elder49743412013-05-29 11:19:00 -05002730 pr_warning("get_reply front %d > preallocated %d (%u#%llu)\n",
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002731 front_len, req->r_reply->front_alloc_len,
Alex Elder49743412013-05-29 11:19:00 -05002732 (unsigned int)con->peer_name.type,
2733 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002734 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2735 false);
Sage Weila79832f2010-04-01 16:06:19 -07002736 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002737 goto out;
2738 ceph_msg_put(req->r_reply);
2739 req->r_reply = m;
2740 }
2741 m = ceph_msg_get(req->r_reply);
2742
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002743 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002744 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002745
Alex Eldera4ce40a2013-04-05 01:27:12 -05002746 /*
2747 * XXX This is assuming there is only one op containing
2748 * XXX page data. Probably OK for reads, but this
2749 * XXX ought to be done more generally.
2750 */
Alex Elder406e2c92013-04-15 14:50:36 -05002751 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002752 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002753 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002754 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002755
Alex Eldere0c59482013-03-07 15:38:25 -06002756 pr_warning("tid %lld reply has %d bytes "
2757 "we had only %llu bytes ready\n",
2758 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002759 *skip = 1;
2760 ceph_msg_put(m);
2761 m = NULL;
2762 goto out;
2763 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002764 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002765 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002766 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002767 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002768
2769out:
2770 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002771 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002772
2773}
2774
2775static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2776 struct ceph_msg_header *hdr,
2777 int *skip)
2778{
2779 struct ceph_osd *osd = con->private;
2780 int type = le16_to_cpu(hdr->type);
2781 int front = le32_to_cpu(hdr->front_len);
2782
Alex Elder1c20f2d2012-06-04 14:43:32 -05002783 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002784 switch (type) {
2785 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002786 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002787 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002788 case CEPH_MSG_OSD_OPREPLY:
2789 return get_reply(con, hdr, skip);
2790 default:
2791 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2792 osd->o_osd);
2793 *skip = 1;
2794 return NULL;
2795 }
Sage Weilf24e9982009-10-06 11:31:10 -07002796}
2797
2798/*
2799 * Wrappers to refcount containing ceph_osd struct
2800 */
2801static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2802{
2803 struct ceph_osd *osd = con->private;
2804 if (get_osd(osd))
2805 return con;
2806 return NULL;
2807}
2808
2809static void put_osd_con(struct ceph_connection *con)
2810{
2811 struct ceph_osd *osd = con->private;
2812 put_osd(osd);
2813}
2814
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002815/*
2816 * authentication
2817 */
Alex Eldera3530df2012-05-16 15:16:39 -05002818/*
2819 * Note: returned pointer is the address of a structure that's
2820 * managed separately. Caller must *not* attempt to free it.
2821 */
2822static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002823 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002824{
2825 struct ceph_osd *o = con->private;
2826 struct ceph_osd_client *osdc = o->o_osdc;
2827 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002828 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002829
Alex Elder74f18692012-05-16 15:16:39 -05002830 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002831 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002832 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002833 }
Sage Weil27859f92013-03-25 10:26:14 -07002834 if (!auth->authorizer) {
2835 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2836 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002837 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002838 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002839 } else {
2840 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002841 auth);
2842 if (ret)
2843 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002844 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002845 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002846
Alex Eldera3530df2012-05-16 15:16:39 -05002847 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002848}
2849
2850
2851static int verify_authorizer_reply(struct ceph_connection *con, int len)
2852{
2853 struct ceph_osd *o = con->private;
2854 struct ceph_osd_client *osdc = o->o_osdc;
2855 struct ceph_auth_client *ac = osdc->client->monc.auth;
2856
Sage Weil27859f92013-03-25 10:26:14 -07002857 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002858}
2859
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002860static int invalidate_authorizer(struct ceph_connection *con)
2861{
2862 struct ceph_osd *o = con->private;
2863 struct ceph_osd_client *osdc = o->o_osdc;
2864 struct ceph_auth_client *ac = osdc->client->monc.auth;
2865
Sage Weil27859f92013-03-25 10:26:14 -07002866 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002867 return ceph_monc_validate_auth(&osdc->client->monc);
2868}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002869
Tobias Klauser9e327892010-05-20 10:40:19 +02002870static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002871 .get = get_osd_con,
2872 .put = put_osd_con,
2873 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002874 .get_authorizer = get_authorizer,
2875 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002876 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002877 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002878 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002879};