blob: ad24f210bf0c876974f63829a859d67df71636f8 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07004#include <linux/err.h>
5#include <linux/highmem.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/slab.h>
9#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070010#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
Sage Weilf24e9982009-10-06 11:31:10 -070013
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include <linux/ceph/libceph.h>
15#include <linux/ceph/osd_client.h>
16#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/auth.h>
19#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070020
Sage Weilc16e7862010-03-01 13:02:00 -080021#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Tobias Klauser9e327892010-05-20 10:40:19 +020024static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070025
Alex Elderf9d25192013-02-15 11:42:29 -060026static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080027static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070028static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080032static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070034
35/*
36 * Implement client access to distributed object storage cluster.
37 *
38 * All data objects are stored within a cluster/cloud of OSDs, or
39 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
40 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
41 * remote daemons serving up and coordinating consistent and safe
42 * access to storage.
43 *
44 * Cluster membership and the mapping of data objects onto storage devices
45 * are described by the osd map.
46 *
47 * We keep track of pending OSD requests (read, write), resubmit
48 * requests to different OSDs when the cluster topology/data layout
49 * change, or retry the affected requests when the communications
50 * channel with an OSD is reset.
51 */
52
53/*
54 * calculate the mapping of a file extent onto an object, and fill out the
55 * request accordingly. shorten extent as necessary if it crosses an
56 * object boundary.
57 *
58 * fill osd op in request message.
59 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060060static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050061 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070062{
Alex Elder60e56f12013-02-15 11:42:29 -060063 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070064 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070065
Alex Elder60e56f12013-02-15 11:42:29 -060066 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050067 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
68 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070069 if (r < 0)
70 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050071 if (*objlen < orig_len) {
72 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060073 dout(" skipping last %llu, final file extent %llu~%llu\n",
74 orig_len - *plen, off, *plen);
75 }
76
Alex Elder75d1c942013-03-13 20:50:00 -050077 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070078
Alex Elder3ff5f382013-02-15 22:10:17 -060079 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070080}
81
Alex Elderc54d47b2013-04-03 01:28:57 -050082static void ceph_osd_data_init(struct ceph_osd_data *osd_data)
83{
84 memset(osd_data, 0, sizeof (*osd_data));
85 osd_data->type = CEPH_OSD_DATA_TYPE_NONE;
86}
87
Alex Elder43bfe5d2013-04-03 01:28:57 -050088void ceph_osd_data_pages_init(struct ceph_osd_data *osd_data,
89 struct page **pages, u64 length, u32 alignment,
90 bool pages_from_pool, bool own_pages)
91{
92 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
93 osd_data->pages = pages;
94 osd_data->length = length;
95 osd_data->alignment = alignment;
96 osd_data->pages_from_pool = pages_from_pool;
97 osd_data->own_pages = own_pages;
98}
99EXPORT_SYMBOL(ceph_osd_data_pages_init);
100
101void ceph_osd_data_pagelist_init(struct ceph_osd_data *osd_data,
102 struct ceph_pagelist *pagelist)
103{
104 osd_data->type = CEPH_OSD_DATA_TYPE_PAGELIST;
105 osd_data->pagelist = pagelist;
106}
107EXPORT_SYMBOL(ceph_osd_data_pagelist_init);
108
109#ifdef CONFIG_BLOCK
110void ceph_osd_data_bio_init(struct ceph_osd_data *osd_data,
111 struct bio *bio, size_t bio_length)
112{
113 osd_data->type = CEPH_OSD_DATA_TYPE_BIO;
114 osd_data->bio = bio;
115 osd_data->bio_length = bio_length;
116}
117EXPORT_SYMBOL(ceph_osd_data_bio_init);
118#endif /* CONFIG_BLOCK */
119
Alex Elder23c08a9cb2013-04-03 01:28:58 -0500120static u64 ceph_osd_data_length(struct ceph_osd_data *osd_data)
121{
122 switch (osd_data->type) {
123 case CEPH_OSD_DATA_TYPE_NONE:
124 return 0;
125 case CEPH_OSD_DATA_TYPE_PAGES:
126 return osd_data->length;
127 case CEPH_OSD_DATA_TYPE_PAGELIST:
128 return (u64)osd_data->pagelist->length;
129#ifdef CONFIG_BLOCK
130 case CEPH_OSD_DATA_TYPE_BIO:
131 return (u64)osd_data->bio_length;
132#endif /* CONFIG_BLOCK */
133 default:
134 WARN(true, "unrecognized data type %d\n", (int)osd_data->type);
135 return 0;
136 }
137}
138
Alex Elderc54d47b2013-04-03 01:28:57 -0500139static void ceph_osd_data_release(struct ceph_osd_data *osd_data)
140{
141 if (osd_data->type != CEPH_OSD_DATA_TYPE_PAGES)
142 return;
143
144 if (osd_data->own_pages) {
145 int num_pages;
146
147 num_pages = calc_pages_for((u64)osd_data->alignment,
148 (u64)osd_data->length);
149 ceph_release_page_vector(osd_data->pages, num_pages);
150 }
151}
152
Sage Weilf24e9982009-10-06 11:31:10 -0700153/*
154 * requests
155 */
Sage Weil415e49a2009-12-07 13:37:03 -0800156void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700157{
Alex Elderc54d47b2013-04-03 01:28:57 -0500158 struct ceph_osd_request *req;
Sage Weil415e49a2009-12-07 13:37:03 -0800159
Alex Elderc54d47b2013-04-03 01:28:57 -0500160 req = container_of(kref, struct ceph_osd_request, r_kref);
Sage Weil415e49a2009-12-07 13:37:03 -0800161 if (req->r_request)
162 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -0500163 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -0500164 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -0500165 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -0500166 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600167
Alex Elderc54d47b2013-04-03 01:28:57 -0500168 ceph_osd_data_release(&req->r_data_in);
169 ceph_osd_data_release(&req->r_data_out);
Alex Elder0fff87e2013-02-14 12:16:43 -0600170
Sage Weil415e49a2009-12-07 13:37:03 -0800171 ceph_put_snap_context(req->r_snapc);
172 if (req->r_mempool)
173 mempool_free(req, req->r_osdc->req_mempool);
174 else
175 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700176}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700177EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700178
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700179struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700180 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800181 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700182 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600183 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700184{
185 struct ceph_osd_request *req;
186 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800187 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700188
Alex Elder79528732013-04-03 21:32:51 -0500189 BUILD_BUG_ON(CEPH_OSD_MAX_OP > U16_MAX);
190 BUG_ON(num_ops > CEPH_OSD_MAX_OP);
191
Sage Weil1b83bef2013-02-25 16:11:12 -0800192 msg_size = 4 + 4 + 8 + 8 + 4+8;
193 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
194 msg_size += 1 + 8 + 4 + 4; /* pg_t */
195 msg_size += 4 + MAX_OBJ_NAME_SIZE;
196 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
197 msg_size += 8; /* snapid */
198 msg_size += 8; /* snap_seq */
199 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
200 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700201
202 if (use_mempool) {
203 req = mempool_alloc(osdc->req_mempool, gfp_flags);
204 memset(req, 0, sizeof(*req));
205 } else {
206 req = kzalloc(sizeof(*req), gfp_flags);
207 }
208 if (req == NULL)
209 return NULL;
210
211 req->r_osdc = osdc;
212 req->r_mempool = use_mempool;
Alex Elder79528732013-04-03 21:32:51 -0500213 req->r_num_ops = num_ops;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700214
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700215 kref_init(&req->r_kref);
216 init_completion(&req->r_completion);
217 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600218 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700219 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700220 INIT_LIST_HEAD(&req->r_linger_item);
221 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700222 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700223 INIT_LIST_HEAD(&req->r_osd_item);
224
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700225 /* create reply message */
226 if (use_mempool)
227 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
228 else
229 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700230 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700231 if (!msg) {
232 ceph_osdc_put_request(req);
233 return NULL;
234 }
235 req->r_reply = msg;
236
Alex Elderc54d47b2013-04-03 01:28:57 -0500237 ceph_osd_data_init(&req->r_data_in);
238 ceph_osd_data_init(&req->r_data_out);
Sage Weild50b4092012-07-09 14:22:34 -0700239
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700240 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700241 if (use_mempool)
242 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
243 else
Sage Weilb61c2762011-08-09 15:03:46 -0700244 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700245 if (!msg) {
246 ceph_osdc_put_request(req);
247 return NULL;
248 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700249
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700250 memset(msg->front.iov_base, 0, msg->front.iov_len);
251
252 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700253
254 return req;
255}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700256EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700257
Alex Eldera8dd0a32013-03-13 20:50:00 -0500258static bool osd_req_opcode_valid(u16 opcode)
259{
260 switch (opcode) {
261 case CEPH_OSD_OP_READ:
262 case CEPH_OSD_OP_STAT:
263 case CEPH_OSD_OP_MAPEXT:
264 case CEPH_OSD_OP_MASKTRUNC:
265 case CEPH_OSD_OP_SPARSE_READ:
266 case CEPH_OSD_OP_NOTIFY:
267 case CEPH_OSD_OP_NOTIFY_ACK:
268 case CEPH_OSD_OP_ASSERT_VER:
269 case CEPH_OSD_OP_WRITE:
270 case CEPH_OSD_OP_WRITEFULL:
271 case CEPH_OSD_OP_TRUNCATE:
272 case CEPH_OSD_OP_ZERO:
273 case CEPH_OSD_OP_DELETE:
274 case CEPH_OSD_OP_APPEND:
275 case CEPH_OSD_OP_STARTSYNC:
276 case CEPH_OSD_OP_SETTRUNC:
277 case CEPH_OSD_OP_TRIMTRUNC:
278 case CEPH_OSD_OP_TMAPUP:
279 case CEPH_OSD_OP_TMAPPUT:
280 case CEPH_OSD_OP_TMAPGET:
281 case CEPH_OSD_OP_CREATE:
282 case CEPH_OSD_OP_ROLLBACK:
283 case CEPH_OSD_OP_WATCH:
284 case CEPH_OSD_OP_OMAPGETKEYS:
285 case CEPH_OSD_OP_OMAPGETVALS:
286 case CEPH_OSD_OP_OMAPGETHEADER:
287 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
288 case CEPH_OSD_OP_OMAPSETVALS:
289 case CEPH_OSD_OP_OMAPSETHEADER:
290 case CEPH_OSD_OP_OMAPCLEAR:
291 case CEPH_OSD_OP_OMAPRMKEYS:
292 case CEPH_OSD_OP_OMAP_CMP:
293 case CEPH_OSD_OP_CLONERANGE:
294 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
295 case CEPH_OSD_OP_SRC_CMPXATTR:
296 case CEPH_OSD_OP_GETXATTR:
297 case CEPH_OSD_OP_GETXATTRS:
298 case CEPH_OSD_OP_CMPXATTR:
299 case CEPH_OSD_OP_SETXATTR:
300 case CEPH_OSD_OP_SETXATTRS:
301 case CEPH_OSD_OP_RESETXATTRS:
302 case CEPH_OSD_OP_RMXATTR:
303 case CEPH_OSD_OP_PULL:
304 case CEPH_OSD_OP_PUSH:
305 case CEPH_OSD_OP_BALANCEREADS:
306 case CEPH_OSD_OP_UNBALANCEREADS:
307 case CEPH_OSD_OP_SCRUB:
308 case CEPH_OSD_OP_SCRUB_RESERVE:
309 case CEPH_OSD_OP_SCRUB_UNRESERVE:
310 case CEPH_OSD_OP_SCRUB_STOP:
311 case CEPH_OSD_OP_SCRUB_MAP:
312 case CEPH_OSD_OP_WRLOCK:
313 case CEPH_OSD_OP_WRUNLOCK:
314 case CEPH_OSD_OP_RDLOCK:
315 case CEPH_OSD_OP_RDUNLOCK:
316 case CEPH_OSD_OP_UPLOCK:
317 case CEPH_OSD_OP_DNLOCK:
318 case CEPH_OSD_OP_CALL:
319 case CEPH_OSD_OP_PGLS:
320 case CEPH_OSD_OP_PGLS_FILTER:
321 return true;
322 default:
323 return false;
324 }
325}
326
Alex Elder33803f32013-03-13 20:50:00 -0500327/*
328 * This is an osd op init function for opcodes that have no data or
329 * other information associated with them. It also serves as a
330 * common init routine for all the other init functions, below.
331 */
Alex Elderc99d2d42013-04-05 01:27:11 -0500332static struct ceph_osd_req_op *
333osd_req_op_init(struct ceph_osd_request *osd_req, unsigned int which,
334 u16 opcode)
Alex Elder33803f32013-03-13 20:50:00 -0500335{
Alex Elderc99d2d42013-04-05 01:27:11 -0500336 struct ceph_osd_req_op *op;
337
338 BUG_ON(which >= osd_req->r_num_ops);
Alex Elder33803f32013-03-13 20:50:00 -0500339 BUG_ON(!osd_req_opcode_valid(opcode));
340
Alex Elderc99d2d42013-04-05 01:27:11 -0500341 op = &osd_req->r_ops[which];
Alex Elder33803f32013-03-13 20:50:00 -0500342 memset(op, 0, sizeof (*op));
Alex Elder33803f32013-03-13 20:50:00 -0500343 op->op = opcode;
Alex Elderc99d2d42013-04-05 01:27:11 -0500344
345 return op;
Alex Elder33803f32013-03-13 20:50:00 -0500346}
347
Alex Elderc99d2d42013-04-05 01:27:11 -0500348void osd_req_op_extent_init(struct ceph_osd_request *osd_req,
349 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500350 u64 offset, u64 length,
351 u64 truncate_size, u32 truncate_seq)
352{
Alex Elderc99d2d42013-04-05 01:27:11 -0500353 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500354 size_t payload_len = 0;
355
356 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
357
Alex Elder33803f32013-03-13 20:50:00 -0500358 op->extent.offset = offset;
359 op->extent.length = length;
360 op->extent.truncate_size = truncate_size;
361 op->extent.truncate_seq = truncate_seq;
362 if (opcode == CEPH_OSD_OP_WRITE)
363 payload_len += length;
364
365 op->payload_len = payload_len;
366}
367EXPORT_SYMBOL(osd_req_op_extent_init);
368
Alex Elderc99d2d42013-04-05 01:27:11 -0500369void osd_req_op_extent_update(struct ceph_osd_request *osd_req,
370 unsigned int which, u64 length)
Alex Eldere5975c72013-03-14 14:09:05 -0500371{
Alex Elderc99d2d42013-04-05 01:27:11 -0500372 struct ceph_osd_req_op *op;
373 u64 previous;
374
375 BUG_ON(which >= osd_req->r_num_ops);
376 op = &osd_req->r_ops[which];
377 previous = op->extent.length;
Alex Eldere5975c72013-03-14 14:09:05 -0500378
379 if (length == previous)
380 return; /* Nothing to do */
381 BUG_ON(length > previous);
382
383 op->extent.length = length;
384 op->payload_len -= previous - length;
385}
386EXPORT_SYMBOL(osd_req_op_extent_update);
387
Alex Elderc99d2d42013-04-05 01:27:11 -0500388void osd_req_op_extent_osd_data(struct ceph_osd_request *osd_req,
389 unsigned int which,
Alex Elder8c042b02013-04-03 01:28:58 -0500390 struct ceph_osd_data *osd_data)
391{
Alex Elderc99d2d42013-04-05 01:27:11 -0500392 BUG_ON(which >= osd_req->r_num_ops);
393 osd_req->r_ops[which].extent.osd_data = osd_data;
Alex Elder8c042b02013-04-03 01:28:58 -0500394}
395EXPORT_SYMBOL(osd_req_op_extent_osd_data);
396
Alex Elderc99d2d42013-04-05 01:27:11 -0500397void osd_req_op_cls_init(struct ceph_osd_request *osd_req, unsigned int which,
398 u16 opcode, const char *class, const char *method,
Alex Elder33803f32013-03-13 20:50:00 -0500399 const void *request_data, size_t request_data_size)
400{
Alex Elderc99d2d42013-04-05 01:27:11 -0500401 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500402 size_t payload_len = 0;
403 size_t size;
404
405 BUG_ON(opcode != CEPH_OSD_OP_CALL);
406
Alex Elder33803f32013-03-13 20:50:00 -0500407 op->cls.class_name = class;
408 size = strlen(class);
409 BUG_ON(size > (size_t) U8_MAX);
410 op->cls.class_len = size;
411 payload_len += size;
412
413 op->cls.method_name = method;
414 size = strlen(method);
415 BUG_ON(size > (size_t) U8_MAX);
416 op->cls.method_len = size;
417 payload_len += size;
418
Alex Elder54d50642013-04-03 01:28:58 -0500419 op->cls.request_data = request_data;
Alex Elder33803f32013-03-13 20:50:00 -0500420 BUG_ON(request_data_size > (size_t) U32_MAX);
Alex Elder54d50642013-04-03 01:28:58 -0500421 op->cls.request_data_len = (u32) request_data_size;
Alex Elder33803f32013-03-13 20:50:00 -0500422 payload_len += request_data_size;
423
424 op->cls.argc = 0; /* currently unused */
425
426 op->payload_len = payload_len;
427}
428EXPORT_SYMBOL(osd_req_op_cls_init);
Alex Elderc99d2d42013-04-05 01:27:11 -0500429void osd_req_op_cls_response_data(struct ceph_osd_request *osd_req,
430 unsigned int which,
Alex Elder8c042b02013-04-03 01:28:58 -0500431 struct ceph_osd_data *response_data)
432{
Alex Elderc99d2d42013-04-05 01:27:11 -0500433 BUG_ON(which >= osd_req->r_num_ops);
434 osd_req->r_ops[which].cls.response_data = response_data;
Alex Elder8c042b02013-04-03 01:28:58 -0500435}
436EXPORT_SYMBOL(osd_req_op_cls_response_data);
437
Alex Elderc99d2d42013-04-05 01:27:11 -0500438void osd_req_op_watch_init(struct ceph_osd_request *osd_req,
439 unsigned int which, u16 opcode,
Alex Elder33803f32013-03-13 20:50:00 -0500440 u64 cookie, u64 version, int flag)
441{
Alex Elderc99d2d42013-04-05 01:27:11 -0500442 struct ceph_osd_req_op *op = osd_req_op_init(osd_req, which, opcode);
Alex Elder33803f32013-03-13 20:50:00 -0500443
Alex Elderc99d2d42013-04-05 01:27:11 -0500444 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
Alex Elder33803f32013-03-13 20:50:00 -0500445
446 op->watch.cookie = cookie;
447 /* op->watch.ver = version; */ /* XXX 3847 */
448 op->watch.ver = cpu_to_le64(version);
449 if (opcode == CEPH_OSD_OP_WATCH && flag)
Alex Elderc99d2d42013-04-05 01:27:11 -0500450 op->watch.flag = (u8)1;
Alex Elder33803f32013-03-13 20:50:00 -0500451}
452EXPORT_SYMBOL(osd_req_op_watch_init);
453
Alex Elder175face2013-03-08 13:35:36 -0600454static u64 osd_req_encode_op(struct ceph_osd_request *req,
Alex Elder79528732013-04-03 21:32:51 -0500455 struct ceph_osd_op *dst, unsigned int which)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700456{
Alex Elder79528732013-04-03 21:32:51 -0500457 struct ceph_osd_req_op *src;
Alex Elder54d50642013-04-03 01:28:58 -0500458 u64 request_data_len = 0;
Alex Elder95e072e2013-03-08 13:35:36 -0600459 struct ceph_pagelist *pagelist;
Alex Elder175face2013-03-08 13:35:36 -0600460
Alex Elder79528732013-04-03 21:32:51 -0500461 BUG_ON(which >= req->r_num_ops);
462 src = &req->r_ops[which];
Alex Eldera8dd0a32013-03-13 20:50:00 -0500463 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
464 pr_err("unrecognized osd opcode %d\n", src->op);
465
466 return 0;
467 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700468
Alex Elder065a68f2012-04-20 15:49:43 -0500469 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600470 case CEPH_OSD_OP_STAT:
471 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700472 case CEPH_OSD_OP_READ:
473 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600474 if (src->op == CEPH_OSD_OP_WRITE)
Alex Elder54d50642013-04-03 01:28:58 -0500475 request_data_len = src->extent.length;
Alex Elder175face2013-03-08 13:35:36 -0600476 dst->extent.offset = cpu_to_le64(src->extent.offset);
477 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700478 dst->extent.truncate_size =
479 cpu_to_le64(src->extent.truncate_size);
480 dst->extent.truncate_seq =
481 cpu_to_le32(src->extent.truncate_seq);
Alex Elder8c042b02013-04-03 01:28:58 -0500482 if (src->op == CEPH_OSD_OP_WRITE)
483 WARN_ON(src->extent.osd_data != &req->r_data_out);
484 else
485 WARN_ON(src->extent.osd_data != &req->r_data_in);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700486 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700487 case CEPH_OSD_OP_CALL:
Alex Elder95e072e2013-03-08 13:35:36 -0600488 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
489 BUG_ON(!pagelist);
490 ceph_pagelist_init(pagelist);
491
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700492 dst->cls.class_len = src->cls.class_len;
493 dst->cls.method_len = src->cls.method_len;
Alex Elder54d50642013-04-03 01:28:58 -0500494 dst->cls.indata_len = cpu_to_le32(src->cls.request_data_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600495 ceph_pagelist_append(pagelist, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700496 src->cls.class_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600497 ceph_pagelist_append(pagelist, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700498 src->cls.method_len);
Alex Elder54d50642013-04-03 01:28:58 -0500499 ceph_pagelist_append(pagelist, src->cls.request_data,
500 src->cls.request_data_len);
Alex Elder43bfe5d2013-04-03 01:28:57 -0500501 ceph_osd_data_pagelist_init(&req->r_data_out, pagelist);
Alex Elder8c042b02013-04-03 01:28:58 -0500502
503 WARN_ON(src->cls.response_data != &req->r_data_in);
Alex Elder54d50642013-04-03 01:28:58 -0500504 request_data_len = pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700505 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700506 case CEPH_OSD_OP_STARTSYNC:
507 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700508 case CEPH_OSD_OP_NOTIFY_ACK:
509 case CEPH_OSD_OP_WATCH:
510 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
511 dst->watch.ver = cpu_to_le64(src->watch.ver);
512 dst->watch.flag = src->watch.flag;
513 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700514 default:
Alex Elder4c464592013-02-15 11:42:30 -0600515 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600516 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600517 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500518
519 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700520 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500521 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700522 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600523
Alex Elder54d50642013-04-03 01:28:58 -0500524 return request_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700525}
526
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700527/*
528 * build new request AND message
529 *
530 */
Alex Elder79528732013-04-03 21:32:51 -0500531void ceph_osdc_build_request(struct ceph_osd_request *req, u64 off,
532 struct ceph_snap_context *snapc, u64 snap_id,
533 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700534{
535 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700536 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800537 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700538 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600539 u64 data_len;
Alex Elder79528732013-04-03 21:32:51 -0500540 unsigned int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700541
Sage Weil1b83bef2013-02-25 16:11:12 -0800542 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700543 req->r_snapc = ceph_get_snap_context(snapc);
544
Sage Weil1b83bef2013-02-25 16:11:12 -0800545 /* encode request */
546 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700547
Sage Weil1b83bef2013-02-25 16:11:12 -0800548 p = msg->front.iov_base;
549 ceph_encode_32(&p, 1); /* client_inc is always 1 */
550 req->r_request_osdmap_epoch = p;
551 p += 4;
552 req->r_request_flags = p;
553 p += 4;
554 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
555 ceph_encode_timespec(p, mtime);
556 p += sizeof(struct ceph_timespec);
557 req->r_request_reassert_version = p;
558 p += sizeof(struct ceph_eversion); /* will get filled in */
559
560 /* oloc */
561 ceph_encode_8(&p, 4);
562 ceph_encode_8(&p, 4);
563 ceph_encode_32(&p, 8 + 4 + 4);
564 req->r_request_pool = p;
565 p += 8;
566 ceph_encode_32(&p, -1); /* preferred */
567 ceph_encode_32(&p, 0); /* key len */
568
569 ceph_encode_8(&p, 1);
570 req->r_request_pgid = p;
571 p += 8 + 4;
572 ceph_encode_32(&p, -1); /* preferred */
573
574 /* oid */
575 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600576 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800577 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600578 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700579
Alex Elder175face2013-03-08 13:35:36 -0600580 /* ops--can imply data */
Alex Elder79528732013-04-03 21:32:51 -0500581 ceph_encode_16(&p, (u16)req->r_num_ops);
Alex Elder175face2013-03-08 13:35:36 -0600582 data_len = 0;
Alex Elder79528732013-04-03 21:32:51 -0500583 for (i = 0; i < req->r_num_ops; i++) {
584 data_len += osd_req_encode_op(req, p, i);
Sage Weil1b83bef2013-02-25 16:11:12 -0800585 p += sizeof(struct ceph_osd_op);
586 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700587
Sage Weil1b83bef2013-02-25 16:11:12 -0800588 /* snaps */
589 ceph_encode_64(&p, req->r_snapid);
590 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
591 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
592 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700593 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800594 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700595 }
596 }
597
Sage Weil1b83bef2013-02-25 16:11:12 -0800598 req->r_request_attempts = p;
599 p += 4;
600
Alex Elder175face2013-03-08 13:35:36 -0600601 /* data */
Alex Elder0baa1bd2013-03-29 14:28:03 -0500602 if (flags & CEPH_OSD_FLAG_WRITE) {
603 u16 data_off;
604
605 /*
606 * The header "data_off" is a hint to the receiver
607 * allowing it to align received data into its
608 * buffers such that there's no need to re-copy
609 * it before writing it to disk (direct I/O).
610 */
611 data_off = (u16) (off & 0xffff);
612 req->r_request->hdr.data_off = cpu_to_le16(data_off);
613 }
Alex Elderf44246e2013-02-14 12:16:43 -0600614 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800615
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700616 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
617 msg_size = p - msg->front.iov_base;
618 msg->front.iov_len = msg_size;
619 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800620
Alex Elderacead002013-03-14 14:09:05 -0500621 dout("build_request msg_size was %d\n", (int)msg_size);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700622}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700623EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700624
Sage Weilf24e9982009-10-06 11:31:10 -0700625/*
626 * build new request AND message, calculate layout, and adjust file
627 * extent as needed.
628 *
629 * if the file was recently truncated, we include information about its
630 * old and new size so that the object can be updated appropriately. (we
631 * avoid synchronously deleting truncated objects because it's slow.)
632 *
633 * if @do_sync, include a 'startsync' command so that the osd will flush
634 * data quickly.
635 */
636struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
637 struct ceph_file_layout *layout,
638 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500639 u64 off, u64 *plen, int num_ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700640 int opcode, int flags,
641 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700642 u32 truncate_seq,
643 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600644 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700645{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700646 struct ceph_osd_request *req;
Alex Elder8c042b02013-04-03 01:28:58 -0500647 struct ceph_osd_data *osd_data;
Alex Elder75d1c942013-03-13 20:50:00 -0500648 u64 objnum = 0;
649 u64 objoff = 0;
650 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500651 u32 object_size;
652 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700653 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700654
Alex Elderd18d1e22013-03-13 20:50:01 -0500655 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700656
Alex Elderacead002013-03-14 14:09:05 -0500657 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600658 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700659 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700660 return ERR_PTR(-ENOMEM);
Alex Elder8c042b02013-04-03 01:28:58 -0500661 osd_data = opcode == CEPH_OSD_OP_WRITE ? &req->r_data_out
662 : &req->r_data_in;
Alex Elder79528732013-04-03 21:32:51 -0500663
Alex Elderd178a9e2012-11-13 21:11:15 -0600664 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700665
666 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500667 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600668 if (r < 0) {
669 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700670 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600671 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500672
Alex Elderd18d1e22013-03-13 20:50:01 -0500673 object_size = le32_to_cpu(layout->fl_object_size);
674 object_base = off - objoff;
675 if (truncate_size <= object_base) {
676 truncate_size = 0;
677 } else {
678 truncate_size -= object_base;
679 if (truncate_size > object_size)
680 truncate_size = object_size;
Alex Eldera19dadf2013-03-13 20:50:01 -0500681 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500682
Alex Elderc99d2d42013-04-05 01:27:11 -0500683 osd_req_op_extent_init(req, 0, opcode, objoff, objlen,
Alex Elderb0270322013-03-13 20:50:01 -0500684 truncate_size, truncate_seq);
Alex Elderc99d2d42013-04-05 01:27:11 -0500685 osd_req_op_extent_osd_data(req, 0, osd_data);
Alex Elder8c042b02013-04-03 01:28:58 -0500686
Alex Elderacead002013-03-14 14:09:05 -0500687 /*
688 * A second op in the ops array means the caller wants to
689 * also issue a include a 'startsync' command so that the
690 * osd will flush data quickly.
691 */
692 if (num_ops > 1)
Alex Elderc99d2d42013-04-05 01:27:11 -0500693 osd_req_op_init(req, 1, CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500694
Sage Weilf24e9982009-10-06 11:31:10 -0700695 req->r_file_layout = *layout; /* keep a copy */
696
Alex Elder75d1c942013-03-13 20:50:00 -0500697 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
698 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600699 req->r_oid_len = strlen(req->r_oid);
700
Sage Weilf24e9982009-10-06 11:31:10 -0700701 return req;
702}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700703EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700704
705/*
706 * We keep osd requests in an rbtree, sorted by ->r_tid.
707 */
708static void __insert_request(struct ceph_osd_client *osdc,
709 struct ceph_osd_request *new)
710{
711 struct rb_node **p = &osdc->requests.rb_node;
712 struct rb_node *parent = NULL;
713 struct ceph_osd_request *req = NULL;
714
715 while (*p) {
716 parent = *p;
717 req = rb_entry(parent, struct ceph_osd_request, r_node);
718 if (new->r_tid < req->r_tid)
719 p = &(*p)->rb_left;
720 else if (new->r_tid > req->r_tid)
721 p = &(*p)->rb_right;
722 else
723 BUG();
724 }
725
726 rb_link_node(&new->r_node, parent, p);
727 rb_insert_color(&new->r_node, &osdc->requests);
728}
729
730static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
731 u64 tid)
732{
733 struct ceph_osd_request *req;
734 struct rb_node *n = osdc->requests.rb_node;
735
736 while (n) {
737 req = rb_entry(n, struct ceph_osd_request, r_node);
738 if (tid < req->r_tid)
739 n = n->rb_left;
740 else if (tid > req->r_tid)
741 n = n->rb_right;
742 else
743 return req;
744 }
745 return NULL;
746}
747
748static struct ceph_osd_request *
749__lookup_request_ge(struct ceph_osd_client *osdc,
750 u64 tid)
751{
752 struct ceph_osd_request *req;
753 struct rb_node *n = osdc->requests.rb_node;
754
755 while (n) {
756 req = rb_entry(n, struct ceph_osd_request, r_node);
757 if (tid < req->r_tid) {
758 if (!n->rb_left)
759 return req;
760 n = n->rb_left;
761 } else if (tid > req->r_tid) {
762 n = n->rb_right;
763 } else {
764 return req;
765 }
766 }
767 return NULL;
768}
769
Sage Weil6f6c7002011-01-17 20:34:08 -0800770/*
771 * Resubmit requests pending on the given osd.
772 */
773static void __kick_osd_requests(struct ceph_osd_client *osdc,
774 struct ceph_osd *osd)
775{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700776 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500777 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800778 int err;
779
780 dout("__kick_osd_requests osd%d\n", osd->o_osd);
781 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600782 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800783 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500784 /*
785 * Build up a list of requests to resend by traversing the
786 * osd's list of requests. Requests for a given object are
787 * sent in tid order, and that is also the order they're
788 * kept on this list. Therefore all requests that are in
789 * flight will be found first, followed by all requests that
790 * have not yet been sent. And to resend requests while
791 * preserving this order we will want to put any sent
792 * requests back on the front of the osd client's unsent
793 * list.
794 *
795 * So we build a separate ordered list of already-sent
796 * requests for the affected osd and splice it onto the
797 * front of the osd client's unsent list. Once we've seen a
798 * request that has not yet been sent we're done. Those
799 * requests are already sitting right where they belong.
800 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800801 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500802 if (!req->r_sent)
803 break;
804 list_move_tail(&req->r_req_lru_item, &resend);
805 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800806 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700807 if (!req->r_linger)
808 req->r_flags |= CEPH_OSD_FLAG_RETRY;
809 }
Alex Eldere02493c2013-03-25 18:16:11 -0500810 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700811
Alex Eldere02493c2013-03-25 18:16:11 -0500812 /*
813 * Linger requests are re-registered before sending, which
814 * sets up a new tid for each. We add them to the unsent
815 * list at the end to keep things in tid order.
816 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700817 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
818 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700819 /*
820 * reregister request prior to unregistering linger so
821 * that r_osd is preserved.
822 */
823 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700824 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500825 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500826 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700827 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700828 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
829 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800830 }
831}
832
Sage Weilf24e9982009-10-06 11:31:10 -0700833/*
Sage Weil81b024e2009-10-09 10:29:18 -0700834 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700835 */
836static void osd_reset(struct ceph_connection *con)
837{
838 struct ceph_osd *osd = con->private;
839 struct ceph_osd_client *osdc;
840
841 if (!osd)
842 return;
843 dout("osd_reset osd%d\n", osd->o_osd);
844 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700845 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800846 mutex_lock(&osdc->request_mutex);
847 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600848 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800849 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700850 up_read(&osdc->map_sem);
851}
852
853/*
854 * Track open sessions with osds.
855 */
Alex Eldere10006f2012-05-26 23:26:43 -0500856static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700857{
858 struct ceph_osd *osd;
859
860 osd = kzalloc(sizeof(*osd), GFP_NOFS);
861 if (!osd)
862 return NULL;
863
864 atomic_set(&osd->o_ref, 1);
865 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500866 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600867 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700868 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700869 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800870 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700871 osd->o_incarnation = 1;
872
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700873 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800874
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800875 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700876 return osd;
877}
878
879static struct ceph_osd *get_osd(struct ceph_osd *osd)
880{
881 if (atomic_inc_not_zero(&osd->o_ref)) {
882 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
883 atomic_read(&osd->o_ref));
884 return osd;
885 } else {
886 dout("get_osd %p FAIL\n", osd);
887 return NULL;
888 }
889}
890
891static void put_osd(struct ceph_osd *osd)
892{
893 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
894 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500895 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700896 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
897
Sage Weil27859f92013-03-25 10:26:14 -0700898 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700899 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700900 }
Sage Weilf24e9982009-10-06 11:31:10 -0700901}
902
903/*
904 * remove an osd from our map
905 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800906static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700907{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800908 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700909 BUG_ON(!list_empty(&osd->o_requests));
910 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800911 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700912 ceph_con_close(&osd->o_con);
913 put_osd(osd);
914}
915
Sage Weilaca420b2011-08-31 14:45:53 -0700916static void remove_all_osds(struct ceph_osd_client *osdc)
917{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500918 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700919 mutex_lock(&osdc->request_mutex);
920 while (!RB_EMPTY_ROOT(&osdc->osds)) {
921 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
922 struct ceph_osd, o_node);
923 __remove_osd(osdc, osd);
924 }
925 mutex_unlock(&osdc->request_mutex);
926}
927
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800928static void __move_osd_to_lru(struct ceph_osd_client *osdc,
929 struct ceph_osd *osd)
930{
931 dout("__move_osd_to_lru %p\n", osd);
932 BUG_ON(!list_empty(&osd->o_osd_lru));
933 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700934 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800935}
936
937static void __remove_osd_from_lru(struct ceph_osd *osd)
938{
939 dout("__remove_osd_from_lru %p\n", osd);
940 if (!list_empty(&osd->o_osd_lru))
941 list_del_init(&osd->o_osd_lru);
942}
943
Sage Weilaca420b2011-08-31 14:45:53 -0700944static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800945{
946 struct ceph_osd *osd, *nosd;
947
948 dout("__remove_old_osds %p\n", osdc);
949 mutex_lock(&osdc->request_mutex);
950 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700951 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800952 break;
953 __remove_osd(osdc, osd);
954 }
955 mutex_unlock(&osdc->request_mutex);
956}
957
Sage Weilf24e9982009-10-06 11:31:10 -0700958/*
959 * reset osd connect
960 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800961static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700962{
Alex Elderc3acb182012-12-07 09:57:58 -0600963 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700964
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800965 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700966 if (list_empty(&osd->o_requests) &&
967 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800968 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600969
970 return -ENODEV;
971 }
972
973 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
974 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
975 !ceph_con_opened(&osd->o_con)) {
976 struct ceph_osd_request *req;
977
Sage Weil87b315a2010-03-22 14:51:18 -0700978 dout(" osd addr hasn't changed and connection never opened,"
979 " letting msgr retry");
980 /* touch each r_stamp for handle_timeout()'s benfit */
981 list_for_each_entry(req, &osd->o_requests, r_osd_item)
982 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600983
984 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700985 }
Alex Elderc3acb182012-12-07 09:57:58 -0600986
987 ceph_con_close(&osd->o_con);
988 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
989 osd->o_incarnation++;
990
991 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700992}
993
994static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
995{
996 struct rb_node **p = &osdc->osds.rb_node;
997 struct rb_node *parent = NULL;
998 struct ceph_osd *osd = NULL;
999
Sage Weilaca420b2011-08-31 14:45:53 -07001000 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001001 while (*p) {
1002 parent = *p;
1003 osd = rb_entry(parent, struct ceph_osd, o_node);
1004 if (new->o_osd < osd->o_osd)
1005 p = &(*p)->rb_left;
1006 else if (new->o_osd > osd->o_osd)
1007 p = &(*p)->rb_right;
1008 else
1009 BUG();
1010 }
1011
1012 rb_link_node(&new->o_node, parent, p);
1013 rb_insert_color(&new->o_node, &osdc->osds);
1014}
1015
1016static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
1017{
1018 struct ceph_osd *osd;
1019 struct rb_node *n = osdc->osds.rb_node;
1020
1021 while (n) {
1022 osd = rb_entry(n, struct ceph_osd, o_node);
1023 if (o < osd->o_osd)
1024 n = n->rb_left;
1025 else if (o > osd->o_osd)
1026 n = n->rb_right;
1027 else
1028 return osd;
1029 }
1030 return NULL;
1031}
1032
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001033static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
1034{
1035 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001036 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001037}
1038
1039static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
1040{
1041 cancel_delayed_work(&osdc->timeout_work);
1042}
Sage Weilf24e9982009-10-06 11:31:10 -07001043
1044/*
1045 * Register request, assign tid. If this is the first request, set up
1046 * the timeout event.
1047 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001048static void __register_request(struct ceph_osd_client *osdc,
1049 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001050{
Sage Weilf24e9982009-10-06 11:31:10 -07001051 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -08001052 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -07001053 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001054 __insert_request(osdc, req);
1055 ceph_osdc_get_request(req);
1056 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -07001057 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001058 dout(" first request, scheduling timeout\n");
1059 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001060 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001061}
1062
Sage Weilf24e9982009-10-06 11:31:10 -07001063/*
1064 * called under osdc->request_mutex
1065 */
1066static void __unregister_request(struct ceph_osd_client *osdc,
1067 struct ceph_osd_request *req)
1068{
Sage Weil35f9f8a2012-05-16 15:16:38 -05001069 if (RB_EMPTY_NODE(&req->r_node)) {
1070 dout("__unregister_request %p tid %lld not registered\n",
1071 req, req->r_tid);
1072 return;
1073 }
1074
Sage Weilf24e9982009-10-06 11:31:10 -07001075 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
1076 rb_erase(&req->r_node, &osdc->requests);
1077 osdc->num_requests--;
1078
Sage Weil0ba64782009-10-08 16:57:16 -07001079 if (req->r_osd) {
1080 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -05001081 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -07001082
1083 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001084 if (list_empty(&req->r_osd->o_requests) &&
1085 list_empty(&req->r_osd->o_linger_requests)) {
1086 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001087 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001088 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001089 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001090 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001091 }
Sage Weilf24e9982009-10-06 11:31:10 -07001092
Alex Elder7d5f2482012-11-29 08:37:03 -06001093 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001094 ceph_osdc_put_request(req);
1095
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001096 if (osdc->num_requests == 0) {
1097 dout(" no requests, canceling timeout\n");
1098 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001099 }
1100}
1101
1102/*
1103 * Cancel a previously queued request message
1104 */
1105static void __cancel_request(struct ceph_osd_request *req)
1106{
Sage Weil6bc18872010-09-27 10:18:52 -07001107 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001108 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001109 req->r_sent = 0;
1110 }
1111}
1112
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001113static void __register_linger_request(struct ceph_osd_client *osdc,
1114 struct ceph_osd_request *req)
1115{
1116 dout("__register_linger_request %p\n", req);
1117 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001118 if (req->r_osd)
1119 list_add_tail(&req->r_linger_osd,
1120 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001121}
1122
1123static void __unregister_linger_request(struct ceph_osd_client *osdc,
1124 struct ceph_osd_request *req)
1125{
1126 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001127 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001128 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001129 list_del_init(&req->r_linger_osd);
1130
1131 if (list_empty(&req->r_osd->o_requests) &&
1132 list_empty(&req->r_osd->o_linger_requests)) {
1133 dout("moving osd to %p lru\n", req->r_osd);
1134 __move_osd_to_lru(osdc, req->r_osd);
1135 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001136 if (list_empty(&req->r_osd_item))
1137 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001138 }
1139}
1140
1141void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1142 struct ceph_osd_request *req)
1143{
1144 mutex_lock(&osdc->request_mutex);
1145 if (req->r_linger) {
1146 __unregister_linger_request(osdc, req);
1147 ceph_osdc_put_request(req);
1148 }
1149 mutex_unlock(&osdc->request_mutex);
1150}
1151EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1152
1153void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1154 struct ceph_osd_request *req)
1155{
1156 if (!req->r_linger) {
1157 dout("set_request_linger %p\n", req);
1158 req->r_linger = 1;
1159 /*
1160 * caller is now responsible for calling
1161 * unregister_linger_request
1162 */
1163 ceph_osdc_get_request(req);
1164 }
1165}
1166EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1167
Sage Weilf24e9982009-10-06 11:31:10 -07001168/*
1169 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1170 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001171 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001172 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001173 *
1174 * Return 0 if unchanged, 1 if changed, or negative on error.
1175 *
1176 * Caller should hold map_sem for read and request_mutex.
1177 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001178static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001179 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001180{
Sage Weil5b191d92013-02-23 10:38:16 -08001181 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001182 int acting[CEPH_PG_MAX_SIZE];
1183 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001184 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001185
Sage Weil6f6c7002011-01-17 20:34:08 -08001186 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001187 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1188 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001189 if (err) {
1190 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001191 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001192 }
Sage Weil7740a422010-01-08 15:58:25 -08001193 req->r_pgid = pgid;
1194
Sage Weild85b7052010-05-10 10:24:48 -07001195 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1196 if (err > 0) {
1197 o = acting[0];
1198 num = err;
1199 }
Sage Weilf24e9982009-10-06 11:31:10 -07001200
Sage Weil38d64532011-10-14 13:33:55 -07001201 if ((!force_resend &&
1202 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001203 req->r_sent >= req->r_osd->o_incarnation &&
1204 req->r_num_pg_osds == num &&
1205 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001206 (req->r_osd == NULL && o == -1))
1207 return 0; /* no change */
1208
Sage Weil5b191d92013-02-23 10:38:16 -08001209 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1210 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001211 req->r_osd ? req->r_osd->o_osd : -1);
1212
Sage Weild85b7052010-05-10 10:24:48 -07001213 /* record full pg acting set */
1214 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1215 req->r_num_pg_osds = num;
1216
Sage Weilf24e9982009-10-06 11:31:10 -07001217 if (req->r_osd) {
1218 __cancel_request(req);
1219 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001220 req->r_osd = NULL;
1221 }
1222
1223 req->r_osd = __lookup_osd(osdc, o);
1224 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001225 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001226 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001227 if (!req->r_osd) {
1228 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001229 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001230 }
Sage Weilf24e9982009-10-06 11:31:10 -07001231
Sage Weil6f6c7002011-01-17 20:34:08 -08001232 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001233 __insert_osd(osdc, req->r_osd);
1234
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001235 ceph_con_open(&req->r_osd->o_con,
1236 CEPH_ENTITY_TYPE_OSD, o,
1237 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001238 }
1239
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001240 if (req->r_osd) {
1241 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001242 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1243 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001244 } else {
Alex Elderad885922013-03-25 18:16:11 -05001245 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001246 }
Sage Weild85b7052010-05-10 10:24:48 -07001247 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001248
1249out:
Sage Weilf24e9982009-10-06 11:31:10 -07001250 return err;
1251}
1252
1253/*
1254 * caller should hold map_sem (for read) and request_mutex
1255 */
Sage Weil56e925b2012-01-03 12:34:34 -08001256static void __send_request(struct ceph_osd_client *osdc,
1257 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001258{
Sage Weil1b83bef2013-02-25 16:11:12 -08001259 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001260
Sage Weil1b83bef2013-02-25 16:11:12 -08001261 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1262 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1263 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001264
Sage Weil1b83bef2013-02-25 16:11:12 -08001265 /* fill in message content that changes each time we send it */
1266 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1267 put_unaligned_le32(req->r_flags, req->r_request_flags);
1268 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1269 p = req->r_request_pgid;
1270 ceph_encode_64(&p, req->r_pgid.pool);
1271 ceph_encode_32(&p, req->r_pgid.seed);
1272 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1273 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1274 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001275
Sage Weil3dd72fc2010-03-22 14:42:30 -07001276 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001277 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001278
1279 ceph_msg_get(req->r_request); /* send consumes a ref */
1280 ceph_con_send(&req->r_osd->o_con, req->r_request);
1281 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001282}
1283
1284/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001285 * Send any requests in the queue (req_unsent).
1286 */
Alex Elderf9d25192013-02-15 11:42:29 -06001287static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001288{
1289 struct ceph_osd_request *req, *tmp;
1290
Alex Elderf9d25192013-02-15 11:42:29 -06001291 dout("__send_queued\n");
1292 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001293 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001294}
1295
1296/*
Sage Weilf24e9982009-10-06 11:31:10 -07001297 * Timeout callback, called every N seconds when 1 or more osd
1298 * requests has been active for more than N seconds. When this
1299 * happens, we ping all OSDs with requests who have timed out to
1300 * ensure any communications channel reset is detected. Reset the
1301 * request timeouts another N seconds in the future as we go.
1302 * Reschedule the timeout event another N seconds in future (unless
1303 * there are no open requests).
1304 */
1305static void handle_timeout(struct work_struct *work)
1306{
1307 struct ceph_osd_client *osdc =
1308 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001309 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001310 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001311 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001312 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001313 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001314 dout("timeout\n");
1315 down_read(&osdc->map_sem);
1316
1317 ceph_monc_request_next_osdmap(&osdc->client->monc);
1318
1319 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001320
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001321 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001322 * ping osds that are a bit slow. this ensures that if there
1323 * is a break in the TCP connection we will notice, and reopen
1324 * a connection with that osd (from the fault callback).
1325 */
1326 INIT_LIST_HEAD(&slow_osds);
1327 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001328 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001329 break;
1330
1331 osd = req->r_osd;
1332 BUG_ON(!osd);
1333 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001334 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001335 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1336 }
1337 while (!list_empty(&slow_osds)) {
1338 osd = list_entry(slow_osds.next, struct ceph_osd,
1339 o_keepalive_item);
1340 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001341 ceph_con_keepalive(&osd->o_con);
1342 }
1343
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001344 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001345 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001346 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001347 up_read(&osdc->map_sem);
1348}
1349
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001350static void handle_osds_timeout(struct work_struct *work)
1351{
1352 struct ceph_osd_client *osdc =
1353 container_of(work, struct ceph_osd_client,
1354 osds_timeout_work.work);
1355 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001356 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001357
1358 dout("osds timeout\n");
1359 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001360 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001361 up_read(&osdc->map_sem);
1362
1363 schedule_delayed_work(&osdc->osds_timeout_work,
1364 round_jiffies_relative(delay));
1365}
1366
Sage Weil25845472011-06-03 09:37:09 -07001367static void complete_request(struct ceph_osd_request *req)
1368{
1369 if (req->r_safe_callback)
1370 req->r_safe_callback(req, NULL);
1371 complete_all(&req->r_safe_completion); /* fsync waiter */
1372}
1373
Sage Weilf24e9982009-10-06 11:31:10 -07001374/*
1375 * handle osd op reply. either call the callback if it is specified,
1376 * or do the completion to wake up the waiting thread.
1377 */
Sage Weil350b1c32009-12-22 10:45:45 -08001378static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1379 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001380{
Sage Weil1b83bef2013-02-25 16:11:12 -08001381 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001382 struct ceph_osd_request *req;
1383 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001384 int object_len;
Alex Elder79528732013-04-03 21:32:51 -05001385 unsigned int numops;
1386 int payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001387 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001388 s32 retry_attempt;
1389 struct ceph_pg pg;
1390 int err;
1391 u32 reassert_epoch;
1392 u64 reassert_version;
1393 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001394 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001395 u32 bytes;
Alex Elder79528732013-04-03 21:32:51 -05001396 unsigned int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001397
Sage Weil6df058c2009-12-22 11:24:33 -08001398 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001399 dout("handle_reply %p tid %llu\n", msg, tid);
1400
1401 p = msg->front.iov_base;
1402 end = p + msg->front.iov_len;
1403
1404 ceph_decode_need(&p, end, 4, bad);
1405 object_len = ceph_decode_32(&p);
1406 ceph_decode_need(&p, end, object_len, bad);
1407 p += object_len;
1408
Alex Elderef4859d2013-04-01 18:58:26 -05001409 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001410 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001411 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001412
1413 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1414 flags = ceph_decode_64(&p);
1415 result = ceph_decode_32(&p);
1416 reassert_epoch = ceph_decode_32(&p);
1417 reassert_version = ceph_decode_64(&p);
1418 osdmap_epoch = ceph_decode_32(&p);
1419
Sage Weilf24e9982009-10-06 11:31:10 -07001420 /* lookup */
1421 mutex_lock(&osdc->request_mutex);
1422 req = __lookup_request(osdc, tid);
1423 if (req == NULL) {
1424 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001425 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001426 }
1427 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001428
1429 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1430 req, result);
1431
1432 ceph_decode_need(&p, end, 4, bad);
1433 numops = ceph_decode_32(&p);
1434 if (numops > CEPH_OSD_MAX_OP)
1435 goto bad_put;
1436 if (numops != req->r_num_ops)
1437 goto bad_put;
1438 payload_len = 0;
1439 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1440 for (i = 0; i < numops; i++) {
1441 struct ceph_osd_op *op = p;
1442 int len;
1443
1444 len = le32_to_cpu(op->payload_len);
1445 req->r_reply_op_len[i] = len;
1446 dout(" op %d has %d bytes\n", i, len);
1447 payload_len += len;
1448 p += sizeof(*op);
1449 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001450 bytes = le32_to_cpu(msg->hdr.data_len);
1451 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001452 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001453 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001454 goto bad_put;
1455 }
1456
1457 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1458 retry_attempt = ceph_decode_32(&p);
1459 for (i = 0; i < numops; i++)
1460 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001461
Sage Weilf24e9982009-10-06 11:31:10 -07001462 if (!req->r_got_reply) {
Sage Weilf24e9982009-10-06 11:31:10 -07001463
Sage Weil1b83bef2013-02-25 16:11:12 -08001464 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001465 dout("handle_reply result %d bytes %d\n", req->r_result,
1466 bytes);
1467 if (req->r_result == 0)
1468 req->r_result = bytes;
1469
1470 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001471 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1472 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001473
1474 req->r_got_reply = 1;
1475 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1476 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001477 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001478 goto done;
1479 }
1480
1481 dout("handle_reply tid %llu flags %d\n", tid, flags);
1482
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001483 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1484 __register_linger_request(osdc, req);
1485
Sage Weilf24e9982009-10-06 11:31:10 -07001486 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001487 if (result < 0 ||
1488 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001489 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1490 __unregister_request(osdc, req);
1491
Alex Elder0d5af162013-02-27 10:26:25 -06001492 already_completed = req->r_completed;
1493 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001494 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001495 if (already_completed)
1496 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001497
1498 if (req->r_callback)
1499 req->r_callback(req, msg);
1500 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001501 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001502
Sage Weil25845472011-06-03 09:37:09 -07001503 if (flags & CEPH_OSD_FLAG_ONDISK)
1504 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001505
1506done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001507 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001508 ceph_osdc_put_request(req);
1509 return;
1510
Sage Weil1b83bef2013-02-25 16:11:12 -08001511bad_put:
1512 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001513bad_mutex:
1514 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001515bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001516 pr_err("corrupt osd_op_reply got %d %d\n",
1517 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001518 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001519}
1520
Sage Weil6f6c7002011-01-17 20:34:08 -08001521static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001522{
Sage Weilf24e9982009-10-06 11:31:10 -07001523 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001524
Sage Weil6f6c7002011-01-17 20:34:08 -08001525 for (p = rb_first(&osdc->osds); p; p = n) {
1526 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001527
Sage Weil6f6c7002011-01-17 20:34:08 -08001528 n = rb_next(p);
1529 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1530 memcmp(&osd->o_con.peer_addr,
1531 ceph_osd_addr(osdc->osdmap,
1532 osd->o_osd),
1533 sizeof(struct ceph_entity_addr)) != 0)
1534 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001535 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001536}
1537
1538/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001539 * Requeue requests whose mapping to an OSD has changed. If requests map to
1540 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001541 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001542 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001543 */
Sage Weil38d64532011-10-14 13:33:55 -07001544static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001545{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001546 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001547 struct rb_node *p;
1548 int needmap = 0;
1549 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001550
Sage Weil38d64532011-10-14 13:33:55 -07001551 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001552 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001553 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001554 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001555 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001556
1557 /*
1558 * For linger requests that have not yet been
1559 * registered, move them to the linger list; they'll
1560 * be sent to the osd in the loop below. Unregister
1561 * the request before re-registering it as a linger
1562 * request to ensure the __map_request() below
1563 * will decide it needs to be sent.
1564 */
1565 if (req->r_linger && list_empty(&req->r_linger_item)) {
1566 dout("%p tid %llu restart on osd%d\n",
1567 req, req->r_tid,
1568 req->r_osd ? req->r_osd->o_osd : -1);
1569 __unregister_request(osdc, req);
1570 __register_linger_request(osdc, req);
1571 continue;
1572 }
1573
Sage Weil38d64532011-10-14 13:33:55 -07001574 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001575 if (err < 0)
1576 continue; /* error */
1577 if (req->r_osd == NULL) {
1578 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1579 needmap++; /* request a newer map */
1580 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001581 if (!req->r_linger) {
1582 dout("%p tid %llu requeued on osd%d\n", req,
1583 req->r_tid,
1584 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001585 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001586 }
1587 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001588 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001589
1590 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1591 r_linger_item) {
1592 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1593
Sage Weil38d64532011-10-14 13:33:55 -07001594 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001595 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001596 if (err == 0)
1597 continue; /* no change and no osd was specified */
1598 if (err < 0)
1599 continue; /* hrm! */
1600 if (req->r_osd == NULL) {
1601 dout("tid %llu maps to no valid osd\n", req->r_tid);
1602 needmap++; /* request a newer map */
1603 continue;
1604 }
1605
1606 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1607 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001608 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001609 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001610 }
Sage Weilf24e9982009-10-06 11:31:10 -07001611 mutex_unlock(&osdc->request_mutex);
1612
1613 if (needmap) {
1614 dout("%d requests for down osds, need new map\n", needmap);
1615 ceph_monc_request_next_osdmap(&osdc->client->monc);
1616 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001617 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001618}
Sage Weil6f6c7002011-01-17 20:34:08 -08001619
1620
Sage Weilf24e9982009-10-06 11:31:10 -07001621/*
1622 * Process updated osd map.
1623 *
1624 * The message contains any number of incremental and full maps, normally
1625 * indicating some sort of topology change in the cluster. Kick requests
1626 * off to different OSDs as needed.
1627 */
1628void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1629{
1630 void *p, *end, *next;
1631 u32 nr_maps, maplen;
1632 u32 epoch;
1633 struct ceph_osdmap *newmap = NULL, *oldmap;
1634 int err;
1635 struct ceph_fsid fsid;
1636
1637 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1638 p = msg->front.iov_base;
1639 end = p + msg->front.iov_len;
1640
1641 /* verify fsid */
1642 ceph_decode_need(&p, end, sizeof(fsid), bad);
1643 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001644 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1645 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001646
1647 down_write(&osdc->map_sem);
1648
1649 /* incremental maps */
1650 ceph_decode_32_safe(&p, end, nr_maps, bad);
1651 dout(" %d inc maps\n", nr_maps);
1652 while (nr_maps > 0) {
1653 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001654 epoch = ceph_decode_32(&p);
1655 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001656 ceph_decode_need(&p, end, maplen, bad);
1657 next = p + maplen;
1658 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1659 dout("applying incremental map %u len %d\n",
1660 epoch, maplen);
1661 newmap = osdmap_apply_incremental(&p, next,
1662 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001663 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001664 if (IS_ERR(newmap)) {
1665 err = PTR_ERR(newmap);
1666 goto bad;
1667 }
Sage Weil30dc6382009-12-21 14:49:37 -08001668 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001669 if (newmap != osdc->osdmap) {
1670 ceph_osdmap_destroy(osdc->osdmap);
1671 osdc->osdmap = newmap;
1672 }
Sage Weil38d64532011-10-14 13:33:55 -07001673 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001674 } else {
1675 dout("ignoring incremental map %u len %d\n",
1676 epoch, maplen);
1677 }
1678 p = next;
1679 nr_maps--;
1680 }
1681 if (newmap)
1682 goto done;
1683
1684 /* full maps */
1685 ceph_decode_32_safe(&p, end, nr_maps, bad);
1686 dout(" %d full maps\n", nr_maps);
1687 while (nr_maps) {
1688 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001689 epoch = ceph_decode_32(&p);
1690 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001691 ceph_decode_need(&p, end, maplen, bad);
1692 if (nr_maps > 1) {
1693 dout("skipping non-latest full map %u len %d\n",
1694 epoch, maplen);
1695 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1696 dout("skipping full map %u len %d, "
1697 "older than our %u\n", epoch, maplen,
1698 osdc->osdmap->epoch);
1699 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001700 int skipped_map = 0;
1701
Sage Weilf24e9982009-10-06 11:31:10 -07001702 dout("taking full map %u len %d\n", epoch, maplen);
1703 newmap = osdmap_decode(&p, p+maplen);
1704 if (IS_ERR(newmap)) {
1705 err = PTR_ERR(newmap);
1706 goto bad;
1707 }
Sage Weil30dc6382009-12-21 14:49:37 -08001708 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001709 oldmap = osdc->osdmap;
1710 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001711 if (oldmap) {
1712 if (oldmap->epoch + 1 < newmap->epoch)
1713 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001714 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001715 }
1716 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001717 }
1718 p += maplen;
1719 nr_maps--;
1720 }
1721
1722done:
1723 downgrade_write(&osdc->map_sem);
1724 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001725
1726 /*
1727 * subscribe to subsequent osdmap updates if full to ensure
1728 * we find out when we are no longer full and stop returning
1729 * ENOSPC.
1730 */
1731 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1732 ceph_monc_request_next_osdmap(&osdc->client->monc);
1733
Alex Elderf9d25192013-02-15 11:42:29 -06001734 mutex_lock(&osdc->request_mutex);
1735 __send_queued(osdc);
1736 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001737 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001738 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001739 return;
1740
1741bad:
1742 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001743 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001744 up_write(&osdc->map_sem);
1745 return;
1746}
1747
Sage Weilf24e9982009-10-06 11:31:10 -07001748/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001749 * watch/notify callback event infrastructure
1750 *
1751 * These callbacks are used both for watch and notify operations.
1752 */
1753static void __release_event(struct kref *kref)
1754{
1755 struct ceph_osd_event *event =
1756 container_of(kref, struct ceph_osd_event, kref);
1757
1758 dout("__release_event %p\n", event);
1759 kfree(event);
1760}
1761
1762static void get_event(struct ceph_osd_event *event)
1763{
1764 kref_get(&event->kref);
1765}
1766
1767void ceph_osdc_put_event(struct ceph_osd_event *event)
1768{
1769 kref_put(&event->kref, __release_event);
1770}
1771EXPORT_SYMBOL(ceph_osdc_put_event);
1772
1773static void __insert_event(struct ceph_osd_client *osdc,
1774 struct ceph_osd_event *new)
1775{
1776 struct rb_node **p = &osdc->event_tree.rb_node;
1777 struct rb_node *parent = NULL;
1778 struct ceph_osd_event *event = NULL;
1779
1780 while (*p) {
1781 parent = *p;
1782 event = rb_entry(parent, struct ceph_osd_event, node);
1783 if (new->cookie < event->cookie)
1784 p = &(*p)->rb_left;
1785 else if (new->cookie > event->cookie)
1786 p = &(*p)->rb_right;
1787 else
1788 BUG();
1789 }
1790
1791 rb_link_node(&new->node, parent, p);
1792 rb_insert_color(&new->node, &osdc->event_tree);
1793}
1794
1795static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1796 u64 cookie)
1797{
1798 struct rb_node **p = &osdc->event_tree.rb_node;
1799 struct rb_node *parent = NULL;
1800 struct ceph_osd_event *event = NULL;
1801
1802 while (*p) {
1803 parent = *p;
1804 event = rb_entry(parent, struct ceph_osd_event, node);
1805 if (cookie < event->cookie)
1806 p = &(*p)->rb_left;
1807 else if (cookie > event->cookie)
1808 p = &(*p)->rb_right;
1809 else
1810 return event;
1811 }
1812 return NULL;
1813}
1814
1815static void __remove_event(struct ceph_osd_event *event)
1816{
1817 struct ceph_osd_client *osdc = event->osdc;
1818
1819 if (!RB_EMPTY_NODE(&event->node)) {
1820 dout("__remove_event removed %p\n", event);
1821 rb_erase(&event->node, &osdc->event_tree);
1822 ceph_osdc_put_event(event);
1823 } else {
1824 dout("__remove_event didn't remove %p\n", event);
1825 }
1826}
1827
1828int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1829 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001830 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001831{
1832 struct ceph_osd_event *event;
1833
1834 event = kmalloc(sizeof(*event), GFP_NOIO);
1835 if (!event)
1836 return -ENOMEM;
1837
1838 dout("create_event %p\n", event);
1839 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001840 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001841 event->data = data;
1842 event->osdc = osdc;
1843 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001844 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001845 kref_init(&event->kref); /* one ref for us */
1846 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001847
1848 spin_lock(&osdc->event_lock);
1849 event->cookie = ++osdc->event_count;
1850 __insert_event(osdc, event);
1851 spin_unlock(&osdc->event_lock);
1852
1853 *pevent = event;
1854 return 0;
1855}
1856EXPORT_SYMBOL(ceph_osdc_create_event);
1857
1858void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1859{
1860 struct ceph_osd_client *osdc = event->osdc;
1861
1862 dout("cancel_event %p\n", event);
1863 spin_lock(&osdc->event_lock);
1864 __remove_event(event);
1865 spin_unlock(&osdc->event_lock);
1866 ceph_osdc_put_event(event); /* caller's */
1867}
1868EXPORT_SYMBOL(ceph_osdc_cancel_event);
1869
1870
1871static void do_event_work(struct work_struct *work)
1872{
1873 struct ceph_osd_event_work *event_work =
1874 container_of(work, struct ceph_osd_event_work, work);
1875 struct ceph_osd_event *event = event_work->event;
1876 u64 ver = event_work->ver;
1877 u64 notify_id = event_work->notify_id;
1878 u8 opcode = event_work->opcode;
1879
1880 dout("do_event_work completing %p\n", event);
1881 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001882 dout("do_event_work completed %p\n", event);
1883 ceph_osdc_put_event(event);
1884 kfree(event_work);
1885}
1886
1887
1888/*
1889 * Process osd watch notifications
1890 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001891static void handle_watch_notify(struct ceph_osd_client *osdc,
1892 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001893{
1894 void *p, *end;
1895 u8 proto_ver;
1896 u64 cookie, ver, notify_id;
1897 u8 opcode;
1898 struct ceph_osd_event *event;
1899 struct ceph_osd_event_work *event_work;
1900
1901 p = msg->front.iov_base;
1902 end = p + msg->front.iov_len;
1903
1904 ceph_decode_8_safe(&p, end, proto_ver, bad);
1905 ceph_decode_8_safe(&p, end, opcode, bad);
1906 ceph_decode_64_safe(&p, end, cookie, bad);
1907 ceph_decode_64_safe(&p, end, ver, bad);
1908 ceph_decode_64_safe(&p, end, notify_id, bad);
1909
1910 spin_lock(&osdc->event_lock);
1911 event = __find_event(osdc, cookie);
1912 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001913 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001914 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001915 }
1916 spin_unlock(&osdc->event_lock);
1917 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1918 cookie, ver, event);
1919 if (event) {
1920 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001921 if (!event_work) {
1922 dout("ERROR: could not allocate event_work\n");
1923 goto done_err;
1924 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001925 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001926 event_work->event = event;
1927 event_work->ver = ver;
1928 event_work->notify_id = notify_id;
1929 event_work->opcode = opcode;
1930 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1931 dout("WARNING: failed to queue notify event work\n");
1932 goto done_err;
1933 }
1934 }
1935
1936 return;
1937
1938done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001939 ceph_osdc_put_event(event);
1940 return;
1941
1942bad:
1943 pr_err("osdc handle_watch_notify corrupt msg\n");
1944 return;
1945}
1946
Alex Elder70636772013-03-04 18:29:06 -06001947static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1948 struct ceph_osd_data *osd_data)
1949{
Alex Elder23c08a9cb2013-04-03 01:28:58 -05001950 u64 length = ceph_osd_data_length(osd_data);
1951
Alex Elder70636772013-03-04 18:29:06 -06001952 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder23c08a9cb2013-04-03 01:28:58 -05001953 BUG_ON(length > (u64) SIZE_MAX);
1954 if (length)
Alex Elder70636772013-03-04 18:29:06 -06001955 ceph_msg_data_set_pages(msg, osd_data->pages,
Alex Elder23c08a9cb2013-04-03 01:28:58 -05001956 length, osd_data->alignment);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001957 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
Alex Elder23c08a9cb2013-04-03 01:28:58 -05001958 BUG_ON(!length);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001959 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
Alex Elder70636772013-03-04 18:29:06 -06001960#ifdef CONFIG_BLOCK
1961 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder23c08a9cb2013-04-03 01:28:58 -05001962 ceph_msg_data_set_bio(msg, osd_data->bio, length);
Alex Elder70636772013-03-04 18:29:06 -06001963#endif
1964 } else {
1965 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1966 }
1967}
1968
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001969/*
Sage Weilf24e9982009-10-06 11:31:10 -07001970 * Register request, send initial attempt.
1971 */
1972int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1973 struct ceph_osd_request *req,
1974 bool nofail)
1975{
Sage Weilc1ea8822009-10-08 16:55:47 -07001976 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001977
Alex Elder70636772013-03-04 18:29:06 -06001978 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001979
Alex Elder70636772013-03-04 18:29:06 -06001980 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1981 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Sage Weilf24e9982009-10-06 11:31:10 -07001982
Sage Weilf24e9982009-10-06 11:31:10 -07001983 down_read(&osdc->map_sem);
1984 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05001985 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05001986 WARN_ON(req->r_sent);
1987 rc = __map_request(osdc, req, 0);
1988 if (rc < 0) {
1989 if (nofail) {
1990 dout("osdc_start_request failed map, "
1991 " will retry %lld\n", req->r_tid);
1992 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001993 }
Alex Elder92451b42013-03-25 18:16:11 -05001994 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001995 }
Alex Elder92451b42013-03-25 18:16:11 -05001996 if (req->r_osd == NULL) {
1997 dout("send_request %p no up osds in pg\n", req);
1998 ceph_monc_request_next_osdmap(&osdc->client->monc);
1999 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05002000 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05002001 }
2002 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03002003out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07002004 mutex_unlock(&osdc->request_mutex);
2005 up_read(&osdc->map_sem);
2006 return rc;
2007}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002008EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002009
2010/*
2011 * wait for a request to complete
2012 */
2013int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
2014 struct ceph_osd_request *req)
2015{
2016 int rc;
2017
2018 rc = wait_for_completion_interruptible(&req->r_completion);
2019 if (rc < 0) {
2020 mutex_lock(&osdc->request_mutex);
2021 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002022 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07002023 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07002024 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08002025 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07002026 return rc;
2027 }
2028
2029 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
2030 return req->r_result;
2031}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002032EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07002033
2034/*
2035 * sync - wait for all in-flight requests to flush. avoid starvation.
2036 */
2037void ceph_osdc_sync(struct ceph_osd_client *osdc)
2038{
2039 struct ceph_osd_request *req;
2040 u64 last_tid, next_tid = 0;
2041
2042 mutex_lock(&osdc->request_mutex);
2043 last_tid = osdc->last_tid;
2044 while (1) {
2045 req = __lookup_request_ge(osdc, next_tid);
2046 if (!req)
2047 break;
2048 if (req->r_tid > last_tid)
2049 break;
2050
2051 next_tid = req->r_tid + 1;
2052 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
2053 continue;
2054
2055 ceph_osdc_get_request(req);
2056 mutex_unlock(&osdc->request_mutex);
2057 dout("sync waiting on tid %llu (last is %llu)\n",
2058 req->r_tid, last_tid);
2059 wait_for_completion(&req->r_safe_completion);
2060 mutex_lock(&osdc->request_mutex);
2061 ceph_osdc_put_request(req);
2062 }
2063 mutex_unlock(&osdc->request_mutex);
2064 dout("sync done (thru tid %llu)\n", last_tid);
2065}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002066EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002067
2068/*
2069 * init, shutdown
2070 */
2071int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2072{
2073 int err;
2074
2075 dout("init\n");
2076 osdc->client = client;
2077 osdc->osdmap = NULL;
2078 init_rwsem(&osdc->map_sem);
2079 init_completion(&osdc->map_waiters);
2080 osdc->last_requested_map = 0;
2081 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002082 osdc->last_tid = 0;
2083 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002084 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002085 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002086 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002087 INIT_LIST_HEAD(&osdc->req_unsent);
2088 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002089 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002090 osdc->num_requests = 0;
2091 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002092 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002093 spin_lock_init(&osdc->event_lock);
2094 osdc->event_tree = RB_ROOT;
2095 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002096
2097 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002098 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002099
Sage Weil5f44f142009-11-18 14:52:18 -08002100 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002101 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2102 sizeof(struct ceph_osd_request));
2103 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002104 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002105
Sage Weild50b4092012-07-09 14:22:34 -07002106 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2107 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002108 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002109 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002110 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002111 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002112 OSD_OPREPLY_FRONT_LEN, 10, true,
2113 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002114 if (err < 0)
2115 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002116
2117 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2118 if (IS_ERR(osdc->notify_wq)) {
2119 err = PTR_ERR(osdc->notify_wq);
2120 osdc->notify_wq = NULL;
2121 goto out_msgpool;
2122 }
Sage Weilf24e9982009-10-06 11:31:10 -07002123 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002124
Sage Weilc16e7862010-03-01 13:02:00 -08002125out_msgpool:
2126 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002127out_mempool:
2128 mempool_destroy(osdc->req_mempool);
2129out:
2130 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002131}
2132
2133void ceph_osdc_stop(struct ceph_osd_client *osdc)
2134{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002135 flush_workqueue(osdc->notify_wq);
2136 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002137 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002138 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002139 if (osdc->osdmap) {
2140 ceph_osdmap_destroy(osdc->osdmap);
2141 osdc->osdmap = NULL;
2142 }
Sage Weilaca420b2011-08-31 14:45:53 -07002143 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002144 mempool_destroy(osdc->req_mempool);
2145 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002146 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002147}
2148
2149/*
2150 * Read some contiguous pages. If we cross a stripe boundary, shorten
2151 * *plen. Return number of bytes read, or error.
2152 */
2153int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2154 struct ceph_vino vino, struct ceph_file_layout *layout,
2155 u64 off, u64 *plen,
2156 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002157 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002158{
2159 struct ceph_osd_request *req;
2160 int rc = 0;
2161
2162 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2163 vino.snap, off, *plen);
Alex Elder79528732013-04-03 21:32:51 -05002164 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002165 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002166 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002167 false);
Sage Weil68162822012-09-24 21:01:02 -07002168 if (IS_ERR(req))
2169 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002170
2171 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002172
Alex Elder43bfe5d2013-04-03 01:28:57 -05002173 ceph_osd_data_pages_init(&req->r_data_in, pages, *plen, page_align,
2174 false, false);
Sage Weilf24e9982009-10-06 11:31:10 -07002175
Alex Eldere0c59482013-03-07 15:38:25 -06002176 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
Alex Elder43bfe5d2013-04-03 01:28:57 -05002177 off, *plen, *plen, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002178
Alex Elder79528732013-04-03 21:32:51 -05002179 ceph_osdc_build_request(req, off, NULL, vino.snap, NULL);
Alex Elder02ee07d2013-03-14 14:09:06 -05002180
Sage Weilf24e9982009-10-06 11:31:10 -07002181 rc = ceph_osdc_start_request(osdc, req, false);
2182 if (!rc)
2183 rc = ceph_osdc_wait_request(osdc, req);
2184
2185 ceph_osdc_put_request(req);
2186 dout("readpages result %d\n", rc);
2187 return rc;
2188}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002189EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002190
2191/*
2192 * do a synchronous write on N pages
2193 */
2194int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2195 struct ceph_file_layout *layout,
2196 struct ceph_snap_context *snapc,
2197 u64 off, u64 len,
2198 u32 truncate_seq, u64 truncate_size,
2199 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002200 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002201{
2202 struct ceph_osd_request *req;
2203 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002204 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002205
Alex Elderacead002013-03-14 14:09:05 -05002206 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
Alex Elder79528732013-04-03 21:32:51 -05002207 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1,
Sage Weilf24e9982009-10-06 11:31:10 -07002208 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002209 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002210 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002211 true);
Sage Weil68162822012-09-24 21:01:02 -07002212 if (IS_ERR(req))
2213 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002214
2215 /* it may be a short write due to an object boundary */
Alex Elder43bfe5d2013-04-03 01:28:57 -05002216 ceph_osd_data_pages_init(&req->r_data_out, pages, len, page_align,
2217 false, false);
2218 dout("writepages %llu~%llu (%llu bytes)\n", off, len, len);
Sage Weilf24e9982009-10-06 11:31:10 -07002219
Alex Elder79528732013-04-03 21:32:51 -05002220 ceph_osdc_build_request(req, off, snapc, CEPH_NOSNAP, mtime);
Alex Elder02ee07d2013-03-14 14:09:06 -05002221
Alex Elder87f979d2013-02-15 11:42:29 -06002222 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002223 if (!rc)
2224 rc = ceph_osdc_wait_request(osdc, req);
2225
2226 ceph_osdc_put_request(req);
2227 if (rc == 0)
2228 rc = len;
2229 dout("writepages result %d\n", rc);
2230 return rc;
2231}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002232EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002233
2234/*
2235 * handle incoming message
2236 */
2237static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2238{
2239 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002240 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002241 int type = le16_to_cpu(msg->hdr.type);
2242
2243 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002244 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002245 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002246
2247 switch (type) {
2248 case CEPH_MSG_OSD_MAP:
2249 ceph_osdc_handle_map(osdc, msg);
2250 break;
2251 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002252 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002253 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002254 case CEPH_MSG_WATCH_NOTIFY:
2255 handle_watch_notify(osdc, msg);
2256 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002257
2258 default:
2259 pr_err("received unknown message type %d %s\n", type,
2260 ceph_msg_type_name(type));
2261 }
Sage Weil4a32f932010-06-13 10:27:53 -07002262out:
Sage Weilf24e9982009-10-06 11:31:10 -07002263 ceph_msg_put(msg);
2264}
2265
Sage Weil5b3a4db2010-02-19 21:43:23 -08002266/*
Sage Weil21b667f2010-03-04 10:22:59 -08002267 * lookup and return message for incoming reply. set up reply message
2268 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002269 */
2270static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002271 struct ceph_msg_header *hdr,
2272 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002273{
2274 struct ceph_osd *osd = con->private;
2275 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002276 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002277 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002278 int front = le32_to_cpu(hdr->front_len);
2279 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002280 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002281
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002282 tid = le64_to_cpu(hdr->tid);
2283 mutex_lock(&osdc->request_mutex);
2284 req = __lookup_request(osdc, tid);
2285 if (!req) {
2286 *skip = 1;
2287 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002288 dout("get_reply unknown tid %llu from osd%d\n", tid,
2289 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002290 goto out;
2291 }
Sage Weilc16e7862010-03-01 13:02:00 -08002292
Alex Elderace6d3a2013-04-01 16:12:14 -05002293 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002294 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002295 req->r_reply, req->r_reply->con);
2296 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002297
Sage Weilc16e7862010-03-01 13:02:00 -08002298 if (front > req->r_reply->front.iov_len) {
2299 pr_warning("get_reply front %d > preallocated %d\n",
2300 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002301 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002302 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002303 goto out;
2304 ceph_msg_put(req->r_reply);
2305 req->r_reply = m;
2306 }
2307 m = ceph_msg_get(req->r_reply);
2308
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002309 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002310 struct ceph_osd_data *osd_data = &req->r_data_in;
2311
2312 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002313 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002314 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002315
Alex Eldere0c59482013-03-07 15:38:25 -06002316 pr_warning("tid %lld reply has %d bytes "
2317 "we had only %llu bytes ready\n",
2318 tid, data_len, osd_data->length);
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002319 *skip = 1;
2320 ceph_msg_put(m);
2321 m = NULL;
2322 goto out;
2323 }
Alex Elder2ac2b7a62013-02-14 12:16:43 -06002324 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002325 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002326 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002327 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002328
2329out:
2330 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002331 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002332
2333}
2334
2335static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2336 struct ceph_msg_header *hdr,
2337 int *skip)
2338{
2339 struct ceph_osd *osd = con->private;
2340 int type = le16_to_cpu(hdr->type);
2341 int front = le32_to_cpu(hdr->front_len);
2342
Alex Elder1c20f2d2012-06-04 14:43:32 -05002343 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002344 switch (type) {
2345 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002346 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002347 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002348 case CEPH_MSG_OSD_OPREPLY:
2349 return get_reply(con, hdr, skip);
2350 default:
2351 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2352 osd->o_osd);
2353 *skip = 1;
2354 return NULL;
2355 }
Sage Weilf24e9982009-10-06 11:31:10 -07002356}
2357
2358/*
2359 * Wrappers to refcount containing ceph_osd struct
2360 */
2361static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2362{
2363 struct ceph_osd *osd = con->private;
2364 if (get_osd(osd))
2365 return con;
2366 return NULL;
2367}
2368
2369static void put_osd_con(struct ceph_connection *con)
2370{
2371 struct ceph_osd *osd = con->private;
2372 put_osd(osd);
2373}
2374
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002375/*
2376 * authentication
2377 */
Alex Eldera3530df2012-05-16 15:16:39 -05002378/*
2379 * Note: returned pointer is the address of a structure that's
2380 * managed separately. Caller must *not* attempt to free it.
2381 */
2382static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002383 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002384{
2385 struct ceph_osd *o = con->private;
2386 struct ceph_osd_client *osdc = o->o_osdc;
2387 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002388 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002389
Alex Elder74f18692012-05-16 15:16:39 -05002390 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002391 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002392 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002393 }
Sage Weil27859f92013-03-25 10:26:14 -07002394 if (!auth->authorizer) {
2395 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2396 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002397 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002398 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002399 } else {
2400 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002401 auth);
2402 if (ret)
2403 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002404 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002405 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002406
Alex Eldera3530df2012-05-16 15:16:39 -05002407 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002408}
2409
2410
2411static int verify_authorizer_reply(struct ceph_connection *con, int len)
2412{
2413 struct ceph_osd *o = con->private;
2414 struct ceph_osd_client *osdc = o->o_osdc;
2415 struct ceph_auth_client *ac = osdc->client->monc.auth;
2416
Sage Weil27859f92013-03-25 10:26:14 -07002417 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002418}
2419
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002420static int invalidate_authorizer(struct ceph_connection *con)
2421{
2422 struct ceph_osd *o = con->private;
2423 struct ceph_osd_client *osdc = o->o_osdc;
2424 struct ceph_auth_client *ac = osdc->client->monc.auth;
2425
Sage Weil27859f92013-03-25 10:26:14 -07002426 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002427 return ceph_monc_validate_auth(&osdc->client->monc);
2428}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002429
Tobias Klauser9e327892010-05-20 10:40:19 +02002430static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002431 .get = get_osd_con,
2432 .put = put_osd_con,
2433 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002434 .get_authorizer = get_authorizer,
2435 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002436 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002437 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002438 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002439};