blob: 71830d79b0f4fc63f45cf8474db190dec3037249 [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:
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200439 case CEPH_OSD_OP_SETALLOCHINT:
Alex Eldera8dd0a32013-03-13 20:50:00 -0500440 case CEPH_OSD_OP_CLONERANGE:
441 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
442 case CEPH_OSD_OP_SRC_CMPXATTR:
443 case CEPH_OSD_OP_GETXATTR:
444 case CEPH_OSD_OP_GETXATTRS:
445 case CEPH_OSD_OP_CMPXATTR:
446 case CEPH_OSD_OP_SETXATTR:
447 case CEPH_OSD_OP_SETXATTRS:
448 case CEPH_OSD_OP_RESETXATTRS:
449 case CEPH_OSD_OP_RMXATTR:
450 case CEPH_OSD_OP_PULL:
451 case CEPH_OSD_OP_PUSH:
452 case CEPH_OSD_OP_BALANCEREADS:
453 case CEPH_OSD_OP_UNBALANCEREADS:
454 case CEPH_OSD_OP_SCRUB:
455 case CEPH_OSD_OP_SCRUB_RESERVE:
456 case CEPH_OSD_OP_SCRUB_UNRESERVE:
457 case CEPH_OSD_OP_SCRUB_STOP:
458 case CEPH_OSD_OP_SCRUB_MAP:
459 case CEPH_OSD_OP_WRLOCK:
460 case CEPH_OSD_OP_WRUNLOCK:
461 case CEPH_OSD_OP_RDLOCK:
462 case CEPH_OSD_OP_RDUNLOCK:
463 case CEPH_OSD_OP_UPLOCK:
464 case CEPH_OSD_OP_DNLOCK:
465 case CEPH_OSD_OP_CALL:
466 case CEPH_OSD_OP_PGLS:
467 case CEPH_OSD_OP_PGLS_FILTER:
468 return true;
469 default:
470 return false;
471 }
472}
473
Alex Elder33803f32013-03-13 20:50:00 -0500474/*
475 * This is an osd op init function for opcodes that have no data or
476 * other information associated with them. It also serves as a
477 * common init routine for all the other init functions, below.
478 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500479static struct ceph_osd_req_op *
Alex Elder49719772013-02-11 12:33:24 -0600480_osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elderc99d2d42013-04-05 01:27:11 -0500481 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500482{
Alex Elderc99d2d42013-04-05 01:27:11 -0500483 struct ceph_osd_req_op *op;
484
485 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500486 BUG_ON(!osd_req_opcode_valid(opcode));
487
Alex Elderc99d2d42013-04-05 01:27:11 -0500488 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500489 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500490 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500491
492 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500493}
494
Alex Elder49719772013-02-11 12:33:24 -0600495void osd_req_op_init(struct ceph_osd_request *osd_req,
496 unsigned int which, u16 opcode)
497{
498 (void)_osd_req_op_init(osd_req, which, opcode);
499}
500EXPORT_SYMBOL(osd_req_op_init);
501
Alex Elderc99d2d42013-04-05 01:27:11 -0500502void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
503 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500504 u64 offset, u64 length,
505 u64 truncate_size, u32 truncate_seq)
506{
Alex Elder49719772013-02-11 12:33:24 -0600507 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500508 size_t payload_len = 0;
509
Li Wangad7a60d2013-08-15 11:51:44 +0800510 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
511 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
512 opcode != CEPH_OSD_OP_TRUNCATE);
Alex Elder33803f32013-03-13 20:50:00 -0500513
Alex Elder33803f32013-03-13 20:50:00 -0500514 op->extent.offset = offset;
515 op->extent.length = length;
516 op->extent.truncate_size = truncate_size;
517 op->extent.truncate_seq = truncate_seq;
518 if (opcode == CEPH_OSD_OP_WRITE)
519 payload_len += length;
520
521 op->payload_len = payload_len;
522}
523EXPORT_SYMBOL(osd_req_op_extent_init);
524
Alex Elderc99d2d42013-04-05 01:27:11 -0500525void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
526 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500527{
Alex Elderc99d2d42013-04-05 01:27:11 -0500528 struct ceph_osd_req_op *op;
529 u64 previous;
530
531 BUG_ON(which >= osd_req->r_num_ops);
532 op = &osd_req->r_ops[which];
533 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500534
535 if (length == previous)
536 return; /* Nothing to do */
537 BUG_ON(length > previous);
538
539 op->extent.length = length;
540 op->payload_len -= previous - length;
541}
542EXPORT_SYMBOL(osd_req_op_extent_update);
543
Alex Elderc99d2d42013-04-05 01:27:11 -0500544void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
Alex Elder04017e22013-04-05 14:46:02 -0500545 u16 opcode, const char *class, const char *method)
Alex Elder33803f32013-03-13 20:50:00 -0500546{
Alex Elder49719772013-02-11 12:33:24 -0600547 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder5f562df2013-04-05 01:27:12 -0500548 struct ceph_pagelist *pagelist;
Alex Elder33803f32013-03-13 20:50:00 -0500549 size_t payload_len = 0;
550 size_t size;
551
552 BUG_ON(opcode != CEPH_OSD_OP_CALL);
553
Alex Elder5f562df2013-04-05 01:27:12 -0500554 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
555 BUG_ON(!pagelist);
556 ceph_pagelist_init(pagelist);
557
Alex Elder33803f32013-03-13 20:50:00 -0500558 op->cls.class_name = class;
559 size = strlen(class);
560 BUG_ON(size > (size_t) U8_MAX);
561 op->cls.class_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500562 ceph_pagelist_append(pagelist, class, size);
Alex Elder33803f32013-03-13 20:50:00 -0500563 payload_len += size;
564
565 op->cls.method_name = method;
566 size = strlen(method);
567 BUG_ON(size > (size_t) U8_MAX);
568 op->cls.method_len = size;
Alex Elder5f562df2013-04-05 01:27:12 -0500569 ceph_pagelist_append(pagelist, method, size);
Alex Elder33803f32013-03-13 20:50:00 -0500570 payload_len += size;
571
Alex Eldera4ce40a2013-04-05 01:27:12 -0500572 osd_req_op_cls_request_info_pagelist(osd_req, which, pagelist);
Alex Elder5f562df2013-04-05 01:27:12 -0500573
Alex Elder33803f32013-03-13 20:50:00 -0500574 op->cls.argc = 0; /* currently unused */
575
576 op->payload_len = payload_len;
577}
578EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elder8c042b02013-04-03 01:28:58 -0500579
Alex Elderc99d2d42013-04-05 01:27:11 -0500580void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
581 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500582 u64 cookie, u64 version, int flag)
583{
Alex Elder49719772013-02-11 12:33:24 -0600584 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500585
Alex Elderc99d2d42013-04-05 01:27:11 -0500586 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500587
588 op->watch.cookie = cookie;
Alex Elder9ef1ee52013-04-21 16:51:50 -0500589 op->watch.ver = version;
Alex Elder33803f32013-03-13 20:50:00 -0500590 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500591 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500592}
593EXPORT_SYMBOL(osd_req_op_watch_init);
594
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200595void osd_req_op_alloc_hint_init(struct ceph_osd_request *osd_req,
596 unsigned int which,
597 u64 expected_object_size,
598 u64 expected_write_size)
599{
600 struct ceph_osd_req_op *op = _osd_req_op_init(osd_req, which,
601 CEPH_OSD_OP_SETALLOCHINT);
602
603 op->alloc_hint.expected_object_size = expected_object_size;
604 op->alloc_hint.expected_write_size = expected_write_size;
605
606 /*
607 * CEPH_OSD_OP_SETALLOCHINT op is advisory and therefore deemed
608 * not worth a feature bit. Set FAILOK per-op flag to make
609 * sure older osds don't trip over an unsupported opcode.
610 */
611 op->flags |= CEPH_OSD_OP_FLAG_FAILOK;
612}
613EXPORT_SYMBOL(osd_req_op_alloc_hint_init);
614
Alex Elder90af3602013-04-05 14:46:01 -0500615static void ceph_osdc_msg_data_add(struct ceph_msg *msg,
Alex Elderec9123c2013-04-05 01:27:12 -0500616 struct ceph_osd_data *osd_data)
617{
618 u64 length = ceph_osd_data_length(osd_data);
619
620 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
621 BUG_ON(length > (u64) SIZE_MAX);
622 if (length)
Alex Elder90af3602013-04-05 14:46:01 -0500623 ceph_msg_data_add_pages(msg, osd_data->pages,
Alex Elderec9123c2013-04-05 01:27:12 -0500624 length, osd_data->alignment);
625 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
626 BUG_ON(!length);
Alex Elder90af3602013-04-05 14:46:01 -0500627 ceph_msg_data_add_pagelist(msg, osd_data->pagelist);
Alex Elderec9123c2013-04-05 01:27:12 -0500628#ifdef CONFIG_BLOCK
629 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder90af3602013-04-05 14:46:01 -0500630 ceph_msg_data_add_bio(msg, osd_data->bio, length);
Alex Elderec9123c2013-04-05 01:27:12 -0500631#endif
632 } else {
633 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
634 }
635}
636
Alex Elder175face2013-03-08 13:35:36 -0600637static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500638 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700639{
Alex Elder79528732013-04-03 21:32:51 -0500640 struct ceph_osd_req_op *src;
Alex Elder04017e22013-04-05 14:46:02 -0500641 struct ceph_osd_data *osd_data;
Alex Elder54d50642013-04-03 01:28:58 -0500642 u64 request_data_len = 0;
Alex Elder04017e22013-04-05 14:46:02 -0500643 u64 data_length;
Alex Elder175face2013-03-08 13:35:36 -0600644
Alex Elder79528732013-04-03 21:32:51 -0500645 BUG_ON(which >= req->r_num_ops);
646 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500647 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
648 pr_err("unrecognized osd opcode %d\n", src->op);
649
650 return 0;
651 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700652
Alex Elder065a68f2012-04-20 15:49:43 -0500653 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600654 case CEPH_OSD_OP_STAT:
Alex Elder49719772013-02-11 12:33:24 -0600655 osd_data = &src->raw_data_in;
656 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Alex Elderfbfab532013-02-08 09:55:48 -0600657 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700658 case CEPH_OSD_OP_READ:
659 case CEPH_OSD_OP_WRITE:
Li Wangad7a60d2013-08-15 11:51:44 +0800660 case CEPH_OSD_OP_ZERO:
661 case CEPH_OSD_OP_DELETE:
662 case CEPH_OSD_OP_TRUNCATE:
Alex Elder175face2013-03-08 13:35:36 -0600663 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500664 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600665 dst->extent.offset = cpu_to_le64(src->extent.offset);
666 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700667 dst->extent.truncate_size =
668 cpu_to_le64(src->extent.truncate_size);
669 dst->extent.truncate_seq =
670 cpu_to_le32(src->extent.truncate_seq);
Alex Elder04017e22013-04-05 14:46:02 -0500671 osd_data = &src->extent.osd_data;
Alex Elder54764922013-04-05 01:27:12 -0500672 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder04017e22013-04-05 14:46:02 -0500673 ceph_osdc_msg_data_add(req->r_request, osd_data);
Alex Elder54764922013-04-05 01:27:12 -0500674 else
Alex Elder04017e22013-04-05 14:46:02 -0500675 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700676 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700677 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700678 dst->cls.class_len = src->cls.class_len;
679 dst->cls.method_len = src->cls.method_len;
Alex Elder04017e22013-04-05 14:46:02 -0500680 osd_data = &src->cls.request_info;
681 ceph_osdc_msg_data_add(req->r_request, osd_data);
682 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_PAGELIST);
683 request_data_len = osd_data->pagelist->length;
684
685 osd_data = &src->cls.request_data;
686 data_length = ceph_osd_data_length(osd_data);
687 if (data_length) {
688 BUG_ON(osd_data->type == CEPH_OSD_DATA_TYPE_NONE);
689 dst->cls.indata_len = cpu_to_le32(data_length);
690 ceph_osdc_msg_data_add(req->r_request, osd_data);
691 src->payload_len += data_length;
692 request_data_len += data_length;
693 }
694 osd_data = &src->cls.response_data;
695 ceph_osdc_msg_data_add(req->r_reply, osd_data);
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700696 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700697 case CEPH_OSD_OP_STARTSYNC:
698 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700699 case CEPH_OSD_OP_NOTIFY_ACK:
700 case CEPH_OSD_OP_WATCH:
701 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
702 dst->watch.ver = cpu_to_le64(src->watch.ver);
703 dst->watch.flag = src->watch.flag;
704 break;
Ilya Dryomovc647b8a2014-02-25 16:22:27 +0200705 case CEPH_OSD_OP_SETALLOCHINT:
706 dst->alloc_hint.expected_object_size =
707 cpu_to_le64(src->alloc_hint.expected_object_size);
708 dst->alloc_hint.expected_write_size =
709 cpu_to_le64(src->alloc_hint.expected_write_size);
710 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700711 default:
Alex Elder4c464592013-02-15 11:42:30 -0600712 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600713 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600714 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500715
716 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700717 }
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200718
Alex Eldera8dd0a32013-03-13 20:50:00 -0500719 dst->op = cpu_to_le16(src->op);
Ilya Dryomov7b25bf52014-02-25 16:22:26 +0200720 dst->flags = cpu_to_le32(src->flags);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700721 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600722
Alex Elder54d50642013-04-03 01:28:58 -0500723 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700724}
725
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700726/*
Sage Weilf24e9982009-10-06 11:31:10 -0700727 * build new request AND message, calculate layout, and adjust file
728 * extent as needed.
729 *
730 * if the file was recently truncated, we include information about its
731 * old and new size so that the object can be updated appropriately. (we
732 * avoid synchronously deleting truncated objects because it's slow.)
733 *
734 * if @do_sync, include a 'startsync' command so that the osd will flush
735 * data quickly.
736 */
737struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
738 struct ceph_file_layout *layout,
739 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500740 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700741 int opcode, int flags,
742 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700743 u32 truncate_seq,
744 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600745 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700746{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700747 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500748 u64 objnum = 0;
749 u64 objoff = 0;
750 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500751 u32 object_size;
752 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700753 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700754
Li Wangad7a60d2013-08-15 11:51:44 +0800755 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE &&
756 opcode != CEPH_OSD_OP_DELETE && opcode != CEPH_OSD_OP_ZERO &&
757 opcode != CEPH_OSD_OP_TRUNCATE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700758
Alex Elderacead002013-03-14 14:09:05 -0500759 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600760 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700761 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700762 return ERR_PTR(-ENOMEM);
Alex Elder79528732013-04-03 21:32:51 -0500763
Alex Elderd178a9e2012-11-13 21:11:15 -0600764 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700765
766 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500767 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600768 if (r < 0) {
769 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700770 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600771 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500772
Alex Elderd18d1e22013-03-13 20:50:01 -0500773 object_size = le32_to_cpu(layout->fl_object_size);
774 object_base = off - objoff;
Yan, Zhengccca4e32013-06-02 18:40:23 +0800775 if (!(truncate_seq == 1 && truncate_size == -1ULL)) {
776 if (truncate_size <= object_base) {
777 truncate_size = 0;
778 } else {
779 truncate_size -= object_base;
780 if (truncate_size > object_size)
781 truncate_size = object_size;
782 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500783 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500784
Alex Elderc99d2d42013-04-05 01:27:11 -0500785 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500786 truncate_size, truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500787
Alex Elderacead002013-03-14 14:09:05 -0500788 /*
789 * A second op in the ops array means the caller wants to
790 * also issue a include a 'startsync' command so that the
791 * osd will flush data quickly.
792 */
793 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500794 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500795
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200796 req->r_base_oloc.pool = ceph_file_layout_pg_pool(*layout);
Sage Weilf24e9982009-10-06 11:31:10 -0700797
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200798 snprintf(req->r_base_oid.name, sizeof(req->r_base_oid.name),
Ilya Dryomov4295f222014-01-27 17:40:18 +0200799 "%llx.%08llx", vino.ino, objnum);
Ilya Dryomov3c972c92014-01-27 17:40:20 +0200800 req->r_base_oid.name_len = strlen(req->r_base_oid.name);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600801
Sage Weilf24e9982009-10-06 11:31:10 -0700802 return req;
803}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700804EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700805
806/*
807 * We keep osd requests in an rbtree, sorted by ->r_tid.
808 */
809static void __insert_request(struct ceph_osd_client *osdc,
810 struct ceph_osd_request *new)
811{
812 struct rb_node **p = &osdc->requests.rb_node;
813 struct rb_node *parent = NULL;
814 struct ceph_osd_request *req = NULL;
815
816 while (*p) {
817 parent = *p;
818 req = rb_entry(parent, struct ceph_osd_request, r_node);
819 if (new->r_tid < req->r_tid)
820 p = &(*p)->rb_left;
821 else if (new->r_tid > req->r_tid)
822 p = &(*p)->rb_right;
823 else
824 BUG();
825 }
826
827 rb_link_node(&new->r_node, parent, p);
828 rb_insert_color(&new->r_node, &osdc->requests);
829}
830
831static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
832 u64 tid)
833{
834 struct ceph_osd_request *req;
835 struct rb_node *n = osdc->requests.rb_node;
836
837 while (n) {
838 req = rb_entry(n, struct ceph_osd_request, r_node);
839 if (tid < req->r_tid)
840 n = n->rb_left;
841 else if (tid > req->r_tid)
842 n = n->rb_right;
843 else
844 return req;
845 }
846 return NULL;
847}
848
849static struct ceph_osd_request *
850__lookup_request_ge(struct ceph_osd_client *osdc,
851 u64 tid)
852{
853 struct ceph_osd_request *req;
854 struct rb_node *n = osdc->requests.rb_node;
855
856 while (n) {
857 req = rb_entry(n, struct ceph_osd_request, r_node);
858 if (tid < req->r_tid) {
859 if (!n->rb_left)
860 return req;
861 n = n->rb_left;
862 } else if (tid > req->r_tid) {
863 n = n->rb_right;
864 } else {
865 return req;
866 }
867 }
868 return NULL;
869}
870
Sage Weil6f6c7002011-01-17 20:34:08 -0800871/*
872 * Resubmit requests pending on the given osd.
873 */
874static void __kick_osd_requests(struct ceph_osd_client *osdc,
875 struct ceph_osd *osd)
876{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700877 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500878 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800879 int err;
880
881 dout("__kick_osd_requests osd%d\n", osd->o_osd);
882 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600883 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800884 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500885 /*
886 * Build up a list of requests to resend by traversing the
887 * osd's list of requests. Requests for a given object are
888 * sent in tid order, and that is also the order they're
889 * kept on this list. Therefore all requests that are in
890 * flight will be found first, followed by all requests that
891 * have not yet been sent. And to resend requests while
892 * preserving this order we will want to put any sent
893 * requests back on the front of the osd client's unsent
894 * list.
895 *
896 * So we build a separate ordered list of already-sent
897 * requests for the affected osd and splice it onto the
898 * front of the osd client's unsent list. Once we've seen a
899 * request that has not yet been sent we're done. Those
900 * requests are already sitting right where they belong.
901 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800902 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500903 if (!req->r_sent)
904 break;
905 list_move_tail(&req->r_req_lru_item, &resend);
906 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800907 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700908 if (!req->r_linger)
909 req->r_flags |= CEPH_OSD_FLAG_RETRY;
910 }
Alex Eldere02493c2013-03-25 18:16:11 -0500911 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700912
Alex Eldere02493c2013-03-25 18:16:11 -0500913 /*
914 * Linger requests are re-registered before sending, which
915 * sets up a new tid for each. We add them to the unsent
916 * list at the end to keep things in tid order.
917 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700918 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
919 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700920 /*
921 * reregister request prior to unregistering linger so
922 * that r_osd is preserved.
923 */
924 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700925 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500926 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500927 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700928 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700929 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
930 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800931 }
932}
933
Sage Weilf24e9982009-10-06 11:31:10 -0700934/*
Sage Weil81b024e2009-10-09 10:29:18 -0700935 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700936 */
937static void osd_reset(struct ceph_connection *con)
938{
939 struct ceph_osd *osd = con->private;
940 struct ceph_osd_client *osdc;
941
942 if (!osd)
943 return;
944 dout("osd_reset osd%d\n", osd->o_osd);
945 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700946 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800947 mutex_lock(&osdc->request_mutex);
948 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600949 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800950 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700951 up_read(&osdc->map_sem);
952}
953
954/*
955 * Track open sessions with osds.
956 */
Alex Eldere10006f2012-05-26 23:26:43 -0500957static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700958{
959 struct ceph_osd *osd;
960
961 osd = kzalloc(sizeof(*osd), GFP_NOFS);
962 if (!osd)
963 return NULL;
964
965 atomic_set(&osd->o_ref, 1);
966 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500967 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600968 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700969 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700970 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800971 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700972 osd->o_incarnation = 1;
973
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700974 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800975
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800976 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700977 return osd;
978}
979
980static struct ceph_osd *get_osd(struct ceph_osd *osd)
981{
982 if (atomic_inc_not_zero(&osd->o_ref)) {
983 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
984 atomic_read(&osd->o_ref));
985 return osd;
986 } else {
987 dout("get_osd %p FAIL\n", osd);
988 return NULL;
989 }
990}
991
992static void put_osd(struct ceph_osd *osd)
993{
994 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
995 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500996 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700997 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
998
Sage Weil27859f92013-03-25 10:26:14 -0700999 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -07001000 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -07001001 }
Sage Weilf24e9982009-10-06 11:31:10 -07001002}
1003
1004/*
1005 * remove an osd from our map
1006 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001007static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001008{
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001009 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 BUG_ON(!list_empty(&osd->o_requests));
1011 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001012 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001013 ceph_con_close(&osd->o_con);
1014 put_osd(osd);
1015}
1016
Sage Weilaca420b2011-08-31 14:45:53 -07001017static void remove_all_osds(struct ceph_osd_client *osdc)
1018{
Jiaju Zhang048a9d22012-07-20 08:18:36 -05001019 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -07001020 mutex_lock(&osdc->request_mutex);
1021 while (!RB_EMPTY_ROOT(&osdc->osds)) {
1022 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
1023 struct ceph_osd, o_node);
1024 __remove_osd(osdc, osd);
1025 }
1026 mutex_unlock(&osdc->request_mutex);
1027}
1028
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001029static void __move_osd_to_lru(struct ceph_osd_client *osdc,
1030 struct ceph_osd *osd)
1031{
1032 dout("__move_osd_to_lru %p\n", osd);
1033 BUG_ON(!list_empty(&osd->o_osd_lru));
1034 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001035 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001036}
1037
1038static void __remove_osd_from_lru(struct ceph_osd *osd)
1039{
1040 dout("__remove_osd_from_lru %p\n", osd);
1041 if (!list_empty(&osd->o_osd_lru))
1042 list_del_init(&osd->o_osd_lru);
1043}
1044
Sage Weilaca420b2011-08-31 14:45:53 -07001045static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001046{
1047 struct ceph_osd *osd, *nosd;
1048
1049 dout("__remove_old_osds %p\n", osdc);
1050 mutex_lock(&osdc->request_mutex);
1051 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -07001052 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001053 break;
1054 __remove_osd(osdc, osd);
1055 }
1056 mutex_unlock(&osdc->request_mutex);
1057}
1058
Sage Weilf24e9982009-10-06 11:31:10 -07001059/*
1060 * reset osd connect
1061 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001062static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -07001063{
Alex Elderc3acb182012-12-07 09:57:58 -06001064 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -07001065
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001066 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001067 if (list_empty(&osd->o_requests) &&
1068 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001069 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -06001070
1071 return -ENODEV;
1072 }
1073
1074 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
1075 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
1076 !ceph_con_opened(&osd->o_con)) {
1077 struct ceph_osd_request *req;
1078
Ilya Dryomov0b4af2e2014-01-16 19:18:27 +02001079 dout("osd addr hasn't changed and connection never opened, "
1080 "letting msgr retry\n");
Sage Weil87b315a2010-03-22 14:51:18 -07001081 /* touch each r_stamp for handle_timeout()'s benfit */
1082 list_for_each_entry(req, &osd->o_requests, r_osd_item)
1083 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -06001084
1085 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -07001086 }
Alex Elderc3acb182012-12-07 09:57:58 -06001087
1088 ceph_con_close(&osd->o_con);
1089 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
1090 osd->o_incarnation++;
1091
1092 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001093}
1094
1095static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
1096{
1097 struct rb_node **p = &osdc->osds.rb_node;
1098 struct rb_node *parent = NULL;
1099 struct ceph_osd *osd = NULL;
1100
Sage Weilaca420b2011-08-31 14:45:53 -07001101 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001102 while (*p) {
1103 parent = *p;
1104 osd = rb_entry(parent, struct ceph_osd, o_node);
1105 if (new->o_osd < osd->o_osd)
1106 p = &(*p)->rb_left;
1107 else if (new->o_osd > osd->o_osd)
1108 p = &(*p)->rb_right;
1109 else
1110 BUG();
1111 }
1112
1113 rb_link_node(&new->o_node, parent, p);
1114 rb_insert_color(&new->o_node, &osdc->osds);
1115}
1116
1117static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1118{
1119 struct ceph_osd *osd;
1120 struct rb_node *n = osdc->osds.rb_node;
1121
1122 while (n) {
1123 osd = rb_entry(n, struct ceph_osd, o_node);
1124 if (o < osd->o_osd)
1125 n = n->rb_left;
1126 else if (o > osd->o_osd)
1127 n = n->rb_right;
1128 else
1129 return osd;
1130 }
1131 return NULL;
1132}
1133
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001134static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1135{
1136 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001137 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001138}
1139
1140static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1141{
1142 cancel_delayed_work(&osdc->timeout_work);
1143}
Sage Weilf24e9982009-10-06 11:31:10 -07001144
1145/*
1146 * Register request, assign tid. If this is the first request, set up
1147 * the timeout event.
1148 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001149static void __register_request(struct ceph_osd_client *osdc,
1150 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001151{
Sage Weilf24e9982009-10-06 11:31:10 -07001152 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001153 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001154 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001155 __insert_request(osdc, req);
1156 ceph_osdc_get_request(req);
1157 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001158 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001159 dout(" first request, scheduling timeout\n");
1160 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001161 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001162}
1163
Sage Weilf24e9982009-10-06 11:31:10 -07001164/*
1165 * called under osdc->request_mutex
1166 */
1167static void __unregister_request(struct ceph_osd_client *osdc,
1168 struct ceph_osd_request *req)
1169{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001170 if (RB_EMPTY_NODE(&req->r_node)) {
1171 dout("__unregister_request %p tid %lld not registered\n",
1172 req, req->r_tid);
1173 return;
1174 }
1175
Sage Weilf24e9982009-10-06 11:31:10 -07001176 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1177 rb_erase(&req->r_node, &osdc->requests);
1178 osdc->num_requests--;
1179
Sage Weil0ba64782009-10-08 16:57:16 -07001180 if (req->r_osd) {
1181 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001182 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001183
1184 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001185 if (list_empty(&req->r_osd->o_requests) &&
1186 list_empty(&req->r_osd->o_linger_requests)) {
1187 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001188 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001189 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001190 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001191 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001192 }
Sage Weilf24e9982009-10-06 11:31:10 -07001193
Alex Elder7d5f2482012-11-29 08:37:03 -06001194 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001195 ceph_osdc_put_request(req);
1196
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001197 if (osdc->num_requests == 0) {
1198 dout(" no requests, canceling timeout\n");
1199 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001200 }
1201}
1202
1203/*
1204 * Cancel a previously queued request message
1205 */
1206static void __cancel_request(struct ceph_osd_request *req)
1207{
Sage Weil6bc18872010-09-27 10:18:52 -07001208 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001209 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001210 req->r_sent = 0;
1211 }
1212}
1213
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001214static void __register_linger_request(struct ceph_osd_client *osdc,
1215 struct ceph_osd_request *req)
1216{
1217 dout("__register_linger_request %p\n", req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001218 ceph_osdc_get_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001219 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001220 if (req->r_osd)
1221 list_add_tail(&req->r_linger_osd,
1222 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001223}
1224
1225static void __unregister_linger_request(struct ceph_osd_client *osdc,
1226 struct ceph_osd_request *req)
1227{
1228 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001229 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001230 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001231 list_del_init(&req->r_linger_osd);
1232
1233 if (list_empty(&req->r_osd->o_requests) &&
1234 list_empty(&req->r_osd->o_linger_requests)) {
1235 dout("moving osd to %p lru\n", req->r_osd);
1236 __move_osd_to_lru(osdc, req->r_osd);
1237 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001238 if (list_empty(&req->r_osd_item))
1239 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001240 }
Alex Elder96e4dac2013-05-22 20:54:25 -05001241 ceph_osdc_put_request(req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001242}
1243
1244void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1245 struct ceph_osd_request *req)
1246{
1247 mutex_lock(&osdc->request_mutex);
1248 if (req->r_linger) {
Alex Elderc10ebbf2013-05-09 14:56:32 -05001249 req->r_linger = 0;
Alex Elder96e4dac2013-05-22 20:54:25 -05001250 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001251 }
1252 mutex_unlock(&osdc->request_mutex);
1253}
1254EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1255
1256void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1257 struct ceph_osd_request *req)
1258{
1259 if (!req->r_linger) {
1260 dout("set_request_linger %p\n", req);
1261 req->r_linger = 1;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001262 }
1263}
1264EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1265
Sage Weilf24e9982009-10-06 11:31:10 -07001266/*
Josh Durgind29adb32013-12-02 19:11:48 -08001267 * Returns whether a request should be blocked from being sent
1268 * based on the current osdmap and osd_client settings.
1269 *
1270 * Caller should hold map_sem for read.
1271 */
1272static bool __req_should_be_paused(struct ceph_osd_client *osdc,
1273 struct ceph_osd_request *req)
1274{
1275 bool pauserd = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD);
1276 bool pausewr = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR) ||
1277 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
1278 return (req->r_flags & CEPH_OSD_FLAG_READ && pauserd) ||
1279 (req->r_flags & CEPH_OSD_FLAG_WRITE && pausewr);
1280}
1281
1282/*
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001283 * Calculate mapping of a request to a PG. Takes tiering into account.
1284 */
1285static int __calc_request_pg(struct ceph_osdmap *osdmap,
1286 struct ceph_osd_request *req,
1287 struct ceph_pg *pg_out)
1288{
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001289 bool need_check_tiering;
1290
1291 need_check_tiering = false;
1292 if (req->r_target_oloc.pool == -1) {
1293 req->r_target_oloc = req->r_base_oloc; /* struct */
1294 need_check_tiering = true;
1295 }
1296 if (req->r_target_oid.name_len == 0) {
1297 ceph_oid_copy(&req->r_target_oid, &req->r_base_oid);
1298 need_check_tiering = true;
1299 }
1300
1301 if (need_check_tiering &&
1302 (req->r_flags & CEPH_OSD_FLAG_IGNORE_OVERLAY) == 0) {
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001303 struct ceph_pg_pool_info *pi;
1304
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001305 pi = ceph_pg_pool_by_id(osdmap, req->r_target_oloc.pool);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001306 if (pi) {
1307 if ((req->r_flags & CEPH_OSD_FLAG_READ) &&
1308 pi->read_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001309 req->r_target_oloc.pool = pi->read_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001310 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) &&
1311 pi->write_tier >= 0)
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001312 req->r_target_oloc.pool = pi->write_tier;
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001313 }
1314 /* !pi is caught in ceph_oloc_oid_to_pg() */
1315 }
1316
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001317 return ceph_oloc_oid_to_pg(osdmap, &req->r_target_oloc,
1318 &req->r_target_oid, pg_out);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001319}
1320
1321/*
Sage Weilf24e9982009-10-06 11:31:10 -07001322 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1323 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001324 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001325 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001326 *
1327 * Return 0 if unchanged, 1 if changed, or negative on error.
1328 *
1329 * Caller should hold map_sem for read and request_mutex.
1330 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001331static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001332 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001333{
Sage Weil5b191d92013-02-23 10:38:16 -08001334 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001335 int acting[CEPH_PG_MAX_SIZE];
1336 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001337 int err;
Josh Durgind29adb32013-12-02 19:11:48 -08001338 bool was_paused;
Sage Weilf24e9982009-10-06 11:31:10 -07001339
Sage Weil6f6c7002011-01-17 20:34:08 -08001340 dout("map_request %p tid %lld\n", req, req->r_tid);
Ilya Dryomov17a13e402014-01-27 17:40:19 +02001341
1342 err = __calc_request_pg(osdc->osdmap, req, &pgid);
Sage Weil6f6c7002011-01-17 20:34:08 -08001343 if (err) {
1344 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001345 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001346 }
Sage Weil7740a422010-01-08 15:58:25 -08001347 req->r_pgid = pgid;
1348
Sage Weild85b7052010-05-10 10:24:48 -07001349 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1350 if (err > 0) {
1351 o = acting[0];
1352 num = err;
1353 }
Sage Weilf24e9982009-10-06 11:31:10 -07001354
Josh Durgind29adb32013-12-02 19:11:48 -08001355 was_paused = req->r_paused;
1356 req->r_paused = __req_should_be_paused(osdc, req);
1357 if (was_paused && !req->r_paused)
1358 force_resend = 1;
1359
Sage Weil38d64532011-10-14 13:33:55 -07001360 if ((!force_resend &&
1361 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001362 req->r_sent >= req->r_osd->o_incarnation &&
1363 req->r_num_pg_osds == num &&
1364 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Josh Durgind29adb32013-12-02 19:11:48 -08001365 (req->r_osd == NULL && o == -1) ||
1366 req->r_paused)
Sage Weilf24e9982009-10-06 11:31:10 -07001367 return 0; /* no change */
1368
Sage Weil5b191d92013-02-23 10:38:16 -08001369 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1370 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001371 req->r_osd ? req->r_osd->o_osd : -1);
1372
Sage Weild85b7052010-05-10 10:24:48 -07001373 /* record full pg acting set */
1374 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1375 req->r_num_pg_osds = num;
1376
Sage Weilf24e9982009-10-06 11:31:10 -07001377 if (req->r_osd) {
1378 __cancel_request(req);
1379 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001380 req->r_osd = NULL;
1381 }
1382
1383 req->r_osd = __lookup_osd(osdc, o);
1384 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001385 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001386 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001387 if (!req->r_osd) {
1388 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001389 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001390 }
Sage Weilf24e9982009-10-06 11:31:10 -07001391
Sage Weil6f6c7002011-01-17 20:34:08 -08001392 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001393 __insert_osd(osdc, req->r_osd);
1394
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001395 ceph_con_open(&req->r_osd->o_con,
1396 CEPH_ENTITY_TYPE_OSD, o,
1397 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001398 }
1399
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001400 if (req->r_osd) {
1401 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001402 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1403 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001404 } else {
Alex Elderad885922013-03-25 18:16:11 -05001405 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001406 }
Sage Weild85b7052010-05-10 10:24:48 -07001407 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001408
1409out:
Sage Weilf24e9982009-10-06 11:31:10 -07001410 return err;
1411}
1412
1413/*
1414 * caller should hold map_sem (for read) and request_mutex
1415 */
Sage Weil56e925b2012-01-03 12:34:34 -08001416static void __send_request(struct ceph_osd_client *osdc,
1417 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001418{
Sage Weil1b83bef2013-02-25 16:11:12 -08001419 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001420
Sage Weil1b83bef2013-02-25 16:11:12 -08001421 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1422 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1423 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001424
Sage Weil1b83bef2013-02-25 16:11:12 -08001425 /* fill in message content that changes each time we send it */
1426 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1427 put_unaligned_le32(req->r_flags, req->r_request_flags);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001428 put_unaligned_le64(req->r_target_oloc.pool, req->r_request_pool);
Sage Weil1b83bef2013-02-25 16:11:12 -08001429 p = req->r_request_pgid;
1430 ceph_encode_64(&p, req->r_pgid.pool);
1431 ceph_encode_32(&p, req->r_pgid.seed);
1432 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1433 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1434 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001435
Sage Weil3dd72fc2010-03-22 14:42:30 -07001436 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001437 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001438
1439 ceph_msg_get(req->r_request); /* send consumes a ref */
Alex Elder26be8802013-04-15 11:20:42 -05001440
Sage Weilf24e9982009-10-06 11:31:10 -07001441 req->r_sent = req->r_osd->o_incarnation;
Alex Elder26be8802013-04-15 11:20:42 -05001442
1443 ceph_con_send(&req->r_osd->o_con, req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001444}
1445
1446/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001447 * Send any requests in the queue (req_unsent).
1448 */
Alex Elderf9d25192013-02-15 11:42:29 -06001449static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001450{
1451 struct ceph_osd_request *req, *tmp;
1452
Alex Elderf9d25192013-02-15 11:42:29 -06001453 dout("__send_queued\n");
1454 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001455 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001456}
1457
1458/*
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02001459 * Caller should hold map_sem for read and request_mutex.
1460 */
1461static int __ceph_osdc_start_request(struct ceph_osd_client *osdc,
1462 struct ceph_osd_request *req,
1463 bool nofail)
1464{
1465 int rc;
1466
1467 __register_request(osdc, req);
1468 req->r_sent = 0;
1469 req->r_got_reply = 0;
1470 rc = __map_request(osdc, req, 0);
1471 if (rc < 0) {
1472 if (nofail) {
1473 dout("osdc_start_request failed map, "
1474 " will retry %lld\n", req->r_tid);
1475 rc = 0;
1476 } else {
1477 __unregister_request(osdc, req);
1478 }
1479 return rc;
1480 }
1481
1482 if (req->r_osd == NULL) {
1483 dout("send_request %p no up osds in pg\n", req);
1484 ceph_monc_request_next_osdmap(&osdc->client->monc);
1485 } else {
1486 __send_queued(osdc);
1487 }
1488
1489 return 0;
1490}
1491
1492/*
Sage Weilf24e9982009-10-06 11:31:10 -07001493 * Timeout callback, called every N seconds when 1 or more osd
1494 * requests has been active for more than N seconds. When this
1495 * happens, we ping all OSDs with requests who have timed out to
1496 * ensure any communications channel reset is detected. Reset the
1497 * request timeouts another N seconds in the future as we go.
1498 * Reschedule the timeout event another N seconds in future (unless
1499 * there are no open requests).
1500 */
1501static void handle_timeout(struct work_struct *work)
1502{
1503 struct ceph_osd_client *osdc =
1504 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001505 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001506 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001507 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001508 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001509 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001510 dout("timeout\n");
1511 down_read(&osdc->map_sem);
1512
1513 ceph_monc_request_next_osdmap(&osdc->client->monc);
1514
1515 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001516
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001517 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001518 * ping osds that are a bit slow. this ensures that if there
1519 * is a break in the TCP connection we will notice, and reopen
1520 * a connection with that osd (from the fault callback).
1521 */
1522 INIT_LIST_HEAD(&slow_osds);
1523 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001524 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001525 break;
1526
1527 osd = req->r_osd;
1528 BUG_ON(!osd);
1529 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001530 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001531 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1532 }
1533 while (!list_empty(&slow_osds)) {
1534 osd = list_entry(slow_osds.next, struct ceph_osd,
1535 o_keepalive_item);
1536 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001537 ceph_con_keepalive(&osd->o_con);
1538 }
1539
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001540 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001541 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001542 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001543 up_read(&osdc->map_sem);
1544}
1545
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001546static void handle_osds_timeout(struct work_struct *work)
1547{
1548 struct ceph_osd_client *osdc =
1549 container_of(work, struct ceph_osd_client,
1550 osds_timeout_work.work);
1551 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001552 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001553
1554 dout("osds timeout\n");
1555 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001556 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001557 up_read(&osdc->map_sem);
1558
1559 schedule_delayed_work(&osdc->osds_timeout_work,
1560 round_jiffies_relative(delay));
1561}
1562
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001563static int ceph_oloc_decode(void **p, void *end,
1564 struct ceph_object_locator *oloc)
1565{
1566 u8 struct_v, struct_cv;
1567 u32 len;
1568 void *struct_end;
1569 int ret = 0;
1570
1571 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1572 struct_v = ceph_decode_8(p);
1573 struct_cv = ceph_decode_8(p);
1574 if (struct_v < 3) {
1575 pr_warn("got v %d < 3 cv %d of ceph_object_locator\n",
1576 struct_v, struct_cv);
1577 goto e_inval;
1578 }
1579 if (struct_cv > 6) {
1580 pr_warn("got v %d cv %d > 6 of ceph_object_locator\n",
1581 struct_v, struct_cv);
1582 goto e_inval;
1583 }
1584 len = ceph_decode_32(p);
1585 ceph_decode_need(p, end, len, e_inval);
1586 struct_end = *p + len;
1587
1588 oloc->pool = ceph_decode_64(p);
1589 *p += 4; /* skip preferred */
1590
1591 len = ceph_decode_32(p);
1592 if (len > 0) {
1593 pr_warn("ceph_object_locator::key is set\n");
1594 goto e_inval;
1595 }
1596
1597 if (struct_v >= 5) {
1598 len = ceph_decode_32(p);
1599 if (len > 0) {
1600 pr_warn("ceph_object_locator::nspace is set\n");
1601 goto e_inval;
1602 }
1603 }
1604
1605 if (struct_v >= 6) {
1606 s64 hash = ceph_decode_64(p);
1607 if (hash != -1) {
1608 pr_warn("ceph_object_locator::hash is set\n");
1609 goto e_inval;
1610 }
1611 }
1612
1613 /* skip the rest */
1614 *p = struct_end;
1615out:
1616 return ret;
1617
1618e_inval:
1619 ret = -EINVAL;
1620 goto out;
1621}
1622
1623static int ceph_redirect_decode(void **p, void *end,
1624 struct ceph_request_redirect *redir)
1625{
1626 u8 struct_v, struct_cv;
1627 u32 len;
1628 void *struct_end;
1629 int ret;
1630
1631 ceph_decode_need(p, end, 1 + 1 + 4, e_inval);
1632 struct_v = ceph_decode_8(p);
1633 struct_cv = ceph_decode_8(p);
1634 if (struct_cv > 1) {
1635 pr_warn("got v %d cv %d > 1 of ceph_request_redirect\n",
1636 struct_v, struct_cv);
1637 goto e_inval;
1638 }
1639 len = ceph_decode_32(p);
1640 ceph_decode_need(p, end, len, e_inval);
1641 struct_end = *p + len;
1642
1643 ret = ceph_oloc_decode(p, end, &redir->oloc);
1644 if (ret)
1645 goto out;
1646
1647 len = ceph_decode_32(p);
1648 if (len > 0) {
1649 pr_warn("ceph_request_redirect::object_name is set\n");
1650 goto e_inval;
1651 }
1652
1653 len = ceph_decode_32(p);
1654 *p += len; /* skip osd_instructions */
1655
1656 /* skip the rest */
1657 *p = struct_end;
1658out:
1659 return ret;
1660
1661e_inval:
1662 ret = -EINVAL;
1663 goto out;
1664}
1665
Sage Weil25845472011-06-03 09:37:09 -07001666static void complete_request(struct ceph_osd_request *req)
1667{
Sage Weil25845472011-06-03 09:37:09 -07001668 complete_all(&req->r_safe_completion); /* fsync waiter */
1669}
1670
Sage Weilf24e9982009-10-06 11:31:10 -07001671/*
1672 * handle osd op reply. either call the callback if it is specified,
1673 * or do the completion to wake up the waiting thread.
1674 */
Sage Weil350b1c32009-12-22 10:45:45 -08001675static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1676 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001677{
Sage Weil1b83bef2013-02-25 16:11:12 -08001678 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001679 struct ceph_osd_request *req;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001680 struct ceph_request_redirect redir;
Sage Weilf24e9982009-10-06 11:31:10 -07001681 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001682 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001683 unsigned int numops;
1684 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001685 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001686 s32 retry_attempt;
1687 struct ceph_pg pg;
1688 int err;
1689 u32 reassert_epoch;
1690 u64 reassert_version;
1691 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001692 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001693 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001694 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001695
Sage Weil6df058c2009-12-22 11:24:33 -08001696 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001697 dout("handle_reply %p tid %llu\n", msg, tid);
1698
1699 p = msg->front.iov_base;
1700 end = p + msg->front.iov_len;
1701
1702 ceph_decode_need(&p, end, 4, bad);
1703 object_len = ceph_decode_32(&p);
1704 ceph_decode_need(&p, end, object_len, bad);
1705 p += object_len;
1706
Alex Elderef4859d2013-04-01 18:58:26 -05001707 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001708 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001709 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001710
1711 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1712 flags = ceph_decode_64(&p);
1713 result = ceph_decode_32(&p);
1714 reassert_epoch = ceph_decode_32(&p);
1715 reassert_version = ceph_decode_64(&p);
1716 osdmap_epoch = ceph_decode_32(&p);
1717
Sage Weilf24e9982009-10-06 11:31:10 -07001718 /* lookup */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001719 down_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001720 mutex_lock(&osdc->request_mutex);
1721 req = __lookup_request(osdc, tid);
1722 if (req == NULL) {
1723 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001724 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001725 }
1726 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001727
1728 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1729 req, result);
1730
Dan Carpenter18741192013-08-15 08:51:58 +03001731 ceph_decode_need(&p, end, 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001732 numops = ceph_decode_32(&p);
1733 if (numops > CEPH_OSD_MAX_OP)
1734 goto bad_put;
1735 if (numops != req->r_num_ops)
1736 goto bad_put;
1737 payload_len = 0;
Dan Carpenter18741192013-08-15 08:51:58 +03001738 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001739 for (i = 0; i < numops; i++) {
1740 struct ceph_osd_op *op = p;
1741 int len;
1742
1743 len = le32_to_cpu(op->payload_len);
1744 req->r_reply_op_len[i] = len;
1745 dout(" op %d has %d bytes\n", i, len);
1746 payload_len += len;
1747 p += sizeof(*op);
1748 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001749 bytes = le32_to_cpu(msg->hdr.data_len);
1750 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001751 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001752 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001753 goto bad_put;
1754 }
1755
Dan Carpenter18741192013-08-15 08:51:58 +03001756 ceph_decode_need(&p, end, 4 + numops * 4, bad_put);
Sage Weil1b83bef2013-02-25 16:11:12 -08001757 retry_attempt = ceph_decode_32(&p);
1758 for (i = 0; i < numops; i++)
1759 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001760
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001761 if (le16_to_cpu(msg->hdr.version) >= 6) {
1762 p += 8 + 4; /* skip replay_version */
1763 p += 8; /* skip user_version */
1764
1765 err = ceph_redirect_decode(&p, end, &redir);
1766 if (err)
1767 goto bad_put;
1768 } else {
1769 redir.oloc.pool = -1;
1770 }
1771
1772 if (redir.oloc.pool != -1) {
1773 dout("redirect pool %lld\n", redir.oloc.pool);
1774
1775 __unregister_request(osdc, req);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001776
1777 req->r_target_oloc = redir.oloc; /* struct */
1778
1779 /*
1780 * Start redirect requests with nofail=true. If
1781 * mapping fails, request will end up on the notarget
1782 * list, waiting for the new osdmap (which can take
1783 * a while), even though the original request mapped
1784 * successfully. In the future we might want to follow
1785 * original request's nofail setting here.
1786 */
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001787 err = __ceph_osdc_start_request(osdc, req, true);
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001788 BUG_ON(err);
1789
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001790 goto out_unlock;
Ilya Dryomov205ee1182014-01-27 17:40:20 +02001791 }
1792
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001793 already_completed = req->r_got_reply;
Sage Weilf24e9982009-10-06 11:31:10 -07001794 if (!req->r_got_reply) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001795 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001796 dout("handle_reply result %d bytes %d\n", req->r_result,
1797 bytes);
1798 if (req->r_result == 0)
1799 req->r_result = bytes;
1800
1801 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001802 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1803 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001804
1805 req->r_got_reply = 1;
1806 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1807 dout("handle_reply tid %llu dup ack\n", tid);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001808 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001809 }
1810
1811 dout("handle_reply tid %llu flags %d\n", tid, flags);
1812
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001813 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1814 __register_linger_request(osdc, req);
1815
Sage Weilf24e9982009-10-06 11:31:10 -07001816 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001817 if (result < 0 ||
1818 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001819 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1820 __unregister_request(osdc, req);
1821
1822 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001823 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001824
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001825 if (!already_completed) {
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001826 if (req->r_unsafe_callback &&
1827 result >= 0 && !(flags & CEPH_OSD_FLAG_ONDISK))
1828 req->r_unsafe_callback(req, true);
Yan, Zhengeb845ff2013-05-31 15:54:44 +08001829 if (req->r_callback)
1830 req->r_callback(req, msg);
1831 else
1832 complete_all(&req->r_completion);
1833 }
Sage Weilf24e9982009-10-06 11:31:10 -07001834
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001835 if (flags & CEPH_OSD_FLAG_ONDISK) {
1836 if (req->r_unsafe_callback && already_completed)
1837 req->r_unsafe_callback(req, false);
Sage Weil25845472011-06-03 09:37:09 -07001838 complete_request(req);
Yan, Zheng61c5d6b2013-06-24 14:41:27 +08001839 }
Sage Weilf24e9982009-10-06 11:31:10 -07001840
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001841out:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001842 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001843 ceph_osdc_put_request(req);
1844 return;
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001845out_unlock:
1846 mutex_unlock(&osdc->request_mutex);
1847 up_read(&osdc->map_sem);
1848 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001849
Sage Weil1b83bef2013-02-25 16:11:12 -08001850bad_put:
Li Wang37c89bd2013-11-27 22:28:14 +08001851 req->r_result = -EIO;
1852 __unregister_request(osdc, req);
1853 if (req->r_callback)
1854 req->r_callback(req, msg);
1855 else
1856 complete_all(&req->r_completion);
1857 complete_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001858 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001859bad_mutex:
1860 mutex_unlock(&osdc->request_mutex);
Ilya Dryomovff513ac2014-02-03 13:56:33 +02001861 up_read(&osdc->map_sem);
Sage Weilf24e9982009-10-06 11:31:10 -07001862bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001863 pr_err("corrupt osd_op_reply got %d %d\n",
1864 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001865 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001866}
1867
Sage Weil6f6c7002011-01-17 20:34:08 -08001868static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001869{
Sage Weilf24e9982009-10-06 11:31:10 -07001870 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001871
Sage Weil6f6c7002011-01-17 20:34:08 -08001872 for (p = rb_first(&osdc->osds); p; p = n) {
1873 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001874
Sage Weil6f6c7002011-01-17 20:34:08 -08001875 n = rb_next(p);
1876 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1877 memcmp(&osd->o_con.peer_addr,
1878 ceph_osd_addr(osdc->osdmap,
1879 osd->o_osd),
1880 sizeof(struct ceph_entity_addr)) != 0)
1881 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001882 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001883}
1884
1885/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001886 * Requeue requests whose mapping to an OSD has changed. If requests map to
1887 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001888 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001889 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001890 */
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001891static void kick_requests(struct ceph_osd_client *osdc, bool force_resend,
1892 bool force_resend_writes)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001893{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001894 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001895 struct rb_node *p;
1896 int needmap = 0;
1897 int err;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001898 bool force_resend_req;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001899
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001900 dout("kick_requests %s %s\n", force_resend ? " (force resend)" : "",
1901 force_resend_writes ? " (force resend writes)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001902 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001903 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001904 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001905 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001906
1907 /*
1908 * For linger requests that have not yet been
1909 * registered, move them to the linger list; they'll
1910 * be sent to the osd in the loop below. Unregister
1911 * the request before re-registering it as a linger
1912 * request to ensure the __map_request() below
1913 * will decide it needs to be sent.
1914 */
1915 if (req->r_linger && list_empty(&req->r_linger_item)) {
1916 dout("%p tid %llu restart on osd%d\n",
1917 req, req->r_tid,
1918 req->r_osd ? req->r_osd->o_osd : -1);
Alex Elder96e4dac2013-05-22 20:54:25 -05001919 ceph_osdc_get_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001920 __unregister_request(osdc, req);
1921 __register_linger_request(osdc, req);
Alex Elder96e4dac2013-05-22 20:54:25 -05001922 ceph_osdc_put_request(req);
Alex Elderab60b162012-12-19 15:52:36 -06001923 continue;
1924 }
1925
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001926 force_resend_req = force_resend ||
1927 (force_resend_writes &&
1928 req->r_flags & CEPH_OSD_FLAG_WRITE);
1929 err = __map_request(osdc, req, force_resend_req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001930 if (err < 0)
1931 continue; /* error */
1932 if (req->r_osd == NULL) {
1933 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1934 needmap++; /* request a newer map */
1935 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001936 if (!req->r_linger) {
1937 dout("%p tid %llu requeued on osd%d\n", req,
1938 req->r_tid,
1939 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001940 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001941 }
1942 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001943 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001944
1945 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1946 r_linger_item) {
1947 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1948
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001949 err = __map_request(osdc, req,
1950 force_resend || force_resend_writes);
Alex Elderab60b162012-12-19 15:52:36 -06001951 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001952 if (err == 0)
1953 continue; /* no change and no osd was specified */
1954 if (err < 0)
1955 continue; /* hrm! */
1956 if (req->r_osd == NULL) {
1957 dout("tid %llu maps to no valid osd\n", req->r_tid);
1958 needmap++; /* request a newer map */
1959 continue;
1960 }
1961
1962 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1963 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001964 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001965 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001966 }
Alex Elder14d2f382013-05-15 16:28:33 -05001967 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001968 mutex_unlock(&osdc->request_mutex);
1969
1970 if (needmap) {
1971 dout("%d requests for down osds, need new map\n", needmap);
1972 ceph_monc_request_next_osdmap(&osdc->client->monc);
1973 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001974}
Sage Weil6f6c7002011-01-17 20:34:08 -08001975
1976
Sage Weilf24e9982009-10-06 11:31:10 -07001977/*
1978 * Process updated osd map.
1979 *
1980 * The message contains any number of incremental and full maps, normally
1981 * indicating some sort of topology change in the cluster. Kick requests
1982 * off to different OSDs as needed.
1983 */
1984void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1985{
1986 void *p, *end, *next;
1987 u32 nr_maps, maplen;
1988 u32 epoch;
1989 struct ceph_osdmap *newmap = NULL, *oldmap;
1990 int err;
1991 struct ceph_fsid fsid;
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08001992 bool was_full;
Sage Weilf24e9982009-10-06 11:31:10 -07001993
1994 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1995 p = msg->front.iov_base;
1996 end = p + msg->front.iov_len;
1997
1998 /* verify fsid */
1999 ceph_decode_need(&p, end, sizeof(fsid), bad);
2000 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08002001 if (ceph_check_fsid(osdc->client, &fsid) < 0)
2002 return;
Sage Weilf24e9982009-10-06 11:31:10 -07002003
2004 down_write(&osdc->map_sem);
2005
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002006 was_full = ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL);
2007
Sage Weilf24e9982009-10-06 11:31:10 -07002008 /* incremental maps */
2009 ceph_decode_32_safe(&p, end, nr_maps, bad);
2010 dout(" %d inc maps\n", nr_maps);
2011 while (nr_maps > 0) {
2012 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002013 epoch = ceph_decode_32(&p);
2014 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002015 ceph_decode_need(&p, end, maplen, bad);
2016 next = p + maplen;
2017 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
2018 dout("applying incremental map %u len %d\n",
2019 epoch, maplen);
2020 newmap = osdmap_apply_incremental(&p, next,
2021 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05002022 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07002023 if (IS_ERR(newmap)) {
2024 err = PTR_ERR(newmap);
2025 goto bad;
2026 }
Sage Weil30dc6382009-12-21 14:49:37 -08002027 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002028 if (newmap != osdc->osdmap) {
2029 ceph_osdmap_destroy(osdc->osdmap);
2030 osdc->osdmap = newmap;
2031 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002032 was_full = was_full ||
2033 ceph_osdmap_flag(osdc->osdmap,
2034 CEPH_OSDMAP_FULL);
2035 kick_requests(osdc, 0, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002036 } else {
2037 dout("ignoring incremental map %u len %d\n",
2038 epoch, maplen);
2039 }
2040 p = next;
2041 nr_maps--;
2042 }
2043 if (newmap)
2044 goto done;
2045
2046 /* full maps */
2047 ceph_decode_32_safe(&p, end, nr_maps, bad);
2048 dout(" %d full maps\n", nr_maps);
2049 while (nr_maps) {
2050 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07002051 epoch = ceph_decode_32(&p);
2052 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07002053 ceph_decode_need(&p, end, maplen, bad);
2054 if (nr_maps > 1) {
2055 dout("skipping non-latest full map %u len %d\n",
2056 epoch, maplen);
2057 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
2058 dout("skipping full map %u len %d, "
2059 "older than our %u\n", epoch, maplen,
2060 osdc->osdmap->epoch);
2061 } else {
Sage Weil38d64532011-10-14 13:33:55 -07002062 int skipped_map = 0;
2063
Sage Weilf24e9982009-10-06 11:31:10 -07002064 dout("taking full map %u len %d\n", epoch, maplen);
2065 newmap = osdmap_decode(&p, p+maplen);
2066 if (IS_ERR(newmap)) {
2067 err = PTR_ERR(newmap);
2068 goto bad;
2069 }
Sage Weil30dc6382009-12-21 14:49:37 -08002070 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07002071 oldmap = osdc->osdmap;
2072 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07002073 if (oldmap) {
2074 if (oldmap->epoch + 1 < newmap->epoch)
2075 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07002076 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07002077 }
Josh Durgin9a1ea2d2013-12-10 09:35:13 -08002078 was_full = was_full ||
2079 ceph_osdmap_flag(osdc->osdmap,
2080 CEPH_OSDMAP_FULL);
2081 kick_requests(osdc, skipped_map, was_full);
Sage Weilf24e9982009-10-06 11:31:10 -07002082 }
2083 p += maplen;
2084 nr_maps--;
2085 }
2086
Dan Carpenterb72e19b2013-08-15 08:52:48 +03002087 if (!osdc->osdmap)
2088 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07002089done:
2090 downgrade_write(&osdc->map_sem);
2091 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07002092
2093 /*
2094 * subscribe to subsequent osdmap updates if full to ensure
2095 * we find out when we are no longer full and stop returning
2096 * ENOSPC.
2097 */
Josh Durgind29adb32013-12-02 19:11:48 -08002098 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL) ||
2099 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSERD) ||
2100 ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_PAUSEWR))
Sage Weilcd634fb2011-05-12 09:29:18 -07002101 ceph_monc_request_next_osdmap(&osdc->client->monc);
2102
Alex Elderf9d25192013-02-15 11:42:29 -06002103 mutex_lock(&osdc->request_mutex);
2104 __send_queued(osdc);
2105 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002106 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07002107 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002108 return;
2109
2110bad:
2111 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08002112 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07002113 up_write(&osdc->map_sem);
2114 return;
2115}
2116
Sage Weilf24e9982009-10-06 11:31:10 -07002117/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002118 * watch/notify callback event infrastructure
2119 *
2120 * These callbacks are used both for watch and notify operations.
2121 */
2122static void __release_event(struct kref *kref)
2123{
2124 struct ceph_osd_event *event =
2125 container_of(kref, struct ceph_osd_event, kref);
2126
2127 dout("__release_event %p\n", event);
2128 kfree(event);
2129}
2130
2131static void get_event(struct ceph_osd_event *event)
2132{
2133 kref_get(&event->kref);
2134}
2135
2136void ceph_osdc_put_event(struct ceph_osd_event *event)
2137{
2138 kref_put(&event->kref, __release_event);
2139}
2140EXPORT_SYMBOL(ceph_osdc_put_event);
2141
2142static void __insert_event(struct ceph_osd_client *osdc,
2143 struct ceph_osd_event *new)
2144{
2145 struct rb_node **p = &osdc->event_tree.rb_node;
2146 struct rb_node *parent = NULL;
2147 struct ceph_osd_event *event = NULL;
2148
2149 while (*p) {
2150 parent = *p;
2151 event = rb_entry(parent, struct ceph_osd_event, node);
2152 if (new->cookie < event->cookie)
2153 p = &(*p)->rb_left;
2154 else if (new->cookie > event->cookie)
2155 p = &(*p)->rb_right;
2156 else
2157 BUG();
2158 }
2159
2160 rb_link_node(&new->node, parent, p);
2161 rb_insert_color(&new->node, &osdc->event_tree);
2162}
2163
2164static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
2165 u64 cookie)
2166{
2167 struct rb_node **p = &osdc->event_tree.rb_node;
2168 struct rb_node *parent = NULL;
2169 struct ceph_osd_event *event = NULL;
2170
2171 while (*p) {
2172 parent = *p;
2173 event = rb_entry(parent, struct ceph_osd_event, node);
2174 if (cookie < event->cookie)
2175 p = &(*p)->rb_left;
2176 else if (cookie > event->cookie)
2177 p = &(*p)->rb_right;
2178 else
2179 return event;
2180 }
2181 return NULL;
2182}
2183
2184static void __remove_event(struct ceph_osd_event *event)
2185{
2186 struct ceph_osd_client *osdc = event->osdc;
2187
2188 if (!RB_EMPTY_NODE(&event->node)) {
2189 dout("__remove_event removed %p\n", event);
2190 rb_erase(&event->node, &osdc->event_tree);
2191 ceph_osdc_put_event(event);
2192 } else {
2193 dout("__remove_event didn't remove %p\n", event);
2194 }
2195}
2196
2197int ceph_osdc_create_event(struct ceph_osd_client *osdc,
2198 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06002199 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002200{
2201 struct ceph_osd_event *event;
2202
2203 event = kmalloc(sizeof(*event), GFP_NOIO);
2204 if (!event)
2205 return -ENOMEM;
2206
2207 dout("create_event %p\n", event);
2208 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06002209 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002210 event->data = data;
2211 event->osdc = osdc;
2212 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06002213 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002214 kref_init(&event->kref); /* one ref for us */
2215 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002216
2217 spin_lock(&osdc->event_lock);
2218 event->cookie = ++osdc->event_count;
2219 __insert_event(osdc, event);
2220 spin_unlock(&osdc->event_lock);
2221
2222 *pevent = event;
2223 return 0;
2224}
2225EXPORT_SYMBOL(ceph_osdc_create_event);
2226
2227void ceph_osdc_cancel_event(struct ceph_osd_event *event)
2228{
2229 struct ceph_osd_client *osdc = event->osdc;
2230
2231 dout("cancel_event %p\n", event);
2232 spin_lock(&osdc->event_lock);
2233 __remove_event(event);
2234 spin_unlock(&osdc->event_lock);
2235 ceph_osdc_put_event(event); /* caller's */
2236}
2237EXPORT_SYMBOL(ceph_osdc_cancel_event);
2238
2239
2240static void do_event_work(struct work_struct *work)
2241{
2242 struct ceph_osd_event_work *event_work =
2243 container_of(work, struct ceph_osd_event_work, work);
2244 struct ceph_osd_event *event = event_work->event;
2245 u64 ver = event_work->ver;
2246 u64 notify_id = event_work->notify_id;
2247 u8 opcode = event_work->opcode;
2248
2249 dout("do_event_work completing %p\n", event);
2250 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002251 dout("do_event_work completed %p\n", event);
2252 ceph_osdc_put_event(event);
2253 kfree(event_work);
2254}
2255
2256
2257/*
2258 * Process osd watch notifications
2259 */
Alex Elder3c663bb2013-02-15 11:42:30 -06002260static void handle_watch_notify(struct ceph_osd_client *osdc,
2261 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002262{
2263 void *p, *end;
2264 u8 proto_ver;
2265 u64 cookie, ver, notify_id;
2266 u8 opcode;
2267 struct ceph_osd_event *event;
2268 struct ceph_osd_event_work *event_work;
2269
2270 p = msg->front.iov_base;
2271 end = p + msg->front.iov_len;
2272
2273 ceph_decode_8_safe(&p, end, proto_ver, bad);
2274 ceph_decode_8_safe(&p, end, opcode, bad);
2275 ceph_decode_64_safe(&p, end, cookie, bad);
2276 ceph_decode_64_safe(&p, end, ver, bad);
2277 ceph_decode_64_safe(&p, end, notify_id, bad);
2278
2279 spin_lock(&osdc->event_lock);
2280 event = __find_event(osdc, cookie);
2281 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06002282 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002283 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002284 }
2285 spin_unlock(&osdc->event_lock);
2286 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
2287 cookie, ver, event);
2288 if (event) {
2289 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002290 if (!event_work) {
2291 dout("ERROR: could not allocate event_work\n");
2292 goto done_err;
2293 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01002294 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002295 event_work->event = event;
2296 event_work->ver = ver;
2297 event_work->notify_id = notify_id;
2298 event_work->opcode = opcode;
2299 if (!queue_work(osdc->notify_wq, &event_work->work)) {
2300 dout("WARNING: failed to queue notify event work\n");
2301 goto done_err;
2302 }
2303 }
2304
2305 return;
2306
2307done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002308 ceph_osdc_put_event(event);
2309 return;
2310
2311bad:
2312 pr_err("osdc handle_watch_notify corrupt msg\n");
2313 return;
2314}
2315
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002316/*
Alex Eldere65550f2013-04-05 01:27:12 -05002317 * build new request AND message
2318 *
2319 */
2320void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
2321 struct ceph_snap_context *snapc, u64 snap_id,
2322 struct timespec *mtime)
2323{
2324 struct ceph_msg *msg = req->r_request;
2325 void *p;
2326 size_t msg_size;
2327 int flags = req->r_flags;
2328 u64 data_len;
2329 unsigned int i;
2330
2331 req->r_snapid = snap_id;
2332 req->r_snapc = ceph_get_snap_context(snapc);
2333
2334 /* encode request */
2335 msg->hdr.version = cpu_to_le16(4);
2336
2337 p = msg->front.iov_base;
2338 ceph_encode_32(&p, 1); /* client_inc is always 1 */
2339 req->r_request_osdmap_epoch = p;
2340 p += 4;
2341 req->r_request_flags = p;
2342 p += 4;
2343 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
2344 ceph_encode_timespec(p, mtime);
2345 p += sizeof(struct ceph_timespec);
2346 req->r_request_reassert_version = p;
2347 p += sizeof(struct ceph_eversion); /* will get filled in */
2348
2349 /* oloc */
2350 ceph_encode_8(&p, 4);
2351 ceph_encode_8(&p, 4);
2352 ceph_encode_32(&p, 8 + 4 + 4);
2353 req->r_request_pool = p;
2354 p += 8;
2355 ceph_encode_32(&p, -1); /* preferred */
2356 ceph_encode_32(&p, 0); /* key len */
2357
2358 ceph_encode_8(&p, 1);
2359 req->r_request_pgid = p;
2360 p += 8 + 4;
2361 ceph_encode_32(&p, -1); /* preferred */
2362
2363 /* oid */
Ilya Dryomov3c972c92014-01-27 17:40:20 +02002364 ceph_encode_32(&p, req->r_base_oid.name_len);
2365 memcpy(p, req->r_base_oid.name, req->r_base_oid.name_len);
2366 dout("oid '%.*s' len %d\n", req->r_base_oid.name_len,
2367 req->r_base_oid.name, req->r_base_oid.name_len);
2368 p += req->r_base_oid.name_len;
Alex Eldere65550f2013-04-05 01:27:12 -05002369
2370 /* ops--can imply data */
2371 ceph_encode_16(&p, (u16)req->r_num_ops);
2372 data_len = 0;
2373 for (i = 0; i < req->r_num_ops; i++) {
2374 data_len += osd_req_encode_op(req, p, i);
2375 p += sizeof(struct ceph_osd_op);
2376 }
2377
2378 /* snaps */
2379 ceph_encode_64(&p, req->r_snapid);
2380 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
2381 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
2382 if (req->r_snapc) {
2383 for (i = 0; i < snapc->num_snaps; i++) {
2384 ceph_encode_64(&p, req->r_snapc->snaps[i]);
2385 }
2386 }
2387
2388 req->r_request_attempts = p;
2389 p += 4;
2390
2391 /* data */
2392 if (flags & CEPH_OSD_FLAG_WRITE) {
2393 u16 data_off;
2394
2395 /*
2396 * The header "data_off" is a hint to the receiver
2397 * allowing it to align received data into its
2398 * buffers such that there's no need to re-copy
2399 * it before writing it to disk (direct I/O).
2400 */
2401 data_off = (u16) (off & 0xffff);
2402 req->r_request->hdr.data_off = cpu_to_le16(data_off);
2403 }
2404 req->r_request->hdr.data_len = cpu_to_le32(data_len);
2405
2406 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
2407 msg_size = p - msg->front.iov_base;
2408 msg->front.iov_len = msg_size;
2409 msg->hdr.front_len = cpu_to_le32(msg_size);
2410
2411 dout("build_request msg_size was %d\n", (int)msg_size);
2412}
2413EXPORT_SYMBOL(ceph_osdc_build_request);
2414
2415/*
Sage Weilf24e9982009-10-06 11:31:10 -07002416 * Register request, send initial attempt.
2417 */
2418int ceph_osdc_start_request(struct ceph_osd_client *osdc,
2419 struct ceph_osd_request *req,
2420 bool nofail)
2421{
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002422 int rc;
Sage Weilf24e9982009-10-06 11:31:10 -07002423
Sage Weilf24e9982009-10-06 11:31:10 -07002424 down_read(&osdc->map_sem);
2425 mutex_lock(&osdc->request_mutex);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002426
2427 rc = __ceph_osdc_start_request(osdc, req, nofail);
2428
Sage Weilf24e9982009-10-06 11:31:10 -07002429 mutex_unlock(&osdc->request_mutex);
2430 up_read(&osdc->map_sem);
Ilya Dryomov0bbfdfe2014-01-31 19:33:39 +02002431
Sage Weilf24e9982009-10-06 11:31:10 -07002432 return rc;
2433}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002434EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002435
2436/*
2437 * wait for a request to complete
2438 */
2439int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2440 struct ceph_osd_request *req)
2441{
2442 int rc;
2443
2444 rc = wait_for_completion_interruptible(&req->r_completion);
2445 if (rc < 0) {
2446 mutex_lock(&osdc->request_mutex);
2447 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002448 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07002449 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07002450 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002451 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07002452 return rc;
2453 }
2454
2455 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2456 return req->r_result;
2457}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002458EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002459
2460/*
2461 * sync - wait for all in-flight requests to flush. avoid starvation.
2462 */
2463void ceph_osdc_sync(struct ceph_osd_client *osdc)
2464{
2465 struct ceph_osd_request *req;
2466 u64 last_tid, next_tid = 0;
2467
2468 mutex_lock(&osdc->request_mutex);
2469 last_tid = osdc->last_tid;
2470 while (1) {
2471 req = __lookup_request_ge(osdc, next_tid);
2472 if (!req)
2473 break;
2474 if (req->r_tid > last_tid)
2475 break;
2476
2477 next_tid = req->r_tid + 1;
2478 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2479 continue;
2480
2481 ceph_osdc_get_request(req);
2482 mutex_unlock(&osdc->request_mutex);
2483 dout("sync waiting on tid %llu (last is %llu)\n",
2484 req->r_tid, last_tid);
2485 wait_for_completion(&req->r_safe_completion);
2486 mutex_lock(&osdc->request_mutex);
2487 ceph_osdc_put_request(req);
2488 }
2489 mutex_unlock(&osdc->request_mutex);
2490 dout("sync done (thru tid %llu)\n", last_tid);
2491}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002492EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002493
2494/*
Josh Durgindd935f42013-08-28 21:43:09 -07002495 * Call all pending notify callbacks - for use after a watch is
2496 * unregistered, to make sure no more callbacks for it will be invoked
2497 */
2498extern void ceph_osdc_flush_notifies(struct ceph_osd_client *osdc)
2499{
2500 flush_workqueue(osdc->notify_wq);
2501}
2502EXPORT_SYMBOL(ceph_osdc_flush_notifies);
2503
2504
2505/*
Sage Weilf24e9982009-10-06 11:31:10 -07002506 * init, shutdown
2507 */
2508int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2509{
2510 int err;
2511
2512 dout("init\n");
2513 osdc->client = client;
2514 osdc->osdmap = NULL;
2515 init_rwsem(&osdc->map_sem);
2516 init_completion(&osdc->map_waiters);
2517 osdc->last_requested_map = 0;
2518 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002519 osdc->last_tid = 0;
2520 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002521 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002522 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002523 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002524 INIT_LIST_HEAD(&osdc->req_unsent);
2525 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002526 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002527 osdc->num_requests = 0;
2528 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002529 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002530 spin_lock_init(&osdc->event_lock);
2531 osdc->event_tree = RB_ROOT;
2532 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002533
2534 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002535 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002536
Sage Weil5f44f142009-11-18 14:52:18 -08002537 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002538 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2539 sizeof(struct ceph_osd_request));
2540 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002541 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002542
Sage Weild50b4092012-07-09 14:22:34 -07002543 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2544 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002545 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002546 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002547 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002548 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002549 OSD_OPREPLY_FRONT_LEN, 10, true,
2550 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002551 if (err < 0)
2552 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002553
Dan Carpenterdbcae082013-08-15 08:58:59 +03002554 err = -ENOMEM;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002555 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
Dan Carpenterdbcae082013-08-15 08:58:59 +03002556 if (!osdc->notify_wq)
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002557 goto out_msgpool_reply;
2558
Sage Weilf24e9982009-10-06 11:31:10 -07002559 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002560
Ilya Dryomovc172ec52014-01-31 17:49:22 +02002561out_msgpool_reply:
2562 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilc16e7862010-03-01 13:02:00 -08002563out_msgpool:
2564 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002565out_mempool:
2566 mempool_destroy(osdc->req_mempool);
2567out:
2568 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002569}
2570
2571void ceph_osdc_stop(struct ceph_osd_client *osdc)
2572{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002573 flush_workqueue(osdc->notify_wq);
2574 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002575 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002576 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002577 if (osdc->osdmap) {
2578 ceph_osdmap_destroy(osdc->osdmap);
2579 osdc->osdmap = NULL;
2580 }
Sage Weilaca420b2011-08-31 14:45:53 -07002581 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002582 mempool_destroy(osdc->req_mempool);
2583 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002584 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002585}
2586
2587/*
2588 * Read some contiguous pages. If we cross a stripe boundary, shorten
2589 * *plen. Return number of bytes read, or error.
2590 */
2591int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2592 struct ceph_vino vino, struct ceph_file_layout *layout,
2593 u64 off, u64 *plen,
2594 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002595 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002596{
2597 struct ceph_osd_request *req;
2598 int rc = 0;
2599
2600 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2601 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002602 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002603 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002604 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002605 false);
Sage Weil68162822012-09-24 21:01:02 -07002606 if (IS_ERR(req))
2607 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002608
2609 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002610
Alex Elder406e2c92013-04-15 14:50:36 -05002611 osd_req_op_extent_osd_data_pages(req, 0,
Alex Eldera4ce40a2013-04-05 01:27:12 -05002612 pages, *plen, page_align, false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002613
Alex Eldere0c59482013-03-07 15:38:25 -06002614 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002615 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002616
Alex Elder79528732013-04-03 21:32:51 -05002617 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002618
Sage Weilf24e9982009-10-06 11:31:10 -07002619 rc = ceph_osdc_start_request(osdc, req, false);
2620 if (!rc)
2621 rc = ceph_osdc_wait_request(osdc, req);
2622
2623 ceph_osdc_put_request(req);
2624 dout("readpages result %d\n", rc);
2625 return rc;
2626}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002627EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002628
2629/*
2630 * do a synchronous write on N pages
2631 */
2632int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2633 struct ceph_file_layout *layout,
2634 struct ceph_snap_context *snapc,
2635 u64 off, u64 len,
2636 u32 truncate_seq, u64 truncate_size,
2637 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002638 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002639{
2640 struct ceph_osd_request *req;
2641 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002642 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002643
Alex Elderacead002013-03-14 14:09:05 -05002644 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002645 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002646 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002647 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002648 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002649 true);
Sage Weil68162822012-09-24 21:01:02 -07002650 if (IS_ERR(req))
2651 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002652
2653 /* it may be a short write due to an object boundary */
Alex Elder406e2c92013-04-15 14:50:36 -05002654 osd_req_op_extent_osd_data_pages(req, 0, pages, len, page_align,
Alex Elder43bfe5d2013-04-03 01:28:57 -05002655 false, false);
2656 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002657
Alex Elder79528732013-04-03 21:32:51 -05002658 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002659
Alex Elder87f979d2013-02-15 11:42:29 -06002660 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002661 if (!rc)
2662 rc = ceph_osdc_wait_request(osdc, req);
2663
2664 ceph_osdc_put_request(req);
2665 if (rc == 0)
2666 rc = len;
2667 dout("writepages result %d\n", rc);
2668 return rc;
2669}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002670EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002671
Alex Elder5522ae02013-05-01 12:43:04 -05002672int ceph_osdc_setup(void)
2673{
2674 BUG_ON(ceph_osd_request_cache);
2675 ceph_osd_request_cache = kmem_cache_create("ceph_osd_request",
2676 sizeof (struct ceph_osd_request),
2677 __alignof__(struct ceph_osd_request),
2678 0, NULL);
2679
2680 return ceph_osd_request_cache ? 0 : -ENOMEM;
2681}
2682EXPORT_SYMBOL(ceph_osdc_setup);
2683
2684void ceph_osdc_cleanup(void)
2685{
2686 BUG_ON(!ceph_osd_request_cache);
2687 kmem_cache_destroy(ceph_osd_request_cache);
2688 ceph_osd_request_cache = NULL;
2689}
2690EXPORT_SYMBOL(ceph_osdc_cleanup);
2691
Sage Weilf24e9982009-10-06 11:31:10 -07002692/*
2693 * handle incoming message
2694 */
2695static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2696{
2697 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002698 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002699 int type = le16_to_cpu(msg->hdr.type);
2700
2701 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002702 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002703 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002704
2705 switch (type) {
2706 case CEPH_MSG_OSD_MAP:
2707 ceph_osdc_handle_map(osdc, msg);
2708 break;
2709 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002710 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002711 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002712 case CEPH_MSG_WATCH_NOTIFY:
2713 handle_watch_notify(osdc, msg);
2714 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002715
2716 default:
2717 pr_err("received unknown message type %d %s\n", type,
2718 ceph_msg_type_name(type));
2719 }
Sage Weil4a32f932010-06-13 10:27:53 -07002720out:
Sage Weilf24e9982009-10-06 11:31:10 -07002721 ceph_msg_put(msg);
2722}
2723
Sage Weil5b3a4db2010-02-19 21:43:23 -08002724/*
Sage Weil21b667f2010-03-04 10:22:59 -08002725 * lookup and return message for incoming reply. set up reply message
2726 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002727 */
2728static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002729 struct ceph_msg_header *hdr,
2730 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002731{
2732 struct ceph_osd *osd = con->private;
2733 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002734 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002735 struct ceph_osd_request *req;
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002736 int front_len = le32_to_cpu(hdr->front_len);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002737 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002738 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002739
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002740 tid = le64_to_cpu(hdr->tid);
2741 mutex_lock(&osdc->request_mutex);
2742 req = __lookup_request(osdc, tid);
2743 if (!req) {
2744 *skip = 1;
2745 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002746 dout("get_reply unknown tid %llu from osd%d\n", tid,
2747 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002748 goto out;
2749 }
Sage Weilc16e7862010-03-01 13:02:00 -08002750
Alex Elderace6d3a2013-04-01 16:12:14 -05002751 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002752 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002753 req->r_reply, req->r_reply->con);
2754 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002755
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002756 if (front_len > req->r_reply->front_alloc_len) {
Alex Elder49743412013-05-29 11:19:00 -05002757 pr_warning("get_reply front %d > preallocated %d (%u#%llu)\n",
Ilya Dryomovf2be82b2014-01-09 20:08:21 +02002758 front_len, req->r_reply->front_alloc_len,
Alex Elder49743412013-05-29 11:19:00 -05002759 (unsigned int)con->peer_name.type,
2760 le64_to_cpu(con->peer_name.num));
Ilya Dryomov3f0a4ac2014-01-09 20:08:21 +02002761 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front_len, GFP_NOFS,
2762 false);
Sage Weila79832f2010-04-01 16:06:19 -07002763 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002764 goto out;
2765 ceph_msg_put(req->r_reply);
2766 req->r_reply = m;
2767 }
2768 m = ceph_msg_get(req->r_reply);
2769
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002770 if (data_len > 0) {
Alex Eldera4ce40a2013-04-05 01:27:12 -05002771 struct ceph_osd_data *osd_data;
Alex Elder0fff87e2013-02-14 12:16:43 -06002772
Alex Eldera4ce40a2013-04-05 01:27:12 -05002773 /*
2774 * XXX This is assuming there is only one op containing
2775 * XXX page data. Probably OK for reads, but this
2776 * XXX ought to be done more generally.
2777 */
Alex Elder406e2c92013-04-15 14:50:36 -05002778 osd_data = osd_req_op_extent_osd_data(req, 0);
Alex Elder0fff87e2013-02-14 12:16:43 -06002779 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002780 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002781 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002782
Alex Eldere0c59482013-03-07 15:38:25 -06002783 pr_warning("tid %lld reply has %d bytes "
2784 "we had only %llu bytes ready\n",
2785 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002786 *skip = 1;
2787 ceph_msg_put(m);
2788 m = NULL;
2789 goto out;
2790 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002791 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002792 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002793 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002794 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002795
2796out:
2797 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002798 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002799
2800}
2801
2802static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2803 struct ceph_msg_header *hdr,
2804 int *skip)
2805{
2806 struct ceph_osd *osd = con->private;
2807 int type = le16_to_cpu(hdr->type);
2808 int front = le32_to_cpu(hdr->front_len);
2809
Alex Elder1c20f2d2012-06-04 14:43:32 -05002810 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002811 switch (type) {
2812 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002813 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002814 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002815 case CEPH_MSG_OSD_OPREPLY:
2816 return get_reply(con, hdr, skip);
2817 default:
2818 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2819 osd->o_osd);
2820 *skip = 1;
2821 return NULL;
2822 }
Sage Weilf24e9982009-10-06 11:31:10 -07002823}
2824
2825/*
2826 * Wrappers to refcount containing ceph_osd struct
2827 */
2828static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2829{
2830 struct ceph_osd *osd = con->private;
2831 if (get_osd(osd))
2832 return con;
2833 return NULL;
2834}
2835
2836static void put_osd_con(struct ceph_connection *con)
2837{
2838 struct ceph_osd *osd = con->private;
2839 put_osd(osd);
2840}
2841
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002842/*
2843 * authentication
2844 */
Alex Eldera3530df2012-05-16 15:16:39 -05002845/*
2846 * Note: returned pointer is the address of a structure that's
2847 * managed separately. Caller must *not* attempt to free it.
2848 */
2849static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002850 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002851{
2852 struct ceph_osd *o = con->private;
2853 struct ceph_osd_client *osdc = o->o_osdc;
2854 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002855 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002856
Alex Elder74f18692012-05-16 15:16:39 -05002857 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002858 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002859 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002860 }
Sage Weil27859f92013-03-25 10:26:14 -07002861 if (!auth->authorizer) {
2862 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2863 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002864 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002865 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002866 } else {
2867 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002868 auth);
2869 if (ret)
2870 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002871 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002872 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002873
Alex Eldera3530df2012-05-16 15:16:39 -05002874 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002875}
2876
2877
2878static int verify_authorizer_reply(struct ceph_connection *con, int len)
2879{
2880 struct ceph_osd *o = con->private;
2881 struct ceph_osd_client *osdc = o->o_osdc;
2882 struct ceph_auth_client *ac = osdc->client->monc.auth;
2883
Sage Weil27859f92013-03-25 10:26:14 -07002884 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002885}
2886
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002887static int invalidate_authorizer(struct ceph_connection *con)
2888{
2889 struct ceph_osd *o = con->private;
2890 struct ceph_osd_client *osdc = o->o_osdc;
2891 struct ceph_auth_client *ac = osdc->client->monc.auth;
2892
Sage Weil27859f92013-03-25 10:26:14 -07002893 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002894 return ceph_monc_validate_auth(&osdc->client->monc);
2895}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002896
Tobias Klauser9e327892010-05-20 10:40:19 +02002897static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002898 .get = get_osd_con,
2899 .put = put_osd_con,
2900 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002901 .get_authorizer = get_authorizer,
2902 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002903 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002904 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002905 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002906};