blob: e0887923e5ab7699ba2685f453e045ec156bef1c [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
Sage Weilf24e9982009-10-06 11:31:10 -070082/*
83 * requests
84 */
Sage Weil415e49a2009-12-07 13:37:03 -080085void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -070086{
Alex Eldere0c59482013-03-07 15:38:25 -060087 int num_pages;
Sage Weil415e49a2009-12-07 13:37:03 -080088 struct ceph_osd_request *req = container_of(kref,
89 struct ceph_osd_request,
90 r_kref);
91
92 if (req->r_request)
93 ceph_msg_put(req->r_request);
Alex Elderace6d3a2013-04-01 16:12:14 -050094 if (req->r_reply) {
Alex Elder8921d112012-06-01 14:56:43 -050095 ceph_msg_revoke_incoming(req->r_reply);
Alex Elderab8cb342012-06-04 14:43:32 -050096 ceph_msg_put(req->r_reply);
Alex Elderace6d3a2013-04-01 16:12:14 -050097 }
Alex Elder0fff87e2013-02-14 12:16:43 -060098
99 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600100 req->r_data_in.own_pages) {
101 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
102 (u64)req->r_data_in.length);
103 ceph_release_page_vector(req->r_data_in.pages, num_pages);
104 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600105 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600106 req->r_data_out.own_pages) {
107 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
108 (u64)req->r_data_out.length);
109 ceph_release_page_vector(req->r_data_out.pages, num_pages);
110 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600111
Sage Weil415e49a2009-12-07 13:37:03 -0800112 ceph_put_snap_context(req->r_snapc);
113 if (req->r_mempool)
114 mempool_free(req, req->r_osdc->req_mempool);
115 else
116 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700117}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700118EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700119
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700120struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700121 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800122 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700123 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600124 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700125{
126 struct ceph_osd_request *req;
127 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800128 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700129
Sage Weil1b83bef2013-02-25 16:11:12 -0800130 msg_size = 4 + 4 + 8 + 8 + 4+8;
131 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
132 msg_size += 1 + 8 + 4 + 4; /* pg_t */
133 msg_size += 4 + MAX_OBJ_NAME_SIZE;
134 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
135 msg_size += 8; /* snapid */
136 msg_size += 8; /* snap_seq */
137 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
138 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700139
140 if (use_mempool) {
141 req = mempool_alloc(osdc->req_mempool, gfp_flags);
142 memset(req, 0, sizeof(*req));
143 } else {
144 req = kzalloc(sizeof(*req), gfp_flags);
145 }
146 if (req == NULL)
147 return NULL;
148
149 req->r_osdc = osdc;
150 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700151
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700152 kref_init(&req->r_kref);
153 init_completion(&req->r_completion);
154 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600155 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700156 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700157 INIT_LIST_HEAD(&req->r_linger_item);
158 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700159 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700160 INIT_LIST_HEAD(&req->r_osd_item);
161
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700162 /* create reply message */
163 if (use_mempool)
164 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
165 else
166 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700167 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700168 if (!msg) {
169 ceph_osdc_put_request(req);
170 return NULL;
171 }
172 req->r_reply = msg;
173
Alex Elder0fff87e2013-02-14 12:16:43 -0600174 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
175 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
Sage Weild50b4092012-07-09 14:22:34 -0700176
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700177 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700178 if (use_mempool)
179 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
180 else
Sage Weilb61c2762011-08-09 15:03:46 -0700181 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700182 if (!msg) {
183 ceph_osdc_put_request(req);
184 return NULL;
185 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700186
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700187 memset(msg->front.iov_base, 0, msg->front.iov_len);
188
189 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700190
191 return req;
192}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700193EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700194
Alex Eldera8dd0a32013-03-13 20:50:00 -0500195static bool osd_req_opcode_valid(u16 opcode)
196{
197 switch (opcode) {
198 case CEPH_OSD_OP_READ:
199 case CEPH_OSD_OP_STAT:
200 case CEPH_OSD_OP_MAPEXT:
201 case CEPH_OSD_OP_MASKTRUNC:
202 case CEPH_OSD_OP_SPARSE_READ:
203 case CEPH_OSD_OP_NOTIFY:
204 case CEPH_OSD_OP_NOTIFY_ACK:
205 case CEPH_OSD_OP_ASSERT_VER:
206 case CEPH_OSD_OP_WRITE:
207 case CEPH_OSD_OP_WRITEFULL:
208 case CEPH_OSD_OP_TRUNCATE:
209 case CEPH_OSD_OP_ZERO:
210 case CEPH_OSD_OP_DELETE:
211 case CEPH_OSD_OP_APPEND:
212 case CEPH_OSD_OP_STARTSYNC:
213 case CEPH_OSD_OP_SETTRUNC:
214 case CEPH_OSD_OP_TRIMTRUNC:
215 case CEPH_OSD_OP_TMAPUP:
216 case CEPH_OSD_OP_TMAPPUT:
217 case CEPH_OSD_OP_TMAPGET:
218 case CEPH_OSD_OP_CREATE:
219 case CEPH_OSD_OP_ROLLBACK:
220 case CEPH_OSD_OP_WATCH:
221 case CEPH_OSD_OP_OMAPGETKEYS:
222 case CEPH_OSD_OP_OMAPGETVALS:
223 case CEPH_OSD_OP_OMAPGETHEADER:
224 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
225 case CEPH_OSD_OP_OMAPSETVALS:
226 case CEPH_OSD_OP_OMAPSETHEADER:
227 case CEPH_OSD_OP_OMAPCLEAR:
228 case CEPH_OSD_OP_OMAPRMKEYS:
229 case CEPH_OSD_OP_OMAP_CMP:
230 case CEPH_OSD_OP_CLONERANGE:
231 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
232 case CEPH_OSD_OP_SRC_CMPXATTR:
233 case CEPH_OSD_OP_GETXATTR:
234 case CEPH_OSD_OP_GETXATTRS:
235 case CEPH_OSD_OP_CMPXATTR:
236 case CEPH_OSD_OP_SETXATTR:
237 case CEPH_OSD_OP_SETXATTRS:
238 case CEPH_OSD_OP_RESETXATTRS:
239 case CEPH_OSD_OP_RMXATTR:
240 case CEPH_OSD_OP_PULL:
241 case CEPH_OSD_OP_PUSH:
242 case CEPH_OSD_OP_BALANCEREADS:
243 case CEPH_OSD_OP_UNBALANCEREADS:
244 case CEPH_OSD_OP_SCRUB:
245 case CEPH_OSD_OP_SCRUB_RESERVE:
246 case CEPH_OSD_OP_SCRUB_UNRESERVE:
247 case CEPH_OSD_OP_SCRUB_STOP:
248 case CEPH_OSD_OP_SCRUB_MAP:
249 case CEPH_OSD_OP_WRLOCK:
250 case CEPH_OSD_OP_WRUNLOCK:
251 case CEPH_OSD_OP_RDLOCK:
252 case CEPH_OSD_OP_RDUNLOCK:
253 case CEPH_OSD_OP_UPLOCK:
254 case CEPH_OSD_OP_DNLOCK:
255 case CEPH_OSD_OP_CALL:
256 case CEPH_OSD_OP_PGLS:
257 case CEPH_OSD_OP_PGLS_FILTER:
258 return true;
259 default:
260 return false;
261 }
262}
263
Alex Elder33803f32013-03-13 20:50:00 -0500264/*
265 * This is an osd op init function for opcodes that have no data or
266 * other information associated with them. It also serves as a
267 * common init routine for all the other init functions, below.
268 */
269void osd_req_op_init(struct ceph_osd_req_op *op, u16 opcode)
270{
271 BUG_ON(!osd_req_opcode_valid(opcode));
272
273 memset(op, 0, sizeof (*op));
274
275 op->op = opcode;
276}
277
278void osd_req_op_extent_init(struct ceph_osd_req_op *op, u16 opcode,
279 u64 offset, u64 length,
280 u64 truncate_size, u32 truncate_seq)
281{
282 size_t payload_len = 0;
283
284 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
285
286 osd_req_op_init(op, opcode);
287
288 op->extent.offset = offset;
289 op->extent.length = length;
290 op->extent.truncate_size = truncate_size;
291 op->extent.truncate_seq = truncate_seq;
292 if (opcode == CEPH_OSD_OP_WRITE)
293 payload_len += length;
294
295 op->payload_len = payload_len;
296}
297EXPORT_SYMBOL(osd_req_op_extent_init);
298
299void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
300 const char *class, const char *method,
301 const void *request_data, size_t request_data_size)
302{
303 size_t payload_len = 0;
304 size_t size;
305
306 BUG_ON(opcode != CEPH_OSD_OP_CALL);
307
308 osd_req_op_init(op, opcode);
309
310 op->cls.class_name = class;
311 size = strlen(class);
312 BUG_ON(size > (size_t) U8_MAX);
313 op->cls.class_len = size;
314 payload_len += size;
315
316 op->cls.method_name = method;
317 size = strlen(method);
318 BUG_ON(size > (size_t) U8_MAX);
319 op->cls.method_len = size;
320 payload_len += size;
321
322 op->cls.indata = request_data;
323 BUG_ON(request_data_size > (size_t) U32_MAX);
324 op->cls.indata_len = (u32) request_data_size;
325 payload_len += request_data_size;
326
327 op->cls.argc = 0; /* currently unused */
328
329 op->payload_len = payload_len;
330}
331EXPORT_SYMBOL(osd_req_op_cls_init);
332
333void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
334 u64 cookie, u64 version, int flag)
335{
336 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
337
338 osd_req_op_init(op, opcode);
339
340 op->watch.cookie = cookie;
341 /* op->watch.ver = version; */ /* XXX 3847 */
342 op->watch.ver = cpu_to_le64(version);
343 if (opcode == CEPH_OSD_OP_WATCH && flag)
344 op->watch.flag = (u8) 1;
345}
346EXPORT_SYMBOL(osd_req_op_watch_init);
347
Alex Elder175face2013-03-08 13:35:36 -0600348static u64 osd_req_encode_op(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700349 struct ceph_osd_op *dst,
350 struct ceph_osd_req_op *src)
351{
Alex Elder175face2013-03-08 13:35:36 -0600352 u64 out_data_len = 0;
Alex Elder95e072e2013-03-08 13:35:36 -0600353 struct ceph_pagelist *pagelist;
Alex Elder175face2013-03-08 13:35:36 -0600354
Alex Eldera8dd0a32013-03-13 20:50:00 -0500355 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
356 pr_err("unrecognized osd opcode %d\n", src->op);
357
358 return 0;
359 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700360
Alex Elder065a68f2012-04-20 15:49:43 -0500361 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600362 case CEPH_OSD_OP_STAT:
363 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700364 case CEPH_OSD_OP_READ:
365 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600366 if (src->op == CEPH_OSD_OP_WRITE)
367 out_data_len = src->extent.length;
368 dst->extent.offset = cpu_to_le64(src->extent.offset);
369 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700370 dst->extent.truncate_size =
371 cpu_to_le64(src->extent.truncate_size);
372 dst->extent.truncate_seq =
373 cpu_to_le32(src->extent.truncate_seq);
374 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700375 case CEPH_OSD_OP_CALL:
Alex Elder95e072e2013-03-08 13:35:36 -0600376 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
377 BUG_ON(!pagelist);
378 ceph_pagelist_init(pagelist);
379
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700380 dst->cls.class_len = src->cls.class_len;
381 dst->cls.method_len = src->cls.method_len;
382 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600383 ceph_pagelist_append(pagelist, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700384 src->cls.class_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600385 ceph_pagelist_append(pagelist, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700386 src->cls.method_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600387 ceph_pagelist_append(pagelist, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700388 src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600389
390 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
391 req->r_data_out.pagelist = pagelist;
392 out_data_len = pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700393 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700394 case CEPH_OSD_OP_STARTSYNC:
395 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700396 case CEPH_OSD_OP_NOTIFY_ACK:
397 case CEPH_OSD_OP_WATCH:
398 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
399 dst->watch.ver = cpu_to_le64(src->watch.ver);
400 dst->watch.flag = src->watch.flag;
401 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700402 default:
Alex Elder4c464592013-02-15 11:42:30 -0600403 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600404 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600405 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500406
407 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700408 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500409 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700410 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600411
412 return out_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700413}
414
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700415/*
416 * build new request AND message
417 *
418 */
419void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder175face2013-03-08 13:35:36 -0600420 u64 off, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700421 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600422 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600423 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700424{
425 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700426 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700427 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800428 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700429 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600430 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700431 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700432
Sage Weil1b83bef2013-02-25 16:11:12 -0800433 req->r_num_ops = num_ops;
434 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700435 req->r_snapc = ceph_get_snap_context(snapc);
436
Sage Weil1b83bef2013-02-25 16:11:12 -0800437 /* encode request */
438 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700439
Sage Weil1b83bef2013-02-25 16:11:12 -0800440 p = msg->front.iov_base;
441 ceph_encode_32(&p, 1); /* client_inc is always 1 */
442 req->r_request_osdmap_epoch = p;
443 p += 4;
444 req->r_request_flags = p;
445 p += 4;
446 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
447 ceph_encode_timespec(p, mtime);
448 p += sizeof(struct ceph_timespec);
449 req->r_request_reassert_version = p;
450 p += sizeof(struct ceph_eversion); /* will get filled in */
451
452 /* oloc */
453 ceph_encode_8(&p, 4);
454 ceph_encode_8(&p, 4);
455 ceph_encode_32(&p, 8 + 4 + 4);
456 req->r_request_pool = p;
457 p += 8;
458 ceph_encode_32(&p, -1); /* preferred */
459 ceph_encode_32(&p, 0); /* key len */
460
461 ceph_encode_8(&p, 1);
462 req->r_request_pgid = p;
463 p += 8 + 4;
464 ceph_encode_32(&p, -1); /* preferred */
465
466 /* oid */
467 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600468 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800469 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600470 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700471
Alex Elder175face2013-03-08 13:35:36 -0600472 /* ops--can imply data */
Sage Weil1b83bef2013-02-25 16:11:12 -0800473 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700474 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800475 req->r_request_ops = p;
Alex Elder175face2013-03-08 13:35:36 -0600476 data_len = 0;
Sage Weil1b83bef2013-02-25 16:11:12 -0800477 for (i = 0; i < num_ops; i++, src_op++) {
Alex Elder175face2013-03-08 13:35:36 -0600478 data_len += osd_req_encode_op(req, p, src_op);
Sage Weil1b83bef2013-02-25 16:11:12 -0800479 p += sizeof(struct ceph_osd_op);
480 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700481
Sage Weil1b83bef2013-02-25 16:11:12 -0800482 /* snaps */
483 ceph_encode_64(&p, req->r_snapid);
484 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
485 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
486 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700487 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800488 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700489 }
490 }
491
Sage Weil1b83bef2013-02-25 16:11:12 -0800492 req->r_request_attempts = p;
493 p += 4;
494
Alex Elder175face2013-03-08 13:35:36 -0600495 /* data */
Alex Elder0baa1bd2013-03-29 14:28:03 -0500496 if (flags & CEPH_OSD_FLAG_WRITE) {
497 u16 data_off;
498
499 /*
500 * The header "data_off" is a hint to the receiver
501 * allowing it to align received data into its
502 * buffers such that there's no need to re-copy
503 * it before writing it to disk (direct I/O).
504 */
505 data_off = (u16) (off & 0xffff);
506 req->r_request->hdr.data_off = cpu_to_le16(data_off);
507 }
Alex Elderf44246e2013-02-14 12:16:43 -0600508 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800509
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700510 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
511 msg_size = p - msg->front.iov_base;
512 msg->front.iov_len = msg_size;
513 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800514
515 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
516 num_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700517 return;
518}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700519EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700520
Sage Weilf24e9982009-10-06 11:31:10 -0700521/*
522 * build new request AND message, calculate layout, and adjust file
523 * extent as needed.
524 *
525 * if the file was recently truncated, we include information about its
526 * old and new size so that the object can be updated appropriately. (we
527 * avoid synchronously deleting truncated objects because it's slow.)
528 *
529 * if @do_sync, include a 'startsync' command so that the osd will flush
530 * data quickly.
531 */
532struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
533 struct ceph_file_layout *layout,
534 struct ceph_vino vino,
535 u64 off, u64 *plen,
536 int opcode, int flags,
537 struct ceph_snap_context *snapc,
538 int do_sync,
539 u32 truncate_seq,
540 u64 truncate_size,
541 struct timespec *mtime,
Alex Elder153e5162013-03-01 18:00:15 -0600542 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700543{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600544 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700545 struct ceph_osd_request *req;
Alex Elderd18d1e22013-03-13 20:50:01 -0500546 unsigned int num_op = do_sync ? 2 : 1;
Alex Elder75d1c942013-03-13 20:50:00 -0500547 u64 objnum = 0;
548 u64 objoff = 0;
549 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500550 u32 object_size;
551 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700552 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700553
Alex Elderd18d1e22013-03-13 20:50:01 -0500554 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700555
Alex Elderae7ca4a32012-11-13 21:11:15 -0600556 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
557 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700558 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700559 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600560 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700561
562 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500563 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600564 if (r < 0) {
565 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700566 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600567 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500568
Alex Elderd18d1e22013-03-13 20:50:01 -0500569 object_size = le32_to_cpu(layout->fl_object_size);
570 object_base = off - objoff;
571 if (truncate_size <= object_base) {
572 truncate_size = 0;
573 } else {
574 truncate_size -= object_base;
575 if (truncate_size > object_size)
576 truncate_size = object_size;
Alex Eldera19dadf2013-03-13 20:50:01 -0500577 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500578
Alex Elderb0270322013-03-13 20:50:01 -0500579 osd_req_op_extent_init(&ops[0], opcode, objoff, objlen,
580 truncate_size, truncate_seq);
Alex Elderd18d1e22013-03-13 20:50:01 -0500581 if (do_sync)
Alex Elderb0270322013-03-13 20:50:01 -0500582 osd_req_op_init(&ops[1], CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500583
Sage Weilf24e9982009-10-06 11:31:10 -0700584 req->r_file_layout = *layout; /* keep a copy */
585
Alex Elder75d1c942013-03-13 20:50:00 -0500586 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
587 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600588 req->r_oid_len = strlen(req->r_oid);
589
Alex Elder175face2013-03-08 13:35:36 -0600590 ceph_osdc_build_request(req, off, num_op, ops,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600591 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700592
Sage Weilf24e9982009-10-06 11:31:10 -0700593 return req;
594}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700595EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700596
597/*
598 * We keep osd requests in an rbtree, sorted by ->r_tid.
599 */
600static void __insert_request(struct ceph_osd_client *osdc,
601 struct ceph_osd_request *new)
602{
603 struct rb_node **p = &osdc->requests.rb_node;
604 struct rb_node *parent = NULL;
605 struct ceph_osd_request *req = NULL;
606
607 while (*p) {
608 parent = *p;
609 req = rb_entry(parent, struct ceph_osd_request, r_node);
610 if (new->r_tid < req->r_tid)
611 p = &(*p)->rb_left;
612 else if (new->r_tid > req->r_tid)
613 p = &(*p)->rb_right;
614 else
615 BUG();
616 }
617
618 rb_link_node(&new->r_node, parent, p);
619 rb_insert_color(&new->r_node, &osdc->requests);
620}
621
622static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
623 u64 tid)
624{
625 struct ceph_osd_request *req;
626 struct rb_node *n = osdc->requests.rb_node;
627
628 while (n) {
629 req = rb_entry(n, struct ceph_osd_request, r_node);
630 if (tid < req->r_tid)
631 n = n->rb_left;
632 else if (tid > req->r_tid)
633 n = n->rb_right;
634 else
635 return req;
636 }
637 return NULL;
638}
639
640static struct ceph_osd_request *
641__lookup_request_ge(struct ceph_osd_client *osdc,
642 u64 tid)
643{
644 struct ceph_osd_request *req;
645 struct rb_node *n = osdc->requests.rb_node;
646
647 while (n) {
648 req = rb_entry(n, struct ceph_osd_request, r_node);
649 if (tid < req->r_tid) {
650 if (!n->rb_left)
651 return req;
652 n = n->rb_left;
653 } else if (tid > req->r_tid) {
654 n = n->rb_right;
655 } else {
656 return req;
657 }
658 }
659 return NULL;
660}
661
Sage Weil6f6c7002011-01-17 20:34:08 -0800662/*
663 * Resubmit requests pending on the given osd.
664 */
665static void __kick_osd_requests(struct ceph_osd_client *osdc,
666 struct ceph_osd *osd)
667{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700668 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500669 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800670 int err;
671
672 dout("__kick_osd_requests osd%d\n", osd->o_osd);
673 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600674 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800675 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500676 /*
677 * Build up a list of requests to resend by traversing the
678 * osd's list of requests. Requests for a given object are
679 * sent in tid order, and that is also the order they're
680 * kept on this list. Therefore all requests that are in
681 * flight will be found first, followed by all requests that
682 * have not yet been sent. And to resend requests while
683 * preserving this order we will want to put any sent
684 * requests back on the front of the osd client's unsent
685 * list.
686 *
687 * So we build a separate ordered list of already-sent
688 * requests for the affected osd and splice it onto the
689 * front of the osd client's unsent list. Once we've seen a
690 * request that has not yet been sent we're done. Those
691 * requests are already sitting right where they belong.
692 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800693 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500694 if (!req->r_sent)
695 break;
696 list_move_tail(&req->r_req_lru_item, &resend);
697 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800698 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700699 if (!req->r_linger)
700 req->r_flags |= CEPH_OSD_FLAG_RETRY;
701 }
Alex Eldere02493c2013-03-25 18:16:11 -0500702 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700703
Alex Eldere02493c2013-03-25 18:16:11 -0500704 /*
705 * Linger requests are re-registered before sending, which
706 * sets up a new tid for each. We add them to the unsent
707 * list at the end to keep things in tid order.
708 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700709 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
710 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700711 /*
712 * reregister request prior to unregistering linger so
713 * that r_osd is preserved.
714 */
715 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700716 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500717 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500718 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700719 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700720 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
721 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800722 }
723}
724
Sage Weilf24e9982009-10-06 11:31:10 -0700725/*
Sage Weil81b024e2009-10-09 10:29:18 -0700726 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700727 */
728static void osd_reset(struct ceph_connection *con)
729{
730 struct ceph_osd *osd = con->private;
731 struct ceph_osd_client *osdc;
732
733 if (!osd)
734 return;
735 dout("osd_reset osd%d\n", osd->o_osd);
736 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700737 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800738 mutex_lock(&osdc->request_mutex);
739 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600740 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800741 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700742 up_read(&osdc->map_sem);
743}
744
745/*
746 * Track open sessions with osds.
747 */
Alex Eldere10006f2012-05-26 23:26:43 -0500748static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700749{
750 struct ceph_osd *osd;
751
752 osd = kzalloc(sizeof(*osd), GFP_NOFS);
753 if (!osd)
754 return NULL;
755
756 atomic_set(&osd->o_ref, 1);
757 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500758 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600759 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700760 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700761 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800762 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700763 osd->o_incarnation = 1;
764
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700765 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800766
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800767 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700768 return osd;
769}
770
771static struct ceph_osd *get_osd(struct ceph_osd *osd)
772{
773 if (atomic_inc_not_zero(&osd->o_ref)) {
774 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
775 atomic_read(&osd->o_ref));
776 return osd;
777 } else {
778 dout("get_osd %p FAIL\n", osd);
779 return NULL;
780 }
781}
782
783static void put_osd(struct ceph_osd *osd)
784{
785 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
786 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500787 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700788 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
789
Sage Weil27859f92013-03-25 10:26:14 -0700790 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700791 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700792 }
Sage Weilf24e9982009-10-06 11:31:10 -0700793}
794
795/*
796 * remove an osd from our map
797 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800798static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700799{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800800 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700801 BUG_ON(!list_empty(&osd->o_requests));
802 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800803 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700804 ceph_con_close(&osd->o_con);
805 put_osd(osd);
806}
807
Sage Weilaca420b2011-08-31 14:45:53 -0700808static void remove_all_osds(struct ceph_osd_client *osdc)
809{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500810 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700811 mutex_lock(&osdc->request_mutex);
812 while (!RB_EMPTY_ROOT(&osdc->osds)) {
813 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
814 struct ceph_osd, o_node);
815 __remove_osd(osdc, osd);
816 }
817 mutex_unlock(&osdc->request_mutex);
818}
819
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800820static void __move_osd_to_lru(struct ceph_osd_client *osdc,
821 struct ceph_osd *osd)
822{
823 dout("__move_osd_to_lru %p\n", osd);
824 BUG_ON(!list_empty(&osd->o_osd_lru));
825 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700826 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800827}
828
829static void __remove_osd_from_lru(struct ceph_osd *osd)
830{
831 dout("__remove_osd_from_lru %p\n", osd);
832 if (!list_empty(&osd->o_osd_lru))
833 list_del_init(&osd->o_osd_lru);
834}
835
Sage Weilaca420b2011-08-31 14:45:53 -0700836static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800837{
838 struct ceph_osd *osd, *nosd;
839
840 dout("__remove_old_osds %p\n", osdc);
841 mutex_lock(&osdc->request_mutex);
842 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700843 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800844 break;
845 __remove_osd(osdc, osd);
846 }
847 mutex_unlock(&osdc->request_mutex);
848}
849
Sage Weilf24e9982009-10-06 11:31:10 -0700850/*
851 * reset osd connect
852 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800853static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700854{
Alex Elderc3acb182012-12-07 09:57:58 -0600855 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700856
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800857 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700858 if (list_empty(&osd->o_requests) &&
859 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800860 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600861
862 return -ENODEV;
863 }
864
865 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
866 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
867 !ceph_con_opened(&osd->o_con)) {
868 struct ceph_osd_request *req;
869
Sage Weil87b315a2010-03-22 14:51:18 -0700870 dout(" osd addr hasn't changed and connection never opened,"
871 " letting msgr retry");
872 /* touch each r_stamp for handle_timeout()'s benfit */
873 list_for_each_entry(req, &osd->o_requests, r_osd_item)
874 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600875
876 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700877 }
Alex Elderc3acb182012-12-07 09:57:58 -0600878
879 ceph_con_close(&osd->o_con);
880 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
881 osd->o_incarnation++;
882
883 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700884}
885
886static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
887{
888 struct rb_node **p = &osdc->osds.rb_node;
889 struct rb_node *parent = NULL;
890 struct ceph_osd *osd = NULL;
891
Sage Weilaca420b2011-08-31 14:45:53 -0700892 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700893 while (*p) {
894 parent = *p;
895 osd = rb_entry(parent, struct ceph_osd, o_node);
896 if (new->o_osd < osd->o_osd)
897 p = &(*p)->rb_left;
898 else if (new->o_osd > osd->o_osd)
899 p = &(*p)->rb_right;
900 else
901 BUG();
902 }
903
904 rb_link_node(&new->o_node, parent, p);
905 rb_insert_color(&new->o_node, &osdc->osds);
906}
907
908static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
909{
910 struct ceph_osd *osd;
911 struct rb_node *n = osdc->osds.rb_node;
912
913 while (n) {
914 osd = rb_entry(n, struct ceph_osd, o_node);
915 if (o < osd->o_osd)
916 n = n->rb_left;
917 else if (o > osd->o_osd)
918 n = n->rb_right;
919 else
920 return osd;
921 }
922 return NULL;
923}
924
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800925static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
926{
927 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700928 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800929}
930
931static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
932{
933 cancel_delayed_work(&osdc->timeout_work);
934}
Sage Weilf24e9982009-10-06 11:31:10 -0700935
936/*
937 * Register request, assign tid. If this is the first request, set up
938 * the timeout event.
939 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700940static void __register_request(struct ceph_osd_client *osdc,
941 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700942{
Sage Weilf24e9982009-10-06 11:31:10 -0700943 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800944 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700945 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700946 __insert_request(osdc, req);
947 ceph_osdc_get_request(req);
948 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700949 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800950 dout(" first request, scheduling timeout\n");
951 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700952 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700953}
954
Sage Weilf24e9982009-10-06 11:31:10 -0700955/*
956 * called under osdc->request_mutex
957 */
958static void __unregister_request(struct ceph_osd_client *osdc,
959 struct ceph_osd_request *req)
960{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500961 if (RB_EMPTY_NODE(&req->r_node)) {
962 dout("__unregister_request %p tid %lld not registered\n",
963 req, req->r_tid);
964 return;
965 }
966
Sage Weilf24e9982009-10-06 11:31:10 -0700967 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
968 rb_erase(&req->r_node, &osdc->requests);
969 osdc->num_requests--;
970
Sage Weil0ba64782009-10-08 16:57:16 -0700971 if (req->r_osd) {
972 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500973 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700974
975 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700976 if (list_empty(&req->r_osd->o_requests) &&
977 list_empty(&req->r_osd->o_linger_requests)) {
978 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800979 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700980 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700981 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700982 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700983 }
Sage Weilf24e9982009-10-06 11:31:10 -0700984
Alex Elder7d5f2482012-11-29 08:37:03 -0600985 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700986 ceph_osdc_put_request(req);
987
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800988 if (osdc->num_requests == 0) {
989 dout(" no requests, canceling timeout\n");
990 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700991 }
992}
993
994/*
995 * Cancel a previously queued request message
996 */
997static void __cancel_request(struct ceph_osd_request *req)
998{
Sage Weil6bc18872010-09-27 10:18:52 -0700999 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001000 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001001 req->r_sent = 0;
1002 }
1003}
1004
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001005static void __register_linger_request(struct ceph_osd_client *osdc,
1006 struct ceph_osd_request *req)
1007{
1008 dout("__register_linger_request %p\n", req);
1009 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001010 if (req->r_osd)
1011 list_add_tail(&req->r_linger_osd,
1012 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001013}
1014
1015static void __unregister_linger_request(struct ceph_osd_client *osdc,
1016 struct ceph_osd_request *req)
1017{
1018 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001019 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001020 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001021 list_del_init(&req->r_linger_osd);
1022
1023 if (list_empty(&req->r_osd->o_requests) &&
1024 list_empty(&req->r_osd->o_linger_requests)) {
1025 dout("moving osd to %p lru\n", req->r_osd);
1026 __move_osd_to_lru(osdc, req->r_osd);
1027 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001028 if (list_empty(&req->r_osd_item))
1029 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001030 }
1031}
1032
1033void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1034 struct ceph_osd_request *req)
1035{
1036 mutex_lock(&osdc->request_mutex);
1037 if (req->r_linger) {
1038 __unregister_linger_request(osdc, req);
1039 ceph_osdc_put_request(req);
1040 }
1041 mutex_unlock(&osdc->request_mutex);
1042}
1043EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1044
1045void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1046 struct ceph_osd_request *req)
1047{
1048 if (!req->r_linger) {
1049 dout("set_request_linger %p\n", req);
1050 req->r_linger = 1;
1051 /*
1052 * caller is now responsible for calling
1053 * unregister_linger_request
1054 */
1055 ceph_osdc_get_request(req);
1056 }
1057}
1058EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1059
Sage Weilf24e9982009-10-06 11:31:10 -07001060/*
1061 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1062 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001063 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001064 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001065 *
1066 * Return 0 if unchanged, 1 if changed, or negative on error.
1067 *
1068 * Caller should hold map_sem for read and request_mutex.
1069 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001070static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001071 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001072{
Sage Weil5b191d92013-02-23 10:38:16 -08001073 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001074 int acting[CEPH_PG_MAX_SIZE];
1075 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001076 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001077
Sage Weil6f6c7002011-01-17 20:34:08 -08001078 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001079 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1080 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001081 if (err) {
1082 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001083 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001084 }
Sage Weil7740a422010-01-08 15:58:25 -08001085 req->r_pgid = pgid;
1086
Sage Weild85b7052010-05-10 10:24:48 -07001087 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1088 if (err > 0) {
1089 o = acting[0];
1090 num = err;
1091 }
Sage Weilf24e9982009-10-06 11:31:10 -07001092
Sage Weil38d64532011-10-14 13:33:55 -07001093 if ((!force_resend &&
1094 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001095 req->r_sent >= req->r_osd->o_incarnation &&
1096 req->r_num_pg_osds == num &&
1097 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001098 (req->r_osd == NULL && o == -1))
1099 return 0; /* no change */
1100
Sage Weil5b191d92013-02-23 10:38:16 -08001101 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1102 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001103 req->r_osd ? req->r_osd->o_osd : -1);
1104
Sage Weild85b7052010-05-10 10:24:48 -07001105 /* record full pg acting set */
1106 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1107 req->r_num_pg_osds = num;
1108
Sage Weilf24e9982009-10-06 11:31:10 -07001109 if (req->r_osd) {
1110 __cancel_request(req);
1111 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001112 req->r_osd = NULL;
1113 }
1114
1115 req->r_osd = __lookup_osd(osdc, o);
1116 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001117 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001118 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001119 if (!req->r_osd) {
1120 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001121 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001122 }
Sage Weilf24e9982009-10-06 11:31:10 -07001123
Sage Weil6f6c7002011-01-17 20:34:08 -08001124 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001125 __insert_osd(osdc, req->r_osd);
1126
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001127 ceph_con_open(&req->r_osd->o_con,
1128 CEPH_ENTITY_TYPE_OSD, o,
1129 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001130 }
1131
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001132 if (req->r_osd) {
1133 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001134 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1135 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001136 } else {
Alex Elderad885922013-03-25 18:16:11 -05001137 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001138 }
Sage Weild85b7052010-05-10 10:24:48 -07001139 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001140
1141out:
Sage Weilf24e9982009-10-06 11:31:10 -07001142 return err;
1143}
1144
1145/*
1146 * caller should hold map_sem (for read) and request_mutex
1147 */
Sage Weil56e925b2012-01-03 12:34:34 -08001148static void __send_request(struct ceph_osd_client *osdc,
1149 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001150{
Sage Weil1b83bef2013-02-25 16:11:12 -08001151 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001152
Sage Weil1b83bef2013-02-25 16:11:12 -08001153 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1154 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1155 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001156
Sage Weil1b83bef2013-02-25 16:11:12 -08001157 /* fill in message content that changes each time we send it */
1158 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1159 put_unaligned_le32(req->r_flags, req->r_request_flags);
1160 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1161 p = req->r_request_pgid;
1162 ceph_encode_64(&p, req->r_pgid.pool);
1163 ceph_encode_32(&p, req->r_pgid.seed);
1164 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1165 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1166 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001167
Sage Weil3dd72fc2010-03-22 14:42:30 -07001168 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001169 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001170
1171 ceph_msg_get(req->r_request); /* send consumes a ref */
1172 ceph_con_send(&req->r_osd->o_con, req->r_request);
1173 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001174}
1175
1176/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001177 * Send any requests in the queue (req_unsent).
1178 */
Alex Elderf9d25192013-02-15 11:42:29 -06001179static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001180{
1181 struct ceph_osd_request *req, *tmp;
1182
Alex Elderf9d25192013-02-15 11:42:29 -06001183 dout("__send_queued\n");
1184 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001185 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001186}
1187
1188/*
Sage Weilf24e9982009-10-06 11:31:10 -07001189 * Timeout callback, called every N seconds when 1 or more osd
1190 * requests has been active for more than N seconds. When this
1191 * happens, we ping all OSDs with requests who have timed out to
1192 * ensure any communications channel reset is detected. Reset the
1193 * request timeouts another N seconds in the future as we go.
1194 * Reschedule the timeout event another N seconds in future (unless
1195 * there are no open requests).
1196 */
1197static void handle_timeout(struct work_struct *work)
1198{
1199 struct ceph_osd_client *osdc =
1200 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001201 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001202 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001203 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001204 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001205 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001206 dout("timeout\n");
1207 down_read(&osdc->map_sem);
1208
1209 ceph_monc_request_next_osdmap(&osdc->client->monc);
1210
1211 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001212
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001213 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001214 * ping osds that are a bit slow. this ensures that if there
1215 * is a break in the TCP connection we will notice, and reopen
1216 * a connection with that osd (from the fault callback).
1217 */
1218 INIT_LIST_HEAD(&slow_osds);
1219 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001220 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001221 break;
1222
1223 osd = req->r_osd;
1224 BUG_ON(!osd);
1225 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001226 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001227 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1228 }
1229 while (!list_empty(&slow_osds)) {
1230 osd = list_entry(slow_osds.next, struct ceph_osd,
1231 o_keepalive_item);
1232 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001233 ceph_con_keepalive(&osd->o_con);
1234 }
1235
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001236 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001237 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001238 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001239 up_read(&osdc->map_sem);
1240}
1241
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001242static void handle_osds_timeout(struct work_struct *work)
1243{
1244 struct ceph_osd_client *osdc =
1245 container_of(work, struct ceph_osd_client,
1246 osds_timeout_work.work);
1247 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001248 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001249
1250 dout("osds timeout\n");
1251 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001252 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001253 up_read(&osdc->map_sem);
1254
1255 schedule_delayed_work(&osdc->osds_timeout_work,
1256 round_jiffies_relative(delay));
1257}
1258
Sage Weil25845472011-06-03 09:37:09 -07001259static void complete_request(struct ceph_osd_request *req)
1260{
1261 if (req->r_safe_callback)
1262 req->r_safe_callback(req, NULL);
1263 complete_all(&req->r_safe_completion); /* fsync waiter */
1264}
1265
Sage Weilf24e9982009-10-06 11:31:10 -07001266/*
1267 * handle osd op reply. either call the callback if it is specified,
1268 * or do the completion to wake up the waiting thread.
1269 */
Sage Weil350b1c32009-12-22 10:45:45 -08001270static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1271 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001272{
Sage Weil1b83bef2013-02-25 16:11:12 -08001273 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001274 struct ceph_osd_request *req;
1275 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001276 int object_len;
1277 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001278 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001279 s32 retry_attempt;
1280 struct ceph_pg pg;
1281 int err;
1282 u32 reassert_epoch;
1283 u64 reassert_version;
1284 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001285 int already_completed;
Sage Weil1b83bef2013-02-25 16:11:12 -08001286 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001287
Sage Weil6df058c2009-12-22 11:24:33 -08001288 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001289 dout("handle_reply %p tid %llu\n", msg, tid);
1290
1291 p = msg->front.iov_base;
1292 end = p + msg->front.iov_len;
1293
1294 ceph_decode_need(&p, end, 4, bad);
1295 object_len = ceph_decode_32(&p);
1296 ceph_decode_need(&p, end, object_len, bad);
1297 p += object_len;
1298
Alex Elderef4859d2013-04-01 18:58:26 -05001299 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001300 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001301 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001302
1303 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1304 flags = ceph_decode_64(&p);
1305 result = ceph_decode_32(&p);
1306 reassert_epoch = ceph_decode_32(&p);
1307 reassert_version = ceph_decode_64(&p);
1308 osdmap_epoch = ceph_decode_32(&p);
1309
Sage Weilf24e9982009-10-06 11:31:10 -07001310 /* lookup */
1311 mutex_lock(&osdc->request_mutex);
1312 req = __lookup_request(osdc, tid);
1313 if (req == NULL) {
1314 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001315 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001316 }
1317 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001318
1319 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1320 req, result);
1321
1322 ceph_decode_need(&p, end, 4, bad);
1323 numops = ceph_decode_32(&p);
1324 if (numops > CEPH_OSD_MAX_OP)
1325 goto bad_put;
1326 if (numops != req->r_num_ops)
1327 goto bad_put;
1328 payload_len = 0;
1329 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1330 for (i = 0; i < numops; i++) {
1331 struct ceph_osd_op *op = p;
1332 int len;
1333
1334 len = le32_to_cpu(op->payload_len);
1335 req->r_reply_op_len[i] = len;
1336 dout(" op %d has %d bytes\n", i, len);
1337 payload_len += len;
1338 p += sizeof(*op);
1339 }
1340 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1341 pr_warning("sum of op payload lens %d != data_len %d",
1342 payload_len, le32_to_cpu(msg->hdr.data_len));
1343 goto bad_put;
1344 }
1345
1346 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1347 retry_attempt = ceph_decode_32(&p);
1348 for (i = 0; i < numops; i++)
1349 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001350
Sage Weilf24e9982009-10-06 11:31:10 -07001351 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001352 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001353
Sage Weil1b83bef2013-02-25 16:11:12 -08001354 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001355 bytes = le32_to_cpu(msg->hdr.data_len);
1356 dout("handle_reply result %d bytes %d\n", req->r_result,
1357 bytes);
1358 if (req->r_result == 0)
1359 req->r_result = bytes;
1360
1361 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001362 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1363 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001364
1365 req->r_got_reply = 1;
1366 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1367 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001368 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001369 goto done;
1370 }
1371
1372 dout("handle_reply tid %llu flags %d\n", tid, flags);
1373
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001374 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1375 __register_linger_request(osdc, req);
1376
Sage Weilf24e9982009-10-06 11:31:10 -07001377 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001378 if (result < 0 ||
1379 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001380 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1381 __unregister_request(osdc, req);
1382
Alex Elder0d5af162013-02-27 10:26:25 -06001383 already_completed = req->r_completed;
1384 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001385 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001386 if (already_completed)
1387 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001388
1389 if (req->r_callback)
1390 req->r_callback(req, msg);
1391 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001392 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001393
Sage Weil25845472011-06-03 09:37:09 -07001394 if (flags & CEPH_OSD_FLAG_ONDISK)
1395 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001396
1397done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001398 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001399 ceph_osdc_put_request(req);
1400 return;
1401
Sage Weil1b83bef2013-02-25 16:11:12 -08001402bad_put:
1403 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001404bad_mutex:
1405 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001406bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001407 pr_err("corrupt osd_op_reply got %d %d\n",
1408 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001409 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001410}
1411
Sage Weil6f6c7002011-01-17 20:34:08 -08001412static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001413{
Sage Weilf24e9982009-10-06 11:31:10 -07001414 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001415
Sage Weil6f6c7002011-01-17 20:34:08 -08001416 for (p = rb_first(&osdc->osds); p; p = n) {
1417 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001418
Sage Weil6f6c7002011-01-17 20:34:08 -08001419 n = rb_next(p);
1420 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1421 memcmp(&osd->o_con.peer_addr,
1422 ceph_osd_addr(osdc->osdmap,
1423 osd->o_osd),
1424 sizeof(struct ceph_entity_addr)) != 0)
1425 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001426 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001427}
1428
1429/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001430 * Requeue requests whose mapping to an OSD has changed. If requests map to
1431 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001432 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001433 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001434 */
Sage Weil38d64532011-10-14 13:33:55 -07001435static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001436{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001437 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001438 struct rb_node *p;
1439 int needmap = 0;
1440 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001441
Sage Weil38d64532011-10-14 13:33:55 -07001442 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001443 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001444 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001445 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001446 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001447
1448 /*
1449 * For linger requests that have not yet been
1450 * registered, move them to the linger list; they'll
1451 * be sent to the osd in the loop below. Unregister
1452 * the request before re-registering it as a linger
1453 * request to ensure the __map_request() below
1454 * will decide it needs to be sent.
1455 */
1456 if (req->r_linger && list_empty(&req->r_linger_item)) {
1457 dout("%p tid %llu restart on osd%d\n",
1458 req, req->r_tid,
1459 req->r_osd ? req->r_osd->o_osd : -1);
1460 __unregister_request(osdc, req);
1461 __register_linger_request(osdc, req);
1462 continue;
1463 }
1464
Sage Weil38d64532011-10-14 13:33:55 -07001465 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001466 if (err < 0)
1467 continue; /* error */
1468 if (req->r_osd == NULL) {
1469 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1470 needmap++; /* request a newer map */
1471 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001472 if (!req->r_linger) {
1473 dout("%p tid %llu requeued on osd%d\n", req,
1474 req->r_tid,
1475 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001476 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001477 }
1478 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001479 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001480
1481 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1482 r_linger_item) {
1483 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1484
Sage Weil38d64532011-10-14 13:33:55 -07001485 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001486 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001487 if (err == 0)
1488 continue; /* no change and no osd was specified */
1489 if (err < 0)
1490 continue; /* hrm! */
1491 if (req->r_osd == NULL) {
1492 dout("tid %llu maps to no valid osd\n", req->r_tid);
1493 needmap++; /* request a newer map */
1494 continue;
1495 }
1496
1497 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1498 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001499 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001500 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001501 }
Sage Weilf24e9982009-10-06 11:31:10 -07001502 mutex_unlock(&osdc->request_mutex);
1503
1504 if (needmap) {
1505 dout("%d requests for down osds, need new map\n", needmap);
1506 ceph_monc_request_next_osdmap(&osdc->client->monc);
1507 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001508 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001509}
Sage Weil6f6c7002011-01-17 20:34:08 -08001510
1511
Sage Weilf24e9982009-10-06 11:31:10 -07001512/*
1513 * Process updated osd map.
1514 *
1515 * The message contains any number of incremental and full maps, normally
1516 * indicating some sort of topology change in the cluster. Kick requests
1517 * off to different OSDs as needed.
1518 */
1519void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1520{
1521 void *p, *end, *next;
1522 u32 nr_maps, maplen;
1523 u32 epoch;
1524 struct ceph_osdmap *newmap = NULL, *oldmap;
1525 int err;
1526 struct ceph_fsid fsid;
1527
1528 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1529 p = msg->front.iov_base;
1530 end = p + msg->front.iov_len;
1531
1532 /* verify fsid */
1533 ceph_decode_need(&p, end, sizeof(fsid), bad);
1534 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001535 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1536 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001537
1538 down_write(&osdc->map_sem);
1539
1540 /* incremental maps */
1541 ceph_decode_32_safe(&p, end, nr_maps, bad);
1542 dout(" %d inc maps\n", nr_maps);
1543 while (nr_maps > 0) {
1544 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001545 epoch = ceph_decode_32(&p);
1546 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001547 ceph_decode_need(&p, end, maplen, bad);
1548 next = p + maplen;
1549 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1550 dout("applying incremental map %u len %d\n",
1551 epoch, maplen);
1552 newmap = osdmap_apply_incremental(&p, next,
1553 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001554 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001555 if (IS_ERR(newmap)) {
1556 err = PTR_ERR(newmap);
1557 goto bad;
1558 }
Sage Weil30dc6382009-12-21 14:49:37 -08001559 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001560 if (newmap != osdc->osdmap) {
1561 ceph_osdmap_destroy(osdc->osdmap);
1562 osdc->osdmap = newmap;
1563 }
Sage Weil38d64532011-10-14 13:33:55 -07001564 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001565 } else {
1566 dout("ignoring incremental map %u len %d\n",
1567 epoch, maplen);
1568 }
1569 p = next;
1570 nr_maps--;
1571 }
1572 if (newmap)
1573 goto done;
1574
1575 /* full maps */
1576 ceph_decode_32_safe(&p, end, nr_maps, bad);
1577 dout(" %d full maps\n", nr_maps);
1578 while (nr_maps) {
1579 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001580 epoch = ceph_decode_32(&p);
1581 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001582 ceph_decode_need(&p, end, maplen, bad);
1583 if (nr_maps > 1) {
1584 dout("skipping non-latest full map %u len %d\n",
1585 epoch, maplen);
1586 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1587 dout("skipping full map %u len %d, "
1588 "older than our %u\n", epoch, maplen,
1589 osdc->osdmap->epoch);
1590 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001591 int skipped_map = 0;
1592
Sage Weilf24e9982009-10-06 11:31:10 -07001593 dout("taking full map %u len %d\n", epoch, maplen);
1594 newmap = osdmap_decode(&p, p+maplen);
1595 if (IS_ERR(newmap)) {
1596 err = PTR_ERR(newmap);
1597 goto bad;
1598 }
Sage Weil30dc6382009-12-21 14:49:37 -08001599 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001600 oldmap = osdc->osdmap;
1601 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001602 if (oldmap) {
1603 if (oldmap->epoch + 1 < newmap->epoch)
1604 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001605 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001606 }
1607 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001608 }
1609 p += maplen;
1610 nr_maps--;
1611 }
1612
1613done:
1614 downgrade_write(&osdc->map_sem);
1615 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001616
1617 /*
1618 * subscribe to subsequent osdmap updates if full to ensure
1619 * we find out when we are no longer full and stop returning
1620 * ENOSPC.
1621 */
1622 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1623 ceph_monc_request_next_osdmap(&osdc->client->monc);
1624
Alex Elderf9d25192013-02-15 11:42:29 -06001625 mutex_lock(&osdc->request_mutex);
1626 __send_queued(osdc);
1627 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001628 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001629 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001630 return;
1631
1632bad:
1633 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001634 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001635 up_write(&osdc->map_sem);
1636 return;
1637}
1638
Sage Weilf24e9982009-10-06 11:31:10 -07001639/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001640 * watch/notify callback event infrastructure
1641 *
1642 * These callbacks are used both for watch and notify operations.
1643 */
1644static void __release_event(struct kref *kref)
1645{
1646 struct ceph_osd_event *event =
1647 container_of(kref, struct ceph_osd_event, kref);
1648
1649 dout("__release_event %p\n", event);
1650 kfree(event);
1651}
1652
1653static void get_event(struct ceph_osd_event *event)
1654{
1655 kref_get(&event->kref);
1656}
1657
1658void ceph_osdc_put_event(struct ceph_osd_event *event)
1659{
1660 kref_put(&event->kref, __release_event);
1661}
1662EXPORT_SYMBOL(ceph_osdc_put_event);
1663
1664static void __insert_event(struct ceph_osd_client *osdc,
1665 struct ceph_osd_event *new)
1666{
1667 struct rb_node **p = &osdc->event_tree.rb_node;
1668 struct rb_node *parent = NULL;
1669 struct ceph_osd_event *event = NULL;
1670
1671 while (*p) {
1672 parent = *p;
1673 event = rb_entry(parent, struct ceph_osd_event, node);
1674 if (new->cookie < event->cookie)
1675 p = &(*p)->rb_left;
1676 else if (new->cookie > event->cookie)
1677 p = &(*p)->rb_right;
1678 else
1679 BUG();
1680 }
1681
1682 rb_link_node(&new->node, parent, p);
1683 rb_insert_color(&new->node, &osdc->event_tree);
1684}
1685
1686static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1687 u64 cookie)
1688{
1689 struct rb_node **p = &osdc->event_tree.rb_node;
1690 struct rb_node *parent = NULL;
1691 struct ceph_osd_event *event = NULL;
1692
1693 while (*p) {
1694 parent = *p;
1695 event = rb_entry(parent, struct ceph_osd_event, node);
1696 if (cookie < event->cookie)
1697 p = &(*p)->rb_left;
1698 else if (cookie > event->cookie)
1699 p = &(*p)->rb_right;
1700 else
1701 return event;
1702 }
1703 return NULL;
1704}
1705
1706static void __remove_event(struct ceph_osd_event *event)
1707{
1708 struct ceph_osd_client *osdc = event->osdc;
1709
1710 if (!RB_EMPTY_NODE(&event->node)) {
1711 dout("__remove_event removed %p\n", event);
1712 rb_erase(&event->node, &osdc->event_tree);
1713 ceph_osdc_put_event(event);
1714 } else {
1715 dout("__remove_event didn't remove %p\n", event);
1716 }
1717}
1718
1719int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1720 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001721 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001722{
1723 struct ceph_osd_event *event;
1724
1725 event = kmalloc(sizeof(*event), GFP_NOIO);
1726 if (!event)
1727 return -ENOMEM;
1728
1729 dout("create_event %p\n", event);
1730 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001731 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001732 event->data = data;
1733 event->osdc = osdc;
1734 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001735 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001736 kref_init(&event->kref); /* one ref for us */
1737 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001738
1739 spin_lock(&osdc->event_lock);
1740 event->cookie = ++osdc->event_count;
1741 __insert_event(osdc, event);
1742 spin_unlock(&osdc->event_lock);
1743
1744 *pevent = event;
1745 return 0;
1746}
1747EXPORT_SYMBOL(ceph_osdc_create_event);
1748
1749void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1750{
1751 struct ceph_osd_client *osdc = event->osdc;
1752
1753 dout("cancel_event %p\n", event);
1754 spin_lock(&osdc->event_lock);
1755 __remove_event(event);
1756 spin_unlock(&osdc->event_lock);
1757 ceph_osdc_put_event(event); /* caller's */
1758}
1759EXPORT_SYMBOL(ceph_osdc_cancel_event);
1760
1761
1762static void do_event_work(struct work_struct *work)
1763{
1764 struct ceph_osd_event_work *event_work =
1765 container_of(work, struct ceph_osd_event_work, work);
1766 struct ceph_osd_event *event = event_work->event;
1767 u64 ver = event_work->ver;
1768 u64 notify_id = event_work->notify_id;
1769 u8 opcode = event_work->opcode;
1770
1771 dout("do_event_work completing %p\n", event);
1772 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001773 dout("do_event_work completed %p\n", event);
1774 ceph_osdc_put_event(event);
1775 kfree(event_work);
1776}
1777
1778
1779/*
1780 * Process osd watch notifications
1781 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001782static void handle_watch_notify(struct ceph_osd_client *osdc,
1783 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001784{
1785 void *p, *end;
1786 u8 proto_ver;
1787 u64 cookie, ver, notify_id;
1788 u8 opcode;
1789 struct ceph_osd_event *event;
1790 struct ceph_osd_event_work *event_work;
1791
1792 p = msg->front.iov_base;
1793 end = p + msg->front.iov_len;
1794
1795 ceph_decode_8_safe(&p, end, proto_ver, bad);
1796 ceph_decode_8_safe(&p, end, opcode, bad);
1797 ceph_decode_64_safe(&p, end, cookie, bad);
1798 ceph_decode_64_safe(&p, end, ver, bad);
1799 ceph_decode_64_safe(&p, end, notify_id, bad);
1800
1801 spin_lock(&osdc->event_lock);
1802 event = __find_event(osdc, cookie);
1803 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001804 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001805 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001806 }
1807 spin_unlock(&osdc->event_lock);
1808 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1809 cookie, ver, event);
1810 if (event) {
1811 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001812 if (!event_work) {
1813 dout("ERROR: could not allocate event_work\n");
1814 goto done_err;
1815 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001816 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001817 event_work->event = event;
1818 event_work->ver = ver;
1819 event_work->notify_id = notify_id;
1820 event_work->opcode = opcode;
1821 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1822 dout("WARNING: failed to queue notify event work\n");
1823 goto done_err;
1824 }
1825 }
1826
1827 return;
1828
1829done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001830 ceph_osdc_put_event(event);
1831 return;
1832
1833bad:
1834 pr_err("osdc handle_watch_notify corrupt msg\n");
1835 return;
1836}
1837
Alex Elder70636772013-03-04 18:29:06 -06001838static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1839 struct ceph_osd_data *osd_data)
1840{
1841 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1842 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1843 if (osd_data->length)
1844 ceph_msg_data_set_pages(msg, osd_data->pages,
1845 osd_data->length, osd_data->alignment);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001846 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1847 BUG_ON(!osd_data->pagelist->length);
1848 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
Alex Elder70636772013-03-04 18:29:06 -06001849#ifdef CONFIG_BLOCK
1850 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1851 ceph_msg_data_set_bio(msg, osd_data->bio);
1852#endif
1853 } else {
1854 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1855 }
1856}
1857
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001858/*
Sage Weilf24e9982009-10-06 11:31:10 -07001859 * Register request, send initial attempt.
1860 */
1861int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1862 struct ceph_osd_request *req,
1863 bool nofail)
1864{
Sage Weilc1ea8822009-10-08 16:55:47 -07001865 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001866
Alex Elder70636772013-03-04 18:29:06 -06001867 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001868
Alex Elder70636772013-03-04 18:29:06 -06001869 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1870 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Sage Weilf24e9982009-10-06 11:31:10 -07001871
Sage Weilf24e9982009-10-06 11:31:10 -07001872 down_read(&osdc->map_sem);
1873 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05001874 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05001875 WARN_ON(req->r_sent);
1876 rc = __map_request(osdc, req, 0);
1877 if (rc < 0) {
1878 if (nofail) {
1879 dout("osdc_start_request failed map, "
1880 " will retry %lld\n", req->r_tid);
1881 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001882 }
Alex Elder92451b42013-03-25 18:16:11 -05001883 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001884 }
Alex Elder92451b42013-03-25 18:16:11 -05001885 if (req->r_osd == NULL) {
1886 dout("send_request %p no up osds in pg\n", req);
1887 ceph_monc_request_next_osdmap(&osdc->client->monc);
1888 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05001889 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05001890 }
1891 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03001892out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001893 mutex_unlock(&osdc->request_mutex);
1894 up_read(&osdc->map_sem);
1895 return rc;
1896}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001897EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001898
1899/*
1900 * wait for a request to complete
1901 */
1902int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1903 struct ceph_osd_request *req)
1904{
1905 int rc;
1906
1907 rc = wait_for_completion_interruptible(&req->r_completion);
1908 if (rc < 0) {
1909 mutex_lock(&osdc->request_mutex);
1910 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001911 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001912 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001913 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001914 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001915 return rc;
1916 }
1917
1918 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1919 return req->r_result;
1920}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001921EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001922
1923/*
1924 * sync - wait for all in-flight requests to flush. avoid starvation.
1925 */
1926void ceph_osdc_sync(struct ceph_osd_client *osdc)
1927{
1928 struct ceph_osd_request *req;
1929 u64 last_tid, next_tid = 0;
1930
1931 mutex_lock(&osdc->request_mutex);
1932 last_tid = osdc->last_tid;
1933 while (1) {
1934 req = __lookup_request_ge(osdc, next_tid);
1935 if (!req)
1936 break;
1937 if (req->r_tid > last_tid)
1938 break;
1939
1940 next_tid = req->r_tid + 1;
1941 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1942 continue;
1943
1944 ceph_osdc_get_request(req);
1945 mutex_unlock(&osdc->request_mutex);
1946 dout("sync waiting on tid %llu (last is %llu)\n",
1947 req->r_tid, last_tid);
1948 wait_for_completion(&req->r_safe_completion);
1949 mutex_lock(&osdc->request_mutex);
1950 ceph_osdc_put_request(req);
1951 }
1952 mutex_unlock(&osdc->request_mutex);
1953 dout("sync done (thru tid %llu)\n", last_tid);
1954}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001955EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001956
1957/*
1958 * init, shutdown
1959 */
1960int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1961{
1962 int err;
1963
1964 dout("init\n");
1965 osdc->client = client;
1966 osdc->osdmap = NULL;
1967 init_rwsem(&osdc->map_sem);
1968 init_completion(&osdc->map_waiters);
1969 osdc->last_requested_map = 0;
1970 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001971 osdc->last_tid = 0;
1972 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001973 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001974 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001975 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001976 INIT_LIST_HEAD(&osdc->req_unsent);
1977 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001978 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001979 osdc->num_requests = 0;
1980 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001981 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001982 spin_lock_init(&osdc->event_lock);
1983 osdc->event_tree = RB_ROOT;
1984 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001985
1986 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001987 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001988
Sage Weil5f44f142009-11-18 14:52:18 -08001989 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001990 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1991 sizeof(struct ceph_osd_request));
1992 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001993 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001994
Sage Weild50b4092012-07-09 14:22:34 -07001995 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1996 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001997 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001998 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001999 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002000 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002001 OSD_OPREPLY_FRONT_LEN, 10, true,
2002 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002003 if (err < 0)
2004 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002005
2006 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2007 if (IS_ERR(osdc->notify_wq)) {
2008 err = PTR_ERR(osdc->notify_wq);
2009 osdc->notify_wq = NULL;
2010 goto out_msgpool;
2011 }
Sage Weilf24e9982009-10-06 11:31:10 -07002012 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002013
Sage Weilc16e7862010-03-01 13:02:00 -08002014out_msgpool:
2015 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002016out_mempool:
2017 mempool_destroy(osdc->req_mempool);
2018out:
2019 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002020}
2021
2022void ceph_osdc_stop(struct ceph_osd_client *osdc)
2023{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002024 flush_workqueue(osdc->notify_wq);
2025 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002026 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002027 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002028 if (osdc->osdmap) {
2029 ceph_osdmap_destroy(osdc->osdmap);
2030 osdc->osdmap = NULL;
2031 }
Sage Weilaca420b2011-08-31 14:45:53 -07002032 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002033 mempool_destroy(osdc->req_mempool);
2034 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002035 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002036}
2037
2038/*
2039 * Read some contiguous pages. If we cross a stripe boundary, shorten
2040 * *plen. Return number of bytes read, or error.
2041 */
2042int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2043 struct ceph_vino vino, struct ceph_file_layout *layout,
2044 u64 off, u64 *plen,
2045 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002046 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002047{
2048 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002049 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002050 int rc = 0;
2051
2052 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2053 vino.snap, off, *plen);
2054 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
2055 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2056 NULL, 0, truncate_seq, truncate_size, NULL,
Alex Elder153e5162013-03-01 18:00:15 -06002057 false);
Sage Weil68162822012-09-24 21:01:02 -07002058 if (IS_ERR(req))
2059 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002060
2061 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002062
2063 osd_data = &req->r_data_in;
2064 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2065 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002066 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06002067 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07002068
Alex Eldere0c59482013-03-07 15:38:25 -06002069 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2070 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002071
2072 rc = ceph_osdc_start_request(osdc, req, false);
2073 if (!rc)
2074 rc = ceph_osdc_wait_request(osdc, req);
2075
2076 ceph_osdc_put_request(req);
2077 dout("readpages result %d\n", rc);
2078 return rc;
2079}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002080EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002081
2082/*
2083 * do a synchronous write on N pages
2084 */
2085int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2086 struct ceph_file_layout *layout,
2087 struct ceph_snap_context *snapc,
2088 u64 off, u64 len,
2089 u32 truncate_seq, u64 truncate_size,
2090 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002091 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002092{
2093 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002094 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002095 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002096 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002097
2098 BUG_ON(vino.snap != CEPH_NOSNAP);
2099 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2100 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002101 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06002102 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07002103 truncate_seq, truncate_size, mtime,
Alex Elder153e5162013-03-01 18:00:15 -06002104 true);
Sage Weil68162822012-09-24 21:01:02 -07002105 if (IS_ERR(req))
2106 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002107
2108 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002109 osd_data = &req->r_data_out;
2110 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2111 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002112 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002113 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002114 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002115
Alex Elder87f979d2013-02-15 11:42:29 -06002116 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002117 if (!rc)
2118 rc = ceph_osdc_wait_request(osdc, req);
2119
2120 ceph_osdc_put_request(req);
2121 if (rc == 0)
2122 rc = len;
2123 dout("writepages result %d\n", rc);
2124 return rc;
2125}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002126EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002127
2128/*
2129 * handle incoming message
2130 */
2131static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2132{
2133 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002134 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002135 int type = le16_to_cpu(msg->hdr.type);
2136
2137 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002138 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002139 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002140
2141 switch (type) {
2142 case CEPH_MSG_OSD_MAP:
2143 ceph_osdc_handle_map(osdc, msg);
2144 break;
2145 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002146 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002147 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002148 case CEPH_MSG_WATCH_NOTIFY:
2149 handle_watch_notify(osdc, msg);
2150 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002151
2152 default:
2153 pr_err("received unknown message type %d %s\n", type,
2154 ceph_msg_type_name(type));
2155 }
Sage Weil4a32f932010-06-13 10:27:53 -07002156out:
Sage Weilf24e9982009-10-06 11:31:10 -07002157 ceph_msg_put(msg);
2158}
2159
Sage Weil5b3a4db2010-02-19 21:43:23 -08002160/*
Sage Weil21b667f2010-03-04 10:22:59 -08002161 * lookup and return message for incoming reply. set up reply message
2162 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002163 */
2164static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002165 struct ceph_msg_header *hdr,
2166 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002167{
2168 struct ceph_osd *osd = con->private;
2169 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002170 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002171 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002172 int front = le32_to_cpu(hdr->front_len);
2173 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002174 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002175
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002176 tid = le64_to_cpu(hdr->tid);
2177 mutex_lock(&osdc->request_mutex);
2178 req = __lookup_request(osdc, tid);
2179 if (!req) {
2180 *skip = 1;
2181 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002182 dout("get_reply unknown tid %llu from osd%d\n", tid,
2183 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002184 goto out;
2185 }
Sage Weilc16e7862010-03-01 13:02:00 -08002186
Alex Elderace6d3a2013-04-01 16:12:14 -05002187 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002188 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002189 req->r_reply, req->r_reply->con);
2190 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002191
Sage Weilc16e7862010-03-01 13:02:00 -08002192 if (front > req->r_reply->front.iov_len) {
2193 pr_warning("get_reply front %d > preallocated %d\n",
2194 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002195 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002196 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002197 goto out;
2198 ceph_msg_put(req->r_reply);
2199 req->r_reply = m;
2200 }
2201 m = ceph_msg_get(req->r_reply);
2202
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002203 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002204 struct ceph_osd_data *osd_data = &req->r_data_in;
2205
2206 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002207 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002208 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002209
Alex Eldere0c59482013-03-07 15:38:25 -06002210 pr_warning("tid %lld reply has %d bytes "
2211 "we had only %llu bytes ready\n",
2212 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002213 *skip = 1;
2214 ceph_msg_put(m);
2215 m = NULL;
2216 goto out;
2217 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002218 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002219 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002220 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002221 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002222
2223out:
2224 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002225 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002226
2227}
2228
2229static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2230 struct ceph_msg_header *hdr,
2231 int *skip)
2232{
2233 struct ceph_osd *osd = con->private;
2234 int type = le16_to_cpu(hdr->type);
2235 int front = le32_to_cpu(hdr->front_len);
2236
Alex Elder1c20f2d2012-06-04 14:43:32 -05002237 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002238 switch (type) {
2239 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002240 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002241 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002242 case CEPH_MSG_OSD_OPREPLY:
2243 return get_reply(con, hdr, skip);
2244 default:
2245 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2246 osd->o_osd);
2247 *skip = 1;
2248 return NULL;
2249 }
Sage Weilf24e9982009-10-06 11:31:10 -07002250}
2251
2252/*
2253 * Wrappers to refcount containing ceph_osd struct
2254 */
2255static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2256{
2257 struct ceph_osd *osd = con->private;
2258 if (get_osd(osd))
2259 return con;
2260 return NULL;
2261}
2262
2263static void put_osd_con(struct ceph_connection *con)
2264{
2265 struct ceph_osd *osd = con->private;
2266 put_osd(osd);
2267}
2268
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002269/*
2270 * authentication
2271 */
Alex Eldera3530df2012-05-16 15:16:39 -05002272/*
2273 * Note: returned pointer is the address of a structure that's
2274 * managed separately. Caller must *not* attempt to free it.
2275 */
2276static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002277 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002278{
2279 struct ceph_osd *o = con->private;
2280 struct ceph_osd_client *osdc = o->o_osdc;
2281 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002282 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002283
Alex Elder74f18692012-05-16 15:16:39 -05002284 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002285 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002286 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002287 }
Sage Weil27859f92013-03-25 10:26:14 -07002288 if (!auth->authorizer) {
2289 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2290 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002291 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002292 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002293 } else {
2294 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002295 auth);
2296 if (ret)
2297 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002298 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002299 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002300
Alex Eldera3530df2012-05-16 15:16:39 -05002301 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002302}
2303
2304
2305static int verify_authorizer_reply(struct ceph_connection *con, int len)
2306{
2307 struct ceph_osd *o = con->private;
2308 struct ceph_osd_client *osdc = o->o_osdc;
2309 struct ceph_auth_client *ac = osdc->client->monc.auth;
2310
Sage Weil27859f92013-03-25 10:26:14 -07002311 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002312}
2313
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002314static int invalidate_authorizer(struct ceph_connection *con)
2315{
2316 struct ceph_osd *o = con->private;
2317 struct ceph_osd_client *osdc = o->o_osdc;
2318 struct ceph_auth_client *ac = osdc->client->monc.auth;
2319
Sage Weil27859f92013-03-25 10:26:14 -07002320 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002321 return ceph_monc_validate_auth(&osdc->client->monc);
2322}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002323
Tobias Klauser9e327892010-05-20 10:40:19 +02002324static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002325 .get = get_osd_con,
2326 .put = put_osd_con,
2327 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002328 .get_authorizer = get_authorizer,
2329 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002330 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002331 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002332 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002333};