blob: 1379b331334884dc2dec77bcb7b8aec686d017e3 [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
Alex Eldere5975c72013-03-14 14:09:05 -0500299void osd_req_op_extent_update(struct ceph_osd_req_op *op, u64 length)
300{
301 u64 previous = op->extent.length;
302
303 if (length == previous)
304 return; /* Nothing to do */
305 BUG_ON(length > previous);
306
307 op->extent.length = length;
308 op->payload_len -= previous - length;
309}
310EXPORT_SYMBOL(osd_req_op_extent_update);
311
Alex Elder33803f32013-03-13 20:50:00 -0500312void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
313 const char *class, const char *method,
314 const void *request_data, size_t request_data_size)
315{
316 size_t payload_len = 0;
317 size_t size;
318
319 BUG_ON(opcode != CEPH_OSD_OP_CALL);
320
321 osd_req_op_init(op, opcode);
322
323 op->cls.class_name = class;
324 size = strlen(class);
325 BUG_ON(size > (size_t) U8_MAX);
326 op->cls.class_len = size;
327 payload_len += size;
328
329 op->cls.method_name = method;
330 size = strlen(method);
331 BUG_ON(size > (size_t) U8_MAX);
332 op->cls.method_len = size;
333 payload_len += size;
334
335 op->cls.indata = request_data;
336 BUG_ON(request_data_size > (size_t) U32_MAX);
337 op->cls.indata_len = (u32) request_data_size;
338 payload_len += request_data_size;
339
340 op->cls.argc = 0; /* currently unused */
341
342 op->payload_len = payload_len;
343}
344EXPORT_SYMBOL(osd_req_op_cls_init);
345
346void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
347 u64 cookie, u64 version, int flag)
348{
349 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
350
351 osd_req_op_init(op, opcode);
352
353 op->watch.cookie = cookie;
354 /* op->watch.ver = version; */ /* XXX 3847 */
355 op->watch.ver = cpu_to_le64(version);
356 if (opcode == CEPH_OSD_OP_WATCH && flag)
357 op->watch.flag = (u8) 1;
358}
359EXPORT_SYMBOL(osd_req_op_watch_init);
360
Alex Elder175face2013-03-08 13:35:36 -0600361static u64 osd_req_encode_op(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700362 struct ceph_osd_op *dst,
363 struct ceph_osd_req_op *src)
364{
Alex Elder175face2013-03-08 13:35:36 -0600365 u64 out_data_len = 0;
Alex Elder95e072e2013-03-08 13:35:36 -0600366 struct ceph_pagelist *pagelist;
Alex Elder175face2013-03-08 13:35:36 -0600367
Alex Eldera8dd0a32013-03-13 20:50:00 -0500368 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
369 pr_err("unrecognized osd opcode %d\n", src->op);
370
371 return 0;
372 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700373
Alex Elder065a68f2012-04-20 15:49:43 -0500374 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600375 case CEPH_OSD_OP_STAT:
376 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700377 case CEPH_OSD_OP_READ:
378 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600379 if (src->op == CEPH_OSD_OP_WRITE)
380 out_data_len = src->extent.length;
381 dst->extent.offset = cpu_to_le64(src->extent.offset);
382 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700383 dst->extent.truncate_size =
384 cpu_to_le64(src->extent.truncate_size);
385 dst->extent.truncate_seq =
386 cpu_to_le32(src->extent.truncate_seq);
387 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700388 case CEPH_OSD_OP_CALL:
Alex Elder95e072e2013-03-08 13:35:36 -0600389 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
390 BUG_ON(!pagelist);
391 ceph_pagelist_init(pagelist);
392
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700393 dst->cls.class_len = src->cls.class_len;
394 dst->cls.method_len = src->cls.method_len;
395 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600396 ceph_pagelist_append(pagelist, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700397 src->cls.class_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600398 ceph_pagelist_append(pagelist, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700399 src->cls.method_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600400 ceph_pagelist_append(pagelist, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700401 src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600402
403 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
404 req->r_data_out.pagelist = pagelist;
405 out_data_len = pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700406 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700407 case CEPH_OSD_OP_STARTSYNC:
408 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700409 case CEPH_OSD_OP_NOTIFY_ACK:
410 case CEPH_OSD_OP_WATCH:
411 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
412 dst->watch.ver = cpu_to_le64(src->watch.ver);
413 dst->watch.flag = src->watch.flag;
414 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700415 default:
Alex Elder4c464592013-02-15 11:42:30 -0600416 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600417 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600418 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500419
420 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700421 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500422 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700423 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600424
425 return out_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700426}
427
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700428/*
429 * build new request AND message
430 *
431 */
432void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder175face2013-03-08 13:35:36 -0600433 u64 off, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700434 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600435 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600436 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700437{
438 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700439 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700440 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800441 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700442 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600443 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700444 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700445
Sage Weil1b83bef2013-02-25 16:11:12 -0800446 req->r_num_ops = num_ops;
447 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700448 req->r_snapc = ceph_get_snap_context(snapc);
449
Sage Weil1b83bef2013-02-25 16:11:12 -0800450 /* encode request */
451 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700452
Sage Weil1b83bef2013-02-25 16:11:12 -0800453 p = msg->front.iov_base;
454 ceph_encode_32(&p, 1); /* client_inc is always 1 */
455 req->r_request_osdmap_epoch = p;
456 p += 4;
457 req->r_request_flags = p;
458 p += 4;
459 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
460 ceph_encode_timespec(p, mtime);
461 p += sizeof(struct ceph_timespec);
462 req->r_request_reassert_version = p;
463 p += sizeof(struct ceph_eversion); /* will get filled in */
464
465 /* oloc */
466 ceph_encode_8(&p, 4);
467 ceph_encode_8(&p, 4);
468 ceph_encode_32(&p, 8 + 4 + 4);
469 req->r_request_pool = p;
470 p += 8;
471 ceph_encode_32(&p, -1); /* preferred */
472 ceph_encode_32(&p, 0); /* key len */
473
474 ceph_encode_8(&p, 1);
475 req->r_request_pgid = p;
476 p += 8 + 4;
477 ceph_encode_32(&p, -1); /* preferred */
478
479 /* oid */
480 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600481 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800482 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600483 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700484
Alex Elder175face2013-03-08 13:35:36 -0600485 /* ops--can imply data */
Sage Weil1b83bef2013-02-25 16:11:12 -0800486 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700487 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800488 req->r_request_ops = p;
Alex Elder175face2013-03-08 13:35:36 -0600489 data_len = 0;
Sage Weil1b83bef2013-02-25 16:11:12 -0800490 for (i = 0; i < num_ops; i++, src_op++) {
Alex Elder175face2013-03-08 13:35:36 -0600491 data_len += osd_req_encode_op(req, p, src_op);
Sage Weil1b83bef2013-02-25 16:11:12 -0800492 p += sizeof(struct ceph_osd_op);
493 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700494
Sage Weil1b83bef2013-02-25 16:11:12 -0800495 /* snaps */
496 ceph_encode_64(&p, req->r_snapid);
497 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
498 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
499 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700500 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800501 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700502 }
503 }
504
Sage Weil1b83bef2013-02-25 16:11:12 -0800505 req->r_request_attempts = p;
506 p += 4;
507
Alex Elder175face2013-03-08 13:35:36 -0600508 /* data */
Alex Elder0baa1bd2013-03-29 14:28:03 -0500509 if (flags & CEPH_OSD_FLAG_WRITE) {
510 u16 data_off;
511
512 /*
513 * The header "data_off" is a hint to the receiver
514 * allowing it to align received data into its
515 * buffers such that there's no need to re-copy
516 * it before writing it to disk (direct I/O).
517 */
518 data_off = (u16) (off & 0xffff);
519 req->r_request->hdr.data_off = cpu_to_le16(data_off);
520 }
Alex Elderf44246e2013-02-14 12:16:43 -0600521 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800522
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700523 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
524 msg_size = p - msg->front.iov_base;
525 msg->front.iov_len = msg_size;
526 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800527
Alex Elderacead002013-03-14 14:09:05 -0500528 dout("build_request msg_size was %d\n", (int)msg_size);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700529}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700530EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700531
Sage Weilf24e9982009-10-06 11:31:10 -0700532/*
533 * build new request AND message, calculate layout, and adjust file
534 * extent as needed.
535 *
536 * if the file was recently truncated, we include information about its
537 * old and new size so that the object can be updated appropriately. (we
538 * avoid synchronously deleting truncated objects because it's slow.)
539 *
540 * if @do_sync, include a 'startsync' command so that the osd will flush
541 * data quickly.
542 */
543struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
544 struct ceph_file_layout *layout,
545 struct ceph_vino vino,
Alex Elderacead002013-03-14 14:09:05 -0500546 u64 off, u64 *plen, int num_ops,
547 struct ceph_osd_req_op *ops,
Sage Weilf24e9982009-10-06 11:31:10 -0700548 int opcode, int flags,
549 struct ceph_snap_context *snapc,
Sage Weilf24e9982009-10-06 11:31:10 -0700550 u32 truncate_seq,
551 u64 truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -0600552 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700553{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700554 struct ceph_osd_request *req;
Alex Elder75d1c942013-03-13 20:50:00 -0500555 u64 objnum = 0;
556 u64 objoff = 0;
557 u64 objlen = 0;
Alex Elderd18d1e22013-03-13 20:50:01 -0500558 u32 object_size;
559 u64 object_base;
Sage Weil68162822012-09-24 21:01:02 -0700560 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700561
Alex Elderd18d1e22013-03-13 20:50:01 -0500562 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700563
Alex Elderacead002013-03-14 14:09:05 -0500564 req = ceph_osdc_alloc_request(osdc, snapc, num_ops, use_mempool,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600565 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700566 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700567 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600568 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700569
570 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500571 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600572 if (r < 0) {
573 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700574 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600575 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500576
Alex Elderd18d1e22013-03-13 20:50:01 -0500577 object_size = le32_to_cpu(layout->fl_object_size);
578 object_base = off - objoff;
579 if (truncate_size <= object_base) {
580 truncate_size = 0;
581 } else {
582 truncate_size -= object_base;
583 if (truncate_size > object_size)
584 truncate_size = object_size;
Alex Eldera19dadf2013-03-13 20:50:01 -0500585 }
Alex Elderd18d1e22013-03-13 20:50:01 -0500586
Alex Elderb0270322013-03-13 20:50:01 -0500587 osd_req_op_extent_init(&ops[0], opcode, objoff, objlen,
588 truncate_size, truncate_seq);
Alex Elderacead002013-03-14 14:09:05 -0500589 /*
590 * A second op in the ops array means the caller wants to
591 * also issue a include a 'startsync' command so that the
592 * osd will flush data quickly.
593 */
594 if (num_ops > 1)
Alex Elderb0270322013-03-13 20:50:01 -0500595 osd_req_op_init(&ops[1], CEPH_OSD_OP_STARTSYNC);
Alex Elderd18d1e22013-03-13 20:50:01 -0500596
Sage Weilf24e9982009-10-06 11:31:10 -0700597 req->r_file_layout = *layout; /* keep a copy */
598
Alex Elder75d1c942013-03-13 20:50:00 -0500599 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
600 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600601 req->r_oid_len = strlen(req->r_oid);
602
Sage Weilf24e9982009-10-06 11:31:10 -0700603 return req;
604}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700605EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700606
607/*
608 * We keep osd requests in an rbtree, sorted by ->r_tid.
609 */
610static void __insert_request(struct ceph_osd_client *osdc,
611 struct ceph_osd_request *new)
612{
613 struct rb_node **p = &osdc->requests.rb_node;
614 struct rb_node *parent = NULL;
615 struct ceph_osd_request *req = NULL;
616
617 while (*p) {
618 parent = *p;
619 req = rb_entry(parent, struct ceph_osd_request, r_node);
620 if (new->r_tid < req->r_tid)
621 p = &(*p)->rb_left;
622 else if (new->r_tid > req->r_tid)
623 p = &(*p)->rb_right;
624 else
625 BUG();
626 }
627
628 rb_link_node(&new->r_node, parent, p);
629 rb_insert_color(&new->r_node, &osdc->requests);
630}
631
632static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
633 u64 tid)
634{
635 struct ceph_osd_request *req;
636 struct rb_node *n = osdc->requests.rb_node;
637
638 while (n) {
639 req = rb_entry(n, struct ceph_osd_request, r_node);
640 if (tid < req->r_tid)
641 n = n->rb_left;
642 else if (tid > req->r_tid)
643 n = n->rb_right;
644 else
645 return req;
646 }
647 return NULL;
648}
649
650static struct ceph_osd_request *
651__lookup_request_ge(struct ceph_osd_client *osdc,
652 u64 tid)
653{
654 struct ceph_osd_request *req;
655 struct rb_node *n = osdc->requests.rb_node;
656
657 while (n) {
658 req = rb_entry(n, struct ceph_osd_request, r_node);
659 if (tid < req->r_tid) {
660 if (!n->rb_left)
661 return req;
662 n = n->rb_left;
663 } else if (tid > req->r_tid) {
664 n = n->rb_right;
665 } else {
666 return req;
667 }
668 }
669 return NULL;
670}
671
Sage Weil6f6c7002011-01-17 20:34:08 -0800672/*
673 * Resubmit requests pending on the given osd.
674 */
675static void __kick_osd_requests(struct ceph_osd_client *osdc,
676 struct ceph_osd *osd)
677{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700678 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500679 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800680 int err;
681
682 dout("__kick_osd_requests osd%d\n", osd->o_osd);
683 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600684 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800685 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500686 /*
687 * Build up a list of requests to resend by traversing the
688 * osd's list of requests. Requests for a given object are
689 * sent in tid order, and that is also the order they're
690 * kept on this list. Therefore all requests that are in
691 * flight will be found first, followed by all requests that
692 * have not yet been sent. And to resend requests while
693 * preserving this order we will want to put any sent
694 * requests back on the front of the osd client's unsent
695 * list.
696 *
697 * So we build a separate ordered list of already-sent
698 * requests for the affected osd and splice it onto the
699 * front of the osd client's unsent list. Once we've seen a
700 * request that has not yet been sent we're done. Those
701 * requests are already sitting right where they belong.
702 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800703 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500704 if (!req->r_sent)
705 break;
706 list_move_tail(&req->r_req_lru_item, &resend);
707 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800708 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700709 if (!req->r_linger)
710 req->r_flags |= CEPH_OSD_FLAG_RETRY;
711 }
Alex Eldere02493c2013-03-25 18:16:11 -0500712 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700713
Alex Eldere02493c2013-03-25 18:16:11 -0500714 /*
715 * Linger requests are re-registered before sending, which
716 * sets up a new tid for each. We add them to the unsent
717 * list at the end to keep things in tid order.
718 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700719 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
720 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700721 /*
722 * reregister request prior to unregistering linger so
723 * that r_osd is preserved.
724 */
725 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700726 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500727 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500728 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700729 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700730 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
731 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800732 }
733}
734
Sage Weilf24e9982009-10-06 11:31:10 -0700735/*
Sage Weil81b024e2009-10-09 10:29:18 -0700736 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700737 */
738static void osd_reset(struct ceph_connection *con)
739{
740 struct ceph_osd *osd = con->private;
741 struct ceph_osd_client *osdc;
742
743 if (!osd)
744 return;
745 dout("osd_reset osd%d\n", osd->o_osd);
746 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700747 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800748 mutex_lock(&osdc->request_mutex);
749 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600750 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800751 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700752 up_read(&osdc->map_sem);
753}
754
755/*
756 * Track open sessions with osds.
757 */
Alex Eldere10006f2012-05-26 23:26:43 -0500758static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700759{
760 struct ceph_osd *osd;
761
762 osd = kzalloc(sizeof(*osd), GFP_NOFS);
763 if (!osd)
764 return NULL;
765
766 atomic_set(&osd->o_ref, 1);
767 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500768 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600769 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700770 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700771 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800772 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700773 osd->o_incarnation = 1;
774
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700775 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800776
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800777 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700778 return osd;
779}
780
781static struct ceph_osd *get_osd(struct ceph_osd *osd)
782{
783 if (atomic_inc_not_zero(&osd->o_ref)) {
784 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
785 atomic_read(&osd->o_ref));
786 return osd;
787 } else {
788 dout("get_osd %p FAIL\n", osd);
789 return NULL;
790 }
791}
792
793static void put_osd(struct ceph_osd *osd)
794{
795 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
796 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500797 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700798 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
799
Sage Weil27859f92013-03-25 10:26:14 -0700800 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700801 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700802 }
Sage Weilf24e9982009-10-06 11:31:10 -0700803}
804
805/*
806 * remove an osd from our map
807 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800808static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700809{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800810 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700811 BUG_ON(!list_empty(&osd->o_requests));
812 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800813 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700814 ceph_con_close(&osd->o_con);
815 put_osd(osd);
816}
817
Sage Weilaca420b2011-08-31 14:45:53 -0700818static void remove_all_osds(struct ceph_osd_client *osdc)
819{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500820 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700821 mutex_lock(&osdc->request_mutex);
822 while (!RB_EMPTY_ROOT(&osdc->osds)) {
823 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
824 struct ceph_osd, o_node);
825 __remove_osd(osdc, osd);
826 }
827 mutex_unlock(&osdc->request_mutex);
828}
829
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800830static void __move_osd_to_lru(struct ceph_osd_client *osdc,
831 struct ceph_osd *osd)
832{
833 dout("__move_osd_to_lru %p\n", osd);
834 BUG_ON(!list_empty(&osd->o_osd_lru));
835 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700836 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800837}
838
839static void __remove_osd_from_lru(struct ceph_osd *osd)
840{
841 dout("__remove_osd_from_lru %p\n", osd);
842 if (!list_empty(&osd->o_osd_lru))
843 list_del_init(&osd->o_osd_lru);
844}
845
Sage Weilaca420b2011-08-31 14:45:53 -0700846static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800847{
848 struct ceph_osd *osd, *nosd;
849
850 dout("__remove_old_osds %p\n", osdc);
851 mutex_lock(&osdc->request_mutex);
852 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700853 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800854 break;
855 __remove_osd(osdc, osd);
856 }
857 mutex_unlock(&osdc->request_mutex);
858}
859
Sage Weilf24e9982009-10-06 11:31:10 -0700860/*
861 * reset osd connect
862 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800863static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700864{
Alex Elderc3acb182012-12-07 09:57:58 -0600865 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700866
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800867 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700868 if (list_empty(&osd->o_requests) &&
869 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800870 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600871
872 return -ENODEV;
873 }
874
875 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
876 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
877 !ceph_con_opened(&osd->o_con)) {
878 struct ceph_osd_request *req;
879
Sage Weil87b315a2010-03-22 14:51:18 -0700880 dout(" osd addr hasn't changed and connection never opened,"
881 " letting msgr retry");
882 /* touch each r_stamp for handle_timeout()'s benfit */
883 list_for_each_entry(req, &osd->o_requests, r_osd_item)
884 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600885
886 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700887 }
Alex Elderc3acb182012-12-07 09:57:58 -0600888
889 ceph_con_close(&osd->o_con);
890 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
891 osd->o_incarnation++;
892
893 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700894}
895
896static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
897{
898 struct rb_node **p = &osdc->osds.rb_node;
899 struct rb_node *parent = NULL;
900 struct ceph_osd *osd = NULL;
901
Sage Weilaca420b2011-08-31 14:45:53 -0700902 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700903 while (*p) {
904 parent = *p;
905 osd = rb_entry(parent, struct ceph_osd, o_node);
906 if (new->o_osd < osd->o_osd)
907 p = &(*p)->rb_left;
908 else if (new->o_osd > osd->o_osd)
909 p = &(*p)->rb_right;
910 else
911 BUG();
912 }
913
914 rb_link_node(&new->o_node, parent, p);
915 rb_insert_color(&new->o_node, &osdc->osds);
916}
917
918static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
919{
920 struct ceph_osd *osd;
921 struct rb_node *n = osdc->osds.rb_node;
922
923 while (n) {
924 osd = rb_entry(n, struct ceph_osd, o_node);
925 if (o < osd->o_osd)
926 n = n->rb_left;
927 else if (o > osd->o_osd)
928 n = n->rb_right;
929 else
930 return osd;
931 }
932 return NULL;
933}
934
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800935static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
936{
937 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700938 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800939}
940
941static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
942{
943 cancel_delayed_work(&osdc->timeout_work);
944}
Sage Weilf24e9982009-10-06 11:31:10 -0700945
946/*
947 * Register request, assign tid. If this is the first request, set up
948 * the timeout event.
949 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700950static void __register_request(struct ceph_osd_client *osdc,
951 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700952{
Sage Weilf24e9982009-10-06 11:31:10 -0700953 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800954 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700955 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700956 __insert_request(osdc, req);
957 ceph_osdc_get_request(req);
958 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700959 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800960 dout(" first request, scheduling timeout\n");
961 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700962 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700963}
964
Sage Weilf24e9982009-10-06 11:31:10 -0700965/*
966 * called under osdc->request_mutex
967 */
968static void __unregister_request(struct ceph_osd_client *osdc,
969 struct ceph_osd_request *req)
970{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500971 if (RB_EMPTY_NODE(&req->r_node)) {
972 dout("__unregister_request %p tid %lld not registered\n",
973 req, req->r_tid);
974 return;
975 }
976
Sage Weilf24e9982009-10-06 11:31:10 -0700977 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
978 rb_erase(&req->r_node, &osdc->requests);
979 osdc->num_requests--;
980
Sage Weil0ba64782009-10-08 16:57:16 -0700981 if (req->r_osd) {
982 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500983 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700984
985 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700986 if (list_empty(&req->r_osd->o_requests) &&
987 list_empty(&req->r_osd->o_linger_requests)) {
988 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800989 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700990 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700991 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700992 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700993 }
Sage Weilf24e9982009-10-06 11:31:10 -0700994
Alex Elder7d5f2482012-11-29 08:37:03 -0600995 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700996 ceph_osdc_put_request(req);
997
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800998 if (osdc->num_requests == 0) {
999 dout(" no requests, canceling timeout\n");
1000 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001001 }
1002}
1003
1004/*
1005 * Cancel a previously queued request message
1006 */
1007static void __cancel_request(struct ceph_osd_request *req)
1008{
Sage Weil6bc18872010-09-27 10:18:52 -07001009 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001010 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001011 req->r_sent = 0;
1012 }
1013}
1014
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001015static void __register_linger_request(struct ceph_osd_client *osdc,
1016 struct ceph_osd_request *req)
1017{
1018 dout("__register_linger_request %p\n", req);
1019 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001020 if (req->r_osd)
1021 list_add_tail(&req->r_linger_osd,
1022 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001023}
1024
1025static void __unregister_linger_request(struct ceph_osd_client *osdc,
1026 struct ceph_osd_request *req)
1027{
1028 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001029 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001030 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001031 list_del_init(&req->r_linger_osd);
1032
1033 if (list_empty(&req->r_osd->o_requests) &&
1034 list_empty(&req->r_osd->o_linger_requests)) {
1035 dout("moving osd to %p lru\n", req->r_osd);
1036 __move_osd_to_lru(osdc, req->r_osd);
1037 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001038 if (list_empty(&req->r_osd_item))
1039 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001040 }
1041}
1042
1043void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1044 struct ceph_osd_request *req)
1045{
1046 mutex_lock(&osdc->request_mutex);
1047 if (req->r_linger) {
1048 __unregister_linger_request(osdc, req);
1049 ceph_osdc_put_request(req);
1050 }
1051 mutex_unlock(&osdc->request_mutex);
1052}
1053EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1054
1055void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1056 struct ceph_osd_request *req)
1057{
1058 if (!req->r_linger) {
1059 dout("set_request_linger %p\n", req);
1060 req->r_linger = 1;
1061 /*
1062 * caller is now responsible for calling
1063 * unregister_linger_request
1064 */
1065 ceph_osdc_get_request(req);
1066 }
1067}
1068EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1069
Sage Weilf24e9982009-10-06 11:31:10 -07001070/*
1071 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1072 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001073 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001074 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001075 *
1076 * Return 0 if unchanged, 1 if changed, or negative on error.
1077 *
1078 * Caller should hold map_sem for read and request_mutex.
1079 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001080static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001081 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001082{
Sage Weil5b191d92013-02-23 10:38:16 -08001083 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001084 int acting[CEPH_PG_MAX_SIZE];
1085 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001086 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001087
Sage Weil6f6c7002011-01-17 20:34:08 -08001088 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001089 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1090 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001091 if (err) {
1092 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001093 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001094 }
Sage Weil7740a422010-01-08 15:58:25 -08001095 req->r_pgid = pgid;
1096
Sage Weild85b7052010-05-10 10:24:48 -07001097 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1098 if (err > 0) {
1099 o = acting[0];
1100 num = err;
1101 }
Sage Weilf24e9982009-10-06 11:31:10 -07001102
Sage Weil38d64532011-10-14 13:33:55 -07001103 if ((!force_resend &&
1104 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001105 req->r_sent >= req->r_osd->o_incarnation &&
1106 req->r_num_pg_osds == num &&
1107 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001108 (req->r_osd == NULL && o == -1))
1109 return 0; /* no change */
1110
Sage Weil5b191d92013-02-23 10:38:16 -08001111 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1112 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001113 req->r_osd ? req->r_osd->o_osd : -1);
1114
Sage Weild85b7052010-05-10 10:24:48 -07001115 /* record full pg acting set */
1116 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1117 req->r_num_pg_osds = num;
1118
Sage Weilf24e9982009-10-06 11:31:10 -07001119 if (req->r_osd) {
1120 __cancel_request(req);
1121 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001122 req->r_osd = NULL;
1123 }
1124
1125 req->r_osd = __lookup_osd(osdc, o);
1126 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001127 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001128 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001129 if (!req->r_osd) {
1130 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001131 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001132 }
Sage Weilf24e9982009-10-06 11:31:10 -07001133
Sage Weil6f6c7002011-01-17 20:34:08 -08001134 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001135 __insert_osd(osdc, req->r_osd);
1136
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001137 ceph_con_open(&req->r_osd->o_con,
1138 CEPH_ENTITY_TYPE_OSD, o,
1139 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001140 }
1141
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001142 if (req->r_osd) {
1143 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001144 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1145 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001146 } else {
Alex Elderad885922013-03-25 18:16:11 -05001147 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001148 }
Sage Weild85b7052010-05-10 10:24:48 -07001149 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001150
1151out:
Sage Weilf24e9982009-10-06 11:31:10 -07001152 return err;
1153}
1154
1155/*
1156 * caller should hold map_sem (for read) and request_mutex
1157 */
Sage Weil56e925b2012-01-03 12:34:34 -08001158static void __send_request(struct ceph_osd_client *osdc,
1159 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001160{
Sage Weil1b83bef2013-02-25 16:11:12 -08001161 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001162
Sage Weil1b83bef2013-02-25 16:11:12 -08001163 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1164 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1165 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001166
Sage Weil1b83bef2013-02-25 16:11:12 -08001167 /* fill in message content that changes each time we send it */
1168 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1169 put_unaligned_le32(req->r_flags, req->r_request_flags);
1170 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1171 p = req->r_request_pgid;
1172 ceph_encode_64(&p, req->r_pgid.pool);
1173 ceph_encode_32(&p, req->r_pgid.seed);
1174 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1175 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1176 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001177
Sage Weil3dd72fc2010-03-22 14:42:30 -07001178 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001179 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001180
1181 ceph_msg_get(req->r_request); /* send consumes a ref */
1182 ceph_con_send(&req->r_osd->o_con, req->r_request);
1183 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001184}
1185
1186/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001187 * Send any requests in the queue (req_unsent).
1188 */
Alex Elderf9d25192013-02-15 11:42:29 -06001189static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001190{
1191 struct ceph_osd_request *req, *tmp;
1192
Alex Elderf9d25192013-02-15 11:42:29 -06001193 dout("__send_queued\n");
1194 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001195 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001196}
1197
1198/*
Sage Weilf24e9982009-10-06 11:31:10 -07001199 * Timeout callback, called every N seconds when 1 or more osd
1200 * requests has been active for more than N seconds. When this
1201 * happens, we ping all OSDs with requests who have timed out to
1202 * ensure any communications channel reset is detected. Reset the
1203 * request timeouts another N seconds in the future as we go.
1204 * Reschedule the timeout event another N seconds in future (unless
1205 * there are no open requests).
1206 */
1207static void handle_timeout(struct work_struct *work)
1208{
1209 struct ceph_osd_client *osdc =
1210 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001211 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001212 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001213 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001214 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001215 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001216 dout("timeout\n");
1217 down_read(&osdc->map_sem);
1218
1219 ceph_monc_request_next_osdmap(&osdc->client->monc);
1220
1221 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001222
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001223 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001224 * ping osds that are a bit slow. this ensures that if there
1225 * is a break in the TCP connection we will notice, and reopen
1226 * a connection with that osd (from the fault callback).
1227 */
1228 INIT_LIST_HEAD(&slow_osds);
1229 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001230 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001231 break;
1232
1233 osd = req->r_osd;
1234 BUG_ON(!osd);
1235 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001236 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001237 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1238 }
1239 while (!list_empty(&slow_osds)) {
1240 osd = list_entry(slow_osds.next, struct ceph_osd,
1241 o_keepalive_item);
1242 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001243 ceph_con_keepalive(&osd->o_con);
1244 }
1245
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001246 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001247 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001248 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001249 up_read(&osdc->map_sem);
1250}
1251
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001252static void handle_osds_timeout(struct work_struct *work)
1253{
1254 struct ceph_osd_client *osdc =
1255 container_of(work, struct ceph_osd_client,
1256 osds_timeout_work.work);
1257 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001258 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001259
1260 dout("osds timeout\n");
1261 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001262 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001263 up_read(&osdc->map_sem);
1264
1265 schedule_delayed_work(&osdc->osds_timeout_work,
1266 round_jiffies_relative(delay));
1267}
1268
Sage Weil25845472011-06-03 09:37:09 -07001269static void complete_request(struct ceph_osd_request *req)
1270{
1271 if (req->r_safe_callback)
1272 req->r_safe_callback(req, NULL);
1273 complete_all(&req->r_safe_completion); /* fsync waiter */
1274}
1275
Sage Weilf24e9982009-10-06 11:31:10 -07001276/*
1277 * handle osd op reply. either call the callback if it is specified,
1278 * or do the completion to wake up the waiting thread.
1279 */
Sage Weil350b1c32009-12-22 10:45:45 -08001280static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1281 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001282{
Sage Weil1b83bef2013-02-25 16:11:12 -08001283 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001284 struct ceph_osd_request *req;
1285 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001286 int object_len;
1287 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001288 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001289 s32 retry_attempt;
1290 struct ceph_pg pg;
1291 int err;
1292 u32 reassert_epoch;
1293 u64 reassert_version;
1294 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001295 int already_completed;
Alex Elder9fc6e062013-04-03 01:28:57 -05001296 u32 bytes;
Sage Weil1b83bef2013-02-25 16:11:12 -08001297 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001298
Sage Weil6df058c2009-12-22 11:24:33 -08001299 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001300 dout("handle_reply %p tid %llu\n", msg, tid);
1301
1302 p = msg->front.iov_base;
1303 end = p + msg->front.iov_len;
1304
1305 ceph_decode_need(&p, end, 4, bad);
1306 object_len = ceph_decode_32(&p);
1307 ceph_decode_need(&p, end, object_len, bad);
1308 p += object_len;
1309
Alex Elderef4859d2013-04-01 18:58:26 -05001310 err = ceph_decode_pgid(&p, end, &pg);
Sage Weil1b83bef2013-02-25 16:11:12 -08001311 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001312 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001313
1314 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1315 flags = ceph_decode_64(&p);
1316 result = ceph_decode_32(&p);
1317 reassert_epoch = ceph_decode_32(&p);
1318 reassert_version = ceph_decode_64(&p);
1319 osdmap_epoch = ceph_decode_32(&p);
1320
Sage Weilf24e9982009-10-06 11:31:10 -07001321 /* lookup */
1322 mutex_lock(&osdc->request_mutex);
1323 req = __lookup_request(osdc, tid);
1324 if (req == NULL) {
1325 dout("handle_reply tid %llu dne\n", tid);
Alex Elder8058fd42013-04-01 18:58:26 -05001326 goto bad_mutex;
Sage Weilf24e9982009-10-06 11:31:10 -07001327 }
1328 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001329
1330 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1331 req, result);
1332
1333 ceph_decode_need(&p, end, 4, bad);
1334 numops = ceph_decode_32(&p);
1335 if (numops > CEPH_OSD_MAX_OP)
1336 goto bad_put;
1337 if (numops != req->r_num_ops)
1338 goto bad_put;
1339 payload_len = 0;
1340 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1341 for (i = 0; i < numops; i++) {
1342 struct ceph_osd_op *op = p;
1343 int len;
1344
1345 len = le32_to_cpu(op->payload_len);
1346 req->r_reply_op_len[i] = len;
1347 dout(" op %d has %d bytes\n", i, len);
1348 payload_len += len;
1349 p += sizeof(*op);
1350 }
Alex Elder9fc6e062013-04-03 01:28:57 -05001351 bytes = le32_to_cpu(msg->hdr.data_len);
1352 if (payload_len != bytes) {
Sage Weil1b83bef2013-02-25 16:11:12 -08001353 pr_warning("sum of op payload lens %d != data_len %d",
Alex Elder9fc6e062013-04-03 01:28:57 -05001354 payload_len, bytes);
Sage Weil1b83bef2013-02-25 16:11:12 -08001355 goto bad_put;
1356 }
1357
1358 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1359 retry_attempt = ceph_decode_32(&p);
1360 for (i = 0; i < numops; i++)
1361 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001362
Sage Weilf24e9982009-10-06 11:31:10 -07001363 if (!req->r_got_reply) {
Sage Weilf24e9982009-10-06 11:31:10 -07001364
Sage Weil1b83bef2013-02-25 16:11:12 -08001365 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001366 dout("handle_reply result %d bytes %d\n", req->r_result,
1367 bytes);
1368 if (req->r_result == 0)
1369 req->r_result = bytes;
1370
1371 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001372 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1373 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001374
1375 req->r_got_reply = 1;
1376 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1377 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001378 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001379 goto done;
1380 }
1381
1382 dout("handle_reply tid %llu flags %d\n", tid, flags);
1383
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001384 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1385 __register_linger_request(osdc, req);
1386
Sage Weilf24e9982009-10-06 11:31:10 -07001387 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001388 if (result < 0 ||
1389 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001390 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1391 __unregister_request(osdc, req);
1392
Alex Elder0d5af162013-02-27 10:26:25 -06001393 already_completed = req->r_completed;
1394 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001395 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001396 if (already_completed)
1397 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001398
1399 if (req->r_callback)
1400 req->r_callback(req, msg);
1401 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001402 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001403
Sage Weil25845472011-06-03 09:37:09 -07001404 if (flags & CEPH_OSD_FLAG_ONDISK)
1405 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001406
1407done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001408 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001409 ceph_osdc_put_request(req);
1410 return;
1411
Sage Weil1b83bef2013-02-25 16:11:12 -08001412bad_put:
1413 ceph_osdc_put_request(req);
Alex Elder8058fd42013-04-01 18:58:26 -05001414bad_mutex:
1415 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001416bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001417 pr_err("corrupt osd_op_reply got %d %d\n",
1418 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001419 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001420}
1421
Sage Weil6f6c7002011-01-17 20:34:08 -08001422static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001423{
Sage Weilf24e9982009-10-06 11:31:10 -07001424 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001425
Sage Weil6f6c7002011-01-17 20:34:08 -08001426 for (p = rb_first(&osdc->osds); p; p = n) {
1427 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001428
Sage Weil6f6c7002011-01-17 20:34:08 -08001429 n = rb_next(p);
1430 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1431 memcmp(&osd->o_con.peer_addr,
1432 ceph_osd_addr(osdc->osdmap,
1433 osd->o_osd),
1434 sizeof(struct ceph_entity_addr)) != 0)
1435 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001436 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001437}
1438
1439/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001440 * Requeue requests whose mapping to an OSD has changed. If requests map to
1441 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001442 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001443 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001444 */
Sage Weil38d64532011-10-14 13:33:55 -07001445static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001446{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001447 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001448 struct rb_node *p;
1449 int needmap = 0;
1450 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001451
Sage Weil38d64532011-10-14 13:33:55 -07001452 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001453 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001454 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001455 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001456 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001457
1458 /*
1459 * For linger requests that have not yet been
1460 * registered, move them to the linger list; they'll
1461 * be sent to the osd in the loop below. Unregister
1462 * the request before re-registering it as a linger
1463 * request to ensure the __map_request() below
1464 * will decide it needs to be sent.
1465 */
1466 if (req->r_linger && list_empty(&req->r_linger_item)) {
1467 dout("%p tid %llu restart on osd%d\n",
1468 req, req->r_tid,
1469 req->r_osd ? req->r_osd->o_osd : -1);
1470 __unregister_request(osdc, req);
1471 __register_linger_request(osdc, req);
1472 continue;
1473 }
1474
Sage Weil38d64532011-10-14 13:33:55 -07001475 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001476 if (err < 0)
1477 continue; /* error */
1478 if (req->r_osd == NULL) {
1479 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1480 needmap++; /* request a newer map */
1481 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001482 if (!req->r_linger) {
1483 dout("%p tid %llu requeued on osd%d\n", req,
1484 req->r_tid,
1485 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001486 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001487 }
1488 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001489 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001490
1491 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1492 r_linger_item) {
1493 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1494
Sage Weil38d64532011-10-14 13:33:55 -07001495 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001496 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001497 if (err == 0)
1498 continue; /* no change and no osd was specified */
1499 if (err < 0)
1500 continue; /* hrm! */
1501 if (req->r_osd == NULL) {
1502 dout("tid %llu maps to no valid osd\n", req->r_tid);
1503 needmap++; /* request a newer map */
1504 continue;
1505 }
1506
1507 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1508 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001509 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001510 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001511 }
Sage Weilf24e9982009-10-06 11:31:10 -07001512 mutex_unlock(&osdc->request_mutex);
1513
1514 if (needmap) {
1515 dout("%d requests for down osds, need new map\n", needmap);
1516 ceph_monc_request_next_osdmap(&osdc->client->monc);
1517 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001518 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001519}
Sage Weil6f6c7002011-01-17 20:34:08 -08001520
1521
Sage Weilf24e9982009-10-06 11:31:10 -07001522/*
1523 * Process updated osd map.
1524 *
1525 * The message contains any number of incremental and full maps, normally
1526 * indicating some sort of topology change in the cluster. Kick requests
1527 * off to different OSDs as needed.
1528 */
1529void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1530{
1531 void *p, *end, *next;
1532 u32 nr_maps, maplen;
1533 u32 epoch;
1534 struct ceph_osdmap *newmap = NULL, *oldmap;
1535 int err;
1536 struct ceph_fsid fsid;
1537
1538 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1539 p = msg->front.iov_base;
1540 end = p + msg->front.iov_len;
1541
1542 /* verify fsid */
1543 ceph_decode_need(&p, end, sizeof(fsid), bad);
1544 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001545 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1546 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001547
1548 down_write(&osdc->map_sem);
1549
1550 /* incremental maps */
1551 ceph_decode_32_safe(&p, end, nr_maps, bad);
1552 dout(" %d inc maps\n", nr_maps);
1553 while (nr_maps > 0) {
1554 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001555 epoch = ceph_decode_32(&p);
1556 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001557 ceph_decode_need(&p, end, maplen, bad);
1558 next = p + maplen;
1559 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1560 dout("applying incremental map %u len %d\n",
1561 epoch, maplen);
1562 newmap = osdmap_apply_incremental(&p, next,
1563 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001564 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001565 if (IS_ERR(newmap)) {
1566 err = PTR_ERR(newmap);
1567 goto bad;
1568 }
Sage Weil30dc6382009-12-21 14:49:37 -08001569 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001570 if (newmap != osdc->osdmap) {
1571 ceph_osdmap_destroy(osdc->osdmap);
1572 osdc->osdmap = newmap;
1573 }
Sage Weil38d64532011-10-14 13:33:55 -07001574 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001575 } else {
1576 dout("ignoring incremental map %u len %d\n",
1577 epoch, maplen);
1578 }
1579 p = next;
1580 nr_maps--;
1581 }
1582 if (newmap)
1583 goto done;
1584
1585 /* full maps */
1586 ceph_decode_32_safe(&p, end, nr_maps, bad);
1587 dout(" %d full maps\n", nr_maps);
1588 while (nr_maps) {
1589 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001590 epoch = ceph_decode_32(&p);
1591 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001592 ceph_decode_need(&p, end, maplen, bad);
1593 if (nr_maps > 1) {
1594 dout("skipping non-latest full map %u len %d\n",
1595 epoch, maplen);
1596 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1597 dout("skipping full map %u len %d, "
1598 "older than our %u\n", epoch, maplen,
1599 osdc->osdmap->epoch);
1600 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001601 int skipped_map = 0;
1602
Sage Weilf24e9982009-10-06 11:31:10 -07001603 dout("taking full map %u len %d\n", epoch, maplen);
1604 newmap = osdmap_decode(&p, p+maplen);
1605 if (IS_ERR(newmap)) {
1606 err = PTR_ERR(newmap);
1607 goto bad;
1608 }
Sage Weil30dc6382009-12-21 14:49:37 -08001609 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001610 oldmap = osdc->osdmap;
1611 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001612 if (oldmap) {
1613 if (oldmap->epoch + 1 < newmap->epoch)
1614 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001615 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001616 }
1617 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001618 }
1619 p += maplen;
1620 nr_maps--;
1621 }
1622
1623done:
1624 downgrade_write(&osdc->map_sem);
1625 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001626
1627 /*
1628 * subscribe to subsequent osdmap updates if full to ensure
1629 * we find out when we are no longer full and stop returning
1630 * ENOSPC.
1631 */
1632 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1633 ceph_monc_request_next_osdmap(&osdc->client->monc);
1634
Alex Elderf9d25192013-02-15 11:42:29 -06001635 mutex_lock(&osdc->request_mutex);
1636 __send_queued(osdc);
1637 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001638 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001639 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001640 return;
1641
1642bad:
1643 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001644 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001645 up_write(&osdc->map_sem);
1646 return;
1647}
1648
Sage Weilf24e9982009-10-06 11:31:10 -07001649/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001650 * watch/notify callback event infrastructure
1651 *
1652 * These callbacks are used both for watch and notify operations.
1653 */
1654static void __release_event(struct kref *kref)
1655{
1656 struct ceph_osd_event *event =
1657 container_of(kref, struct ceph_osd_event, kref);
1658
1659 dout("__release_event %p\n", event);
1660 kfree(event);
1661}
1662
1663static void get_event(struct ceph_osd_event *event)
1664{
1665 kref_get(&event->kref);
1666}
1667
1668void ceph_osdc_put_event(struct ceph_osd_event *event)
1669{
1670 kref_put(&event->kref, __release_event);
1671}
1672EXPORT_SYMBOL(ceph_osdc_put_event);
1673
1674static void __insert_event(struct ceph_osd_client *osdc,
1675 struct ceph_osd_event *new)
1676{
1677 struct rb_node **p = &osdc->event_tree.rb_node;
1678 struct rb_node *parent = NULL;
1679 struct ceph_osd_event *event = NULL;
1680
1681 while (*p) {
1682 parent = *p;
1683 event = rb_entry(parent, struct ceph_osd_event, node);
1684 if (new->cookie < event->cookie)
1685 p = &(*p)->rb_left;
1686 else if (new->cookie > event->cookie)
1687 p = &(*p)->rb_right;
1688 else
1689 BUG();
1690 }
1691
1692 rb_link_node(&new->node, parent, p);
1693 rb_insert_color(&new->node, &osdc->event_tree);
1694}
1695
1696static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1697 u64 cookie)
1698{
1699 struct rb_node **p = &osdc->event_tree.rb_node;
1700 struct rb_node *parent = NULL;
1701 struct ceph_osd_event *event = NULL;
1702
1703 while (*p) {
1704 parent = *p;
1705 event = rb_entry(parent, struct ceph_osd_event, node);
1706 if (cookie < event->cookie)
1707 p = &(*p)->rb_left;
1708 else if (cookie > event->cookie)
1709 p = &(*p)->rb_right;
1710 else
1711 return event;
1712 }
1713 return NULL;
1714}
1715
1716static void __remove_event(struct ceph_osd_event *event)
1717{
1718 struct ceph_osd_client *osdc = event->osdc;
1719
1720 if (!RB_EMPTY_NODE(&event->node)) {
1721 dout("__remove_event removed %p\n", event);
1722 rb_erase(&event->node, &osdc->event_tree);
1723 ceph_osdc_put_event(event);
1724 } else {
1725 dout("__remove_event didn't remove %p\n", event);
1726 }
1727}
1728
1729int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1730 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001731 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001732{
1733 struct ceph_osd_event *event;
1734
1735 event = kmalloc(sizeof(*event), GFP_NOIO);
1736 if (!event)
1737 return -ENOMEM;
1738
1739 dout("create_event %p\n", event);
1740 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001741 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001742 event->data = data;
1743 event->osdc = osdc;
1744 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001745 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001746 kref_init(&event->kref); /* one ref for us */
1747 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001748
1749 spin_lock(&osdc->event_lock);
1750 event->cookie = ++osdc->event_count;
1751 __insert_event(osdc, event);
1752 spin_unlock(&osdc->event_lock);
1753
1754 *pevent = event;
1755 return 0;
1756}
1757EXPORT_SYMBOL(ceph_osdc_create_event);
1758
1759void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1760{
1761 struct ceph_osd_client *osdc = event->osdc;
1762
1763 dout("cancel_event %p\n", event);
1764 spin_lock(&osdc->event_lock);
1765 __remove_event(event);
1766 spin_unlock(&osdc->event_lock);
1767 ceph_osdc_put_event(event); /* caller's */
1768}
1769EXPORT_SYMBOL(ceph_osdc_cancel_event);
1770
1771
1772static void do_event_work(struct work_struct *work)
1773{
1774 struct ceph_osd_event_work *event_work =
1775 container_of(work, struct ceph_osd_event_work, work);
1776 struct ceph_osd_event *event = event_work->event;
1777 u64 ver = event_work->ver;
1778 u64 notify_id = event_work->notify_id;
1779 u8 opcode = event_work->opcode;
1780
1781 dout("do_event_work completing %p\n", event);
1782 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001783 dout("do_event_work completed %p\n", event);
1784 ceph_osdc_put_event(event);
1785 kfree(event_work);
1786}
1787
1788
1789/*
1790 * Process osd watch notifications
1791 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001792static void handle_watch_notify(struct ceph_osd_client *osdc,
1793 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001794{
1795 void *p, *end;
1796 u8 proto_ver;
1797 u64 cookie, ver, notify_id;
1798 u8 opcode;
1799 struct ceph_osd_event *event;
1800 struct ceph_osd_event_work *event_work;
1801
1802 p = msg->front.iov_base;
1803 end = p + msg->front.iov_len;
1804
1805 ceph_decode_8_safe(&p, end, proto_ver, bad);
1806 ceph_decode_8_safe(&p, end, opcode, bad);
1807 ceph_decode_64_safe(&p, end, cookie, bad);
1808 ceph_decode_64_safe(&p, end, ver, bad);
1809 ceph_decode_64_safe(&p, end, notify_id, bad);
1810
1811 spin_lock(&osdc->event_lock);
1812 event = __find_event(osdc, cookie);
1813 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001814 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001815 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001816 }
1817 spin_unlock(&osdc->event_lock);
1818 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1819 cookie, ver, event);
1820 if (event) {
1821 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001822 if (!event_work) {
1823 dout("ERROR: could not allocate event_work\n");
1824 goto done_err;
1825 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001826 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001827 event_work->event = event;
1828 event_work->ver = ver;
1829 event_work->notify_id = notify_id;
1830 event_work->opcode = opcode;
1831 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1832 dout("WARNING: failed to queue notify event work\n");
1833 goto done_err;
1834 }
1835 }
1836
1837 return;
1838
1839done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001840 ceph_osdc_put_event(event);
1841 return;
1842
1843bad:
1844 pr_err("osdc handle_watch_notify corrupt msg\n");
1845 return;
1846}
1847
Alex Elder70636772013-03-04 18:29:06 -06001848static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1849 struct ceph_osd_data *osd_data)
1850{
1851 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1852 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1853 if (osd_data->length)
1854 ceph_msg_data_set_pages(msg, osd_data->pages,
1855 osd_data->length, osd_data->alignment);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001856 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1857 BUG_ON(!osd_data->pagelist->length);
1858 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
Alex Elder70636772013-03-04 18:29:06 -06001859#ifdef CONFIG_BLOCK
1860 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Eldera1930802013-03-14 14:09:06 -05001861 ceph_msg_data_set_bio(msg, osd_data->bio, osd_data->bio_length);
Alex Elder70636772013-03-04 18:29:06 -06001862#endif
1863 } else {
1864 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1865 }
1866}
1867
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001868/*
Sage Weilf24e9982009-10-06 11:31:10 -07001869 * Register request, send initial attempt.
1870 */
1871int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1872 struct ceph_osd_request *req,
1873 bool nofail)
1874{
Sage Weilc1ea8822009-10-08 16:55:47 -07001875 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001876
Alex Elder70636772013-03-04 18:29:06 -06001877 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001878
Alex Elder70636772013-03-04 18:29:06 -06001879 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1880 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Sage Weilf24e9982009-10-06 11:31:10 -07001881
Sage Weilf24e9982009-10-06 11:31:10 -07001882 down_read(&osdc->map_sem);
1883 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05001884 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05001885 WARN_ON(req->r_sent);
1886 rc = __map_request(osdc, req, 0);
1887 if (rc < 0) {
1888 if (nofail) {
1889 dout("osdc_start_request failed map, "
1890 " will retry %lld\n", req->r_tid);
1891 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001892 }
Alex Elder92451b42013-03-25 18:16:11 -05001893 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001894 }
Alex Elder92451b42013-03-25 18:16:11 -05001895 if (req->r_osd == NULL) {
1896 dout("send_request %p no up osds in pg\n", req);
1897 ceph_monc_request_next_osdmap(&osdc->client->monc);
1898 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05001899 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05001900 }
1901 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03001902out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001903 mutex_unlock(&osdc->request_mutex);
1904 up_read(&osdc->map_sem);
1905 return rc;
1906}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001907EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001908
1909/*
1910 * wait for a request to complete
1911 */
1912int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1913 struct ceph_osd_request *req)
1914{
1915 int rc;
1916
1917 rc = wait_for_completion_interruptible(&req->r_completion);
1918 if (rc < 0) {
1919 mutex_lock(&osdc->request_mutex);
1920 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001921 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001922 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001923 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001924 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001925 return rc;
1926 }
1927
1928 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1929 return req->r_result;
1930}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001931EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001932
1933/*
1934 * sync - wait for all in-flight requests to flush. avoid starvation.
1935 */
1936void ceph_osdc_sync(struct ceph_osd_client *osdc)
1937{
1938 struct ceph_osd_request *req;
1939 u64 last_tid, next_tid = 0;
1940
1941 mutex_lock(&osdc->request_mutex);
1942 last_tid = osdc->last_tid;
1943 while (1) {
1944 req = __lookup_request_ge(osdc, next_tid);
1945 if (!req)
1946 break;
1947 if (req->r_tid > last_tid)
1948 break;
1949
1950 next_tid = req->r_tid + 1;
1951 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1952 continue;
1953
1954 ceph_osdc_get_request(req);
1955 mutex_unlock(&osdc->request_mutex);
1956 dout("sync waiting on tid %llu (last is %llu)\n",
1957 req->r_tid, last_tid);
1958 wait_for_completion(&req->r_safe_completion);
1959 mutex_lock(&osdc->request_mutex);
1960 ceph_osdc_put_request(req);
1961 }
1962 mutex_unlock(&osdc->request_mutex);
1963 dout("sync done (thru tid %llu)\n", last_tid);
1964}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001965EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001966
1967/*
1968 * init, shutdown
1969 */
1970int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1971{
1972 int err;
1973
1974 dout("init\n");
1975 osdc->client = client;
1976 osdc->osdmap = NULL;
1977 init_rwsem(&osdc->map_sem);
1978 init_completion(&osdc->map_waiters);
1979 osdc->last_requested_map = 0;
1980 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001981 osdc->last_tid = 0;
1982 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001983 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001984 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001985 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001986 INIT_LIST_HEAD(&osdc->req_unsent);
1987 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001988 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001989 osdc->num_requests = 0;
1990 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001991 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001992 spin_lock_init(&osdc->event_lock);
1993 osdc->event_tree = RB_ROOT;
1994 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001995
1996 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001997 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001998
Sage Weil5f44f142009-11-18 14:52:18 -08001999 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002000 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2001 sizeof(struct ceph_osd_request));
2002 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002003 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002004
Sage Weild50b4092012-07-09 14:22:34 -07002005 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2006 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002007 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002008 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002009 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002010 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002011 OSD_OPREPLY_FRONT_LEN, 10, true,
2012 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002013 if (err < 0)
2014 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002015
2016 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2017 if (IS_ERR(osdc->notify_wq)) {
2018 err = PTR_ERR(osdc->notify_wq);
2019 osdc->notify_wq = NULL;
2020 goto out_msgpool;
2021 }
Sage Weilf24e9982009-10-06 11:31:10 -07002022 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002023
Sage Weilc16e7862010-03-01 13:02:00 -08002024out_msgpool:
2025 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002026out_mempool:
2027 mempool_destroy(osdc->req_mempool);
2028out:
2029 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002030}
2031
2032void ceph_osdc_stop(struct ceph_osd_client *osdc)
2033{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002034 flush_workqueue(osdc->notify_wq);
2035 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002036 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002037 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002038 if (osdc->osdmap) {
2039 ceph_osdmap_destroy(osdc->osdmap);
2040 osdc->osdmap = NULL;
2041 }
Sage Weilaca420b2011-08-31 14:45:53 -07002042 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002043 mempool_destroy(osdc->req_mempool);
2044 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002045 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002046}
2047
2048/*
2049 * Read some contiguous pages. If we cross a stripe boundary, shorten
2050 * *plen. Return number of bytes read, or error.
2051 */
2052int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2053 struct ceph_vino vino, struct ceph_file_layout *layout,
2054 u64 off, u64 *plen,
2055 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002056 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002057{
2058 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002059 struct ceph_osd_data *osd_data;
Alex Elderacead002013-03-14 14:09:05 -05002060 struct ceph_osd_req_op op;
Sage Weilf24e9982009-10-06 11:31:10 -07002061 int rc = 0;
2062
2063 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2064 vino.snap, off, *plen);
Alex Elderacead002013-03-14 14:09:05 -05002065 req = ceph_osdc_new_request(osdc, layout, vino, off, plen, 1, &op,
Sage Weilf24e9982009-10-06 11:31:10 -07002066 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
Alex Elderacead002013-03-14 14:09:05 -05002067 NULL, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002068 false);
Sage Weil68162822012-09-24 21:01:02 -07002069 if (IS_ERR(req))
2070 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002071
2072 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002073
2074 osd_data = &req->r_data_in;
2075 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2076 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002077 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06002078 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07002079
Alex Eldere0c59482013-03-07 15:38:25 -06002080 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2081 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002082
Alex Elder02ee07d2013-03-14 14:09:06 -05002083 ceph_osdc_build_request(req, off, 1, &op, NULL, vino.snap, NULL);
2084
Sage Weilf24e9982009-10-06 11:31:10 -07002085 rc = ceph_osdc_start_request(osdc, req, false);
2086 if (!rc)
2087 rc = ceph_osdc_wait_request(osdc, req);
2088
2089 ceph_osdc_put_request(req);
2090 dout("readpages result %d\n", rc);
2091 return rc;
2092}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002093EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002094
2095/*
2096 * do a synchronous write on N pages
2097 */
2098int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2099 struct ceph_file_layout *layout,
2100 struct ceph_snap_context *snapc,
2101 u64 off, u64 len,
2102 u32 truncate_seq, u64 truncate_size,
2103 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002104 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002105{
2106 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002107 struct ceph_osd_data *osd_data;
Alex Elderacead002013-03-14 14:09:05 -05002108 struct ceph_osd_req_op op;
Sage Weilf24e9982009-10-06 11:31:10 -07002109 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002110 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002111
Alex Elderacead002013-03-14 14:09:05 -05002112 BUG_ON(vino.snap != CEPH_NOSNAP); /* snapshots aren't writeable */
2113 req = ceph_osdc_new_request(osdc, layout, vino, off, &len, 1, &op,
Sage Weilf24e9982009-10-06 11:31:10 -07002114 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002115 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderacead002013-03-14 14:09:05 -05002116 snapc, truncate_seq, truncate_size,
Alex Elder153e5162013-03-01 18:00:15 -06002117 true);
Sage Weil68162822012-09-24 21:01:02 -07002118 if (IS_ERR(req))
2119 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002120
2121 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002122 osd_data = &req->r_data_out;
2123 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2124 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002125 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002126 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002127 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002128
Alex Elder02ee07d2013-03-14 14:09:06 -05002129 ceph_osdc_build_request(req, off, 1, &op, snapc, CEPH_NOSNAP, mtime);
2130
Alex Elder87f979d2013-02-15 11:42:29 -06002131 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002132 if (!rc)
2133 rc = ceph_osdc_wait_request(osdc, req);
2134
2135 ceph_osdc_put_request(req);
2136 if (rc == 0)
2137 rc = len;
2138 dout("writepages result %d\n", rc);
2139 return rc;
2140}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002141EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002142
2143/*
2144 * handle incoming message
2145 */
2146static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2147{
2148 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002149 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002150 int type = le16_to_cpu(msg->hdr.type);
2151
2152 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002153 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002154 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002155
2156 switch (type) {
2157 case CEPH_MSG_OSD_MAP:
2158 ceph_osdc_handle_map(osdc, msg);
2159 break;
2160 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002161 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002162 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002163 case CEPH_MSG_WATCH_NOTIFY:
2164 handle_watch_notify(osdc, msg);
2165 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002166
2167 default:
2168 pr_err("received unknown message type %d %s\n", type,
2169 ceph_msg_type_name(type));
2170 }
Sage Weil4a32f932010-06-13 10:27:53 -07002171out:
Sage Weilf24e9982009-10-06 11:31:10 -07002172 ceph_msg_put(msg);
2173}
2174
Sage Weil5b3a4db2010-02-19 21:43:23 -08002175/*
Sage Weil21b667f2010-03-04 10:22:59 -08002176 * lookup and return message for incoming reply. set up reply message
2177 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002178 */
2179static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002180 struct ceph_msg_header *hdr,
2181 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002182{
2183 struct ceph_osd *osd = con->private;
2184 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002185 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002186 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002187 int front = le32_to_cpu(hdr->front_len);
2188 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002189 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002190
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002191 tid = le64_to_cpu(hdr->tid);
2192 mutex_lock(&osdc->request_mutex);
2193 req = __lookup_request(osdc, tid);
2194 if (!req) {
2195 *skip = 1;
2196 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002197 dout("get_reply unknown tid %llu from osd%d\n", tid,
2198 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002199 goto out;
2200 }
Sage Weilc16e7862010-03-01 13:02:00 -08002201
Alex Elderace6d3a2013-04-01 16:12:14 -05002202 if (req->r_reply->con)
Alex Elder8921d112012-06-01 14:56:43 -05002203 dout("%s revoking msg %p from old con %p\n", __func__,
Alex Elderace6d3a2013-04-01 16:12:14 -05002204 req->r_reply, req->r_reply->con);
2205 ceph_msg_revoke_incoming(req->r_reply);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002206
Sage Weilc16e7862010-03-01 13:02:00 -08002207 if (front > req->r_reply->front.iov_len) {
2208 pr_warning("get_reply front %d > preallocated %d\n",
2209 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002210 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002211 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002212 goto out;
2213 ceph_msg_put(req->r_reply);
2214 req->r_reply = m;
2215 }
2216 m = ceph_msg_get(req->r_reply);
2217
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002218 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002219 struct ceph_osd_data *osd_data = &req->r_data_in;
2220
2221 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002222 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002223 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002224
Alex Eldere0c59482013-03-07 15:38:25 -06002225 pr_warning("tid %lld reply has %d bytes "
2226 "we had only %llu bytes ready\n",
2227 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002228 *skip = 1;
2229 ceph_msg_put(m);
2230 m = NULL;
2231 goto out;
2232 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002233 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002234 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002235 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002236 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002237
2238out:
2239 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002240 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002241
2242}
2243
2244static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2245 struct ceph_msg_header *hdr,
2246 int *skip)
2247{
2248 struct ceph_osd *osd = con->private;
2249 int type = le16_to_cpu(hdr->type);
2250 int front = le32_to_cpu(hdr->front_len);
2251
Alex Elder1c20f2d2012-06-04 14:43:32 -05002252 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002253 switch (type) {
2254 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002255 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002256 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002257 case CEPH_MSG_OSD_OPREPLY:
2258 return get_reply(con, hdr, skip);
2259 default:
2260 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2261 osd->o_osd);
2262 *skip = 1;
2263 return NULL;
2264 }
Sage Weilf24e9982009-10-06 11:31:10 -07002265}
2266
2267/*
2268 * Wrappers to refcount containing ceph_osd struct
2269 */
2270static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2271{
2272 struct ceph_osd *osd = con->private;
2273 if (get_osd(osd))
2274 return con;
2275 return NULL;
2276}
2277
2278static void put_osd_con(struct ceph_connection *con)
2279{
2280 struct ceph_osd *osd = con->private;
2281 put_osd(osd);
2282}
2283
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002284/*
2285 * authentication
2286 */
Alex Eldera3530df2012-05-16 15:16:39 -05002287/*
2288 * Note: returned pointer is the address of a structure that's
2289 * managed separately. Caller must *not* attempt to free it.
2290 */
2291static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002292 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002293{
2294 struct ceph_osd *o = con->private;
2295 struct ceph_osd_client *osdc = o->o_osdc;
2296 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002297 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002298
Alex Elder74f18692012-05-16 15:16:39 -05002299 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002300 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002301 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002302 }
Sage Weil27859f92013-03-25 10:26:14 -07002303 if (!auth->authorizer) {
2304 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2305 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002306 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002307 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002308 } else {
2309 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002310 auth);
2311 if (ret)
2312 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002313 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002314 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002315
Alex Eldera3530df2012-05-16 15:16:39 -05002316 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002317}
2318
2319
2320static int verify_authorizer_reply(struct ceph_connection *con, int len)
2321{
2322 struct ceph_osd *o = con->private;
2323 struct ceph_osd_client *osdc = o->o_osdc;
2324 struct ceph_auth_client *ac = osdc->client->monc.auth;
2325
Sage Weil27859f92013-03-25 10:26:14 -07002326 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002327}
2328
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002329static int invalidate_authorizer(struct ceph_connection *con)
2330{
2331 struct ceph_osd *o = con->private;
2332 struct ceph_osd_client *osdc = o->o_osdc;
2333 struct ceph_auth_client *ac = osdc->client->monc.auth;
2334
Sage Weil27859f92013-03-25 10:26:14 -07002335 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002336 return ceph_monc_validate_auth(&osdc->client->monc);
2337}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002338
Tobias Klauser9e327892010-05-20 10:40:19 +02002339static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002340 .get = get_osd_con,
2341 .put = put_osd_con,
2342 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002343 .get_authorizer = get_authorizer,
2344 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002345 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002346 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002347 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002348};