blob: 0eb417b4419593f12f009d05a1e56c814b18500d [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
Yehuda Sadeh68b44762010-04-06 15:01:27 -070035static int op_has_extent(int op)
36{
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
39}
40
Sage Weilf24e9982009-10-06 11:31:10 -070041/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Eldera19dadf2013-03-13 20:50:01 -050067 u64 *objnum, u64 *objoff, u64 *objlen)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Sage Weild63b77f2012-09-24 20:59:48 -070070 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070071
Alex Elder60e56f12013-02-15 11:42:29 -060072 /* object extent? */
Alex Elder75d1c942013-03-13 20:50:00 -050073 r = ceph_calc_file_object_mapping(layout, off, orig_len, objnum,
74 objoff, objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070075 if (r < 0)
76 return r;
Alex Elder75d1c942013-03-13 20:50:00 -050077 if (*objlen < orig_len) {
78 *plen = *objlen;
Alex Elder60e56f12013-02-15 11:42:29 -060079 dout(" skipping last %llu, final file extent %llu~%llu\n",
80 orig_len - *plen, off, *plen);
81 }
82
Alex Elder75d1c942013-03-13 20:50:00 -050083 dout("calc_layout objnum=%llx %llu~%llu\n", *objnum, *objoff, *objlen);
Sage Weilf24e9982009-10-06 11:31:10 -070084
Alex Elder3ff5f382013-02-15 22:10:17 -060085 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -070086}
87
Sage Weilf24e9982009-10-06 11:31:10 -070088/*
89 * requests
90 */
Sage Weil415e49a2009-12-07 13:37:03 -080091void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -070092{
Alex Eldere0c59482013-03-07 15:38:25 -060093 int num_pages;
Sage Weil415e49a2009-12-07 13:37:03 -080094 struct ceph_osd_request *req = container_of(kref,
95 struct ceph_osd_request,
96 r_kref);
97
98 if (req->r_request)
99 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800100 if (req->r_con_filling_msg) {
Alex Elder9cbb1d72013-01-31 16:02:00 -0600101 dout("%s revoking msg %p from con %p\n", __func__,
102 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500103 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700104 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Alex Elder9cbb1d72013-01-31 16:02:00 -0600105 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800106 }
Alex Elderab8cb342012-06-04 14:43:32 -0500107 if (req->r_reply)
108 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600109
110 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600111 req->r_data_in.own_pages) {
112 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
113 (u64)req->r_data_in.length);
114 ceph_release_page_vector(req->r_data_in.pages, num_pages);
115 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600116 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600117 req->r_data_out.own_pages) {
118 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
119 (u64)req->r_data_out.length);
120 ceph_release_page_vector(req->r_data_out.pages, num_pages);
121 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600122
Sage Weil415e49a2009-12-07 13:37:03 -0800123 ceph_put_snap_context(req->r_snapc);
124 if (req->r_mempool)
125 mempool_free(req, req->r_osdc->req_mempool);
126 else
127 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700128}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700129EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700130
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700131struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700132 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800133 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700134 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600135 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700136{
137 struct ceph_osd_request *req;
138 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800139 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700140
Sage Weil1b83bef2013-02-25 16:11:12 -0800141 msg_size = 4 + 4 + 8 + 8 + 4+8;
142 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
143 msg_size += 1 + 8 + 4 + 4; /* pg_t */
144 msg_size += 4 + MAX_OBJ_NAME_SIZE;
145 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
146 msg_size += 8; /* snapid */
147 msg_size += 8; /* snap_seq */
148 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
149 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700150
151 if (use_mempool) {
152 req = mempool_alloc(osdc->req_mempool, gfp_flags);
153 memset(req, 0, sizeof(*req));
154 } else {
155 req = kzalloc(sizeof(*req), gfp_flags);
156 }
157 if (req == NULL)
158 return NULL;
159
160 req->r_osdc = osdc;
161 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700162
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700163 kref_init(&req->r_kref);
164 init_completion(&req->r_completion);
165 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600166 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700167 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700168 INIT_LIST_HEAD(&req->r_linger_item);
169 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b639a2011-09-16 11:13:17 -0700170 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700171 INIT_LIST_HEAD(&req->r_osd_item);
172
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700173 /* create reply message */
174 if (use_mempool)
175 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
176 else
177 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700178 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700179 if (!msg) {
180 ceph_osdc_put_request(req);
181 return NULL;
182 }
183 req->r_reply = msg;
184
Alex Elder0fff87e2013-02-14 12:16:43 -0600185 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
186 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
Sage Weild50b4092012-07-09 14:22:34 -0700187
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700188 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700189 if (use_mempool)
190 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
191 else
Sage Weilb61c2762011-08-09 15:03:46 -0700192 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700193 if (!msg) {
194 ceph_osdc_put_request(req);
195 return NULL;
196 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700197
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700198 memset(msg->front.iov_base, 0, msg->front.iov_len);
199
200 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700201
202 return req;
203}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700204EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700205
Alex Eldera8dd0a32013-03-13 20:50:00 -0500206static bool osd_req_opcode_valid(u16 opcode)
207{
208 switch (opcode) {
209 case CEPH_OSD_OP_READ:
210 case CEPH_OSD_OP_STAT:
211 case CEPH_OSD_OP_MAPEXT:
212 case CEPH_OSD_OP_MASKTRUNC:
213 case CEPH_OSD_OP_SPARSE_READ:
214 case CEPH_OSD_OP_NOTIFY:
215 case CEPH_OSD_OP_NOTIFY_ACK:
216 case CEPH_OSD_OP_ASSERT_VER:
217 case CEPH_OSD_OP_WRITE:
218 case CEPH_OSD_OP_WRITEFULL:
219 case CEPH_OSD_OP_TRUNCATE:
220 case CEPH_OSD_OP_ZERO:
221 case CEPH_OSD_OP_DELETE:
222 case CEPH_OSD_OP_APPEND:
223 case CEPH_OSD_OP_STARTSYNC:
224 case CEPH_OSD_OP_SETTRUNC:
225 case CEPH_OSD_OP_TRIMTRUNC:
226 case CEPH_OSD_OP_TMAPUP:
227 case CEPH_OSD_OP_TMAPPUT:
228 case CEPH_OSD_OP_TMAPGET:
229 case CEPH_OSD_OP_CREATE:
230 case CEPH_OSD_OP_ROLLBACK:
231 case CEPH_OSD_OP_WATCH:
232 case CEPH_OSD_OP_OMAPGETKEYS:
233 case CEPH_OSD_OP_OMAPGETVALS:
234 case CEPH_OSD_OP_OMAPGETHEADER:
235 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
236 case CEPH_OSD_OP_OMAPSETVALS:
237 case CEPH_OSD_OP_OMAPSETHEADER:
238 case CEPH_OSD_OP_OMAPCLEAR:
239 case CEPH_OSD_OP_OMAPRMKEYS:
240 case CEPH_OSD_OP_OMAP_CMP:
241 case CEPH_OSD_OP_CLONERANGE:
242 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
243 case CEPH_OSD_OP_SRC_CMPXATTR:
244 case CEPH_OSD_OP_GETXATTR:
245 case CEPH_OSD_OP_GETXATTRS:
246 case CEPH_OSD_OP_CMPXATTR:
247 case CEPH_OSD_OP_SETXATTR:
248 case CEPH_OSD_OP_SETXATTRS:
249 case CEPH_OSD_OP_RESETXATTRS:
250 case CEPH_OSD_OP_RMXATTR:
251 case CEPH_OSD_OP_PULL:
252 case CEPH_OSD_OP_PUSH:
253 case CEPH_OSD_OP_BALANCEREADS:
254 case CEPH_OSD_OP_UNBALANCEREADS:
255 case CEPH_OSD_OP_SCRUB:
256 case CEPH_OSD_OP_SCRUB_RESERVE:
257 case CEPH_OSD_OP_SCRUB_UNRESERVE:
258 case CEPH_OSD_OP_SCRUB_STOP:
259 case CEPH_OSD_OP_SCRUB_MAP:
260 case CEPH_OSD_OP_WRLOCK:
261 case CEPH_OSD_OP_WRUNLOCK:
262 case CEPH_OSD_OP_RDLOCK:
263 case CEPH_OSD_OP_RDUNLOCK:
264 case CEPH_OSD_OP_UPLOCK:
265 case CEPH_OSD_OP_DNLOCK:
266 case CEPH_OSD_OP_CALL:
267 case CEPH_OSD_OP_PGLS:
268 case CEPH_OSD_OP_PGLS_FILTER:
269 return true;
270 default:
271 return false;
272 }
273}
274
Alex Elder33803f32013-03-13 20:50:00 -0500275/*
276 * This is an osd op init function for opcodes that have no data or
277 * other information associated with them. It also serves as a
278 * common init routine for all the other init functions, below.
279 */
280void osd_req_op_init(struct ceph_osd_req_op *op, u16 opcode)
281{
282 BUG_ON(!osd_req_opcode_valid(opcode));
283
284 memset(op, 0, sizeof (*op));
285
286 op->op = opcode;
287}
288
289void osd_req_op_extent_init(struct ceph_osd_req_op *op, u16 opcode,
290 u64 offset, u64 length,
291 u64 truncate_size, u32 truncate_seq)
292{
293 size_t payload_len = 0;
294
295 BUG_ON(opcode != CEPH_OSD_OP_READ && opcode != CEPH_OSD_OP_WRITE);
296
297 osd_req_op_init(op, opcode);
298
299 op->extent.offset = offset;
300 op->extent.length = length;
301 op->extent.truncate_size = truncate_size;
302 op->extent.truncate_seq = truncate_seq;
303 if (opcode == CEPH_OSD_OP_WRITE)
304 payload_len += length;
305
306 op->payload_len = payload_len;
307}
308EXPORT_SYMBOL(osd_req_op_extent_init);
309
310void osd_req_op_cls_init(struct ceph_osd_req_op *op, u16 opcode,
311 const char *class, const char *method,
312 const void *request_data, size_t request_data_size)
313{
314 size_t payload_len = 0;
315 size_t size;
316
317 BUG_ON(opcode != CEPH_OSD_OP_CALL);
318
319 osd_req_op_init(op, opcode);
320
321 op->cls.class_name = class;
322 size = strlen(class);
323 BUG_ON(size > (size_t) U8_MAX);
324 op->cls.class_len = size;
325 payload_len += size;
326
327 op->cls.method_name = method;
328 size = strlen(method);
329 BUG_ON(size > (size_t) U8_MAX);
330 op->cls.method_len = size;
331 payload_len += size;
332
333 op->cls.indata = request_data;
334 BUG_ON(request_data_size > (size_t) U32_MAX);
335 op->cls.indata_len = (u32) request_data_size;
336 payload_len += request_data_size;
337
338 op->cls.argc = 0; /* currently unused */
339
340 op->payload_len = payload_len;
341}
342EXPORT_SYMBOL(osd_req_op_cls_init);
343
344void osd_req_op_watch_init(struct ceph_osd_req_op *op, u16 opcode,
345 u64 cookie, u64 version, int flag)
346{
347 BUG_ON(opcode != CEPH_OSD_OP_NOTIFY_ACK && opcode != CEPH_OSD_OP_WATCH);
348
349 osd_req_op_init(op, opcode);
350
351 op->watch.cookie = cookie;
352 /* op->watch.ver = version; */ /* XXX 3847 */
353 op->watch.ver = cpu_to_le64(version);
354 if (opcode == CEPH_OSD_OP_WATCH && flag)
355 op->watch.flag = (u8) 1;
356}
357EXPORT_SYMBOL(osd_req_op_watch_init);
358
Alex Elder175face2013-03-08 13:35:36 -0600359static u64 osd_req_encode_op(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700360 struct ceph_osd_op *dst,
361 struct ceph_osd_req_op *src)
362{
Alex Elder175face2013-03-08 13:35:36 -0600363 u64 out_data_len = 0;
Alex Elder95e072e2013-03-08 13:35:36 -0600364 struct ceph_pagelist *pagelist;
Alex Elder175face2013-03-08 13:35:36 -0600365
Alex Eldera8dd0a32013-03-13 20:50:00 -0500366 if (WARN_ON(!osd_req_opcode_valid(src->op))) {
367 pr_err("unrecognized osd opcode %d\n", src->op);
368
369 return 0;
370 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700371
Alex Elder065a68f2012-04-20 15:49:43 -0500372 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600373 case CEPH_OSD_OP_STAT:
374 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700375 case CEPH_OSD_OP_READ:
376 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600377 if (src->op == CEPH_OSD_OP_WRITE)
378 out_data_len = src->extent.length;
379 dst->extent.offset = cpu_to_le64(src->extent.offset);
380 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700381 dst->extent.truncate_size =
382 cpu_to_le64(src->extent.truncate_size);
383 dst->extent.truncate_seq =
384 cpu_to_le32(src->extent.truncate_seq);
385 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700386 case CEPH_OSD_OP_CALL:
Alex Elder95e072e2013-03-08 13:35:36 -0600387 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
388 BUG_ON(!pagelist);
389 ceph_pagelist_init(pagelist);
390
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700391 dst->cls.class_len = src->cls.class_len;
392 dst->cls.method_len = src->cls.method_len;
393 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600394 ceph_pagelist_append(pagelist, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700395 src->cls.class_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600396 ceph_pagelist_append(pagelist, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700397 src->cls.method_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600398 ceph_pagelist_append(pagelist, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700399 src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600400
401 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
402 req->r_data_out.pagelist = pagelist;
403 out_data_len = pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700404 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700405 case CEPH_OSD_OP_STARTSYNC:
406 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700407 case CEPH_OSD_OP_NOTIFY_ACK:
408 case CEPH_OSD_OP_WATCH:
409 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
410 dst->watch.ver = cpu_to_le64(src->watch.ver);
411 dst->watch.flag = src->watch.flag;
412 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700413 default:
Alex Elder4c464592013-02-15 11:42:30 -0600414 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600415 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600416 WARN_ON(1);
Alex Eldera8dd0a32013-03-13 20:50:00 -0500417
418 return 0;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700419 }
Alex Eldera8dd0a32013-03-13 20:50:00 -0500420 dst->op = cpu_to_le16(src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700421 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600422
423 return out_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700424}
425
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700426/*
427 * build new request AND message
428 *
429 */
430void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder175face2013-03-08 13:35:36 -0600431 u64 off, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700432 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600433 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600434 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700435{
436 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700437 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700438 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800439 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700440 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600441 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700442 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700443
Sage Weil1b83bef2013-02-25 16:11:12 -0800444 req->r_num_ops = num_ops;
445 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700446 req->r_snapc = ceph_get_snap_context(snapc);
447
Sage Weil1b83bef2013-02-25 16:11:12 -0800448 /* encode request */
449 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700450
Sage Weil1b83bef2013-02-25 16:11:12 -0800451 p = msg->front.iov_base;
452 ceph_encode_32(&p, 1); /* client_inc is always 1 */
453 req->r_request_osdmap_epoch = p;
454 p += 4;
455 req->r_request_flags = p;
456 p += 4;
457 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
458 ceph_encode_timespec(p, mtime);
459 p += sizeof(struct ceph_timespec);
460 req->r_request_reassert_version = p;
461 p += sizeof(struct ceph_eversion); /* will get filled in */
462
463 /* oloc */
464 ceph_encode_8(&p, 4);
465 ceph_encode_8(&p, 4);
466 ceph_encode_32(&p, 8 + 4 + 4);
467 req->r_request_pool = p;
468 p += 8;
469 ceph_encode_32(&p, -1); /* preferred */
470 ceph_encode_32(&p, 0); /* key len */
471
472 ceph_encode_8(&p, 1);
473 req->r_request_pgid = p;
474 p += 8 + 4;
475 ceph_encode_32(&p, -1); /* preferred */
476
477 /* oid */
478 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600479 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800480 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600481 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700482
Alex Elder175face2013-03-08 13:35:36 -0600483 /* ops--can imply data */
Sage Weil1b83bef2013-02-25 16:11:12 -0800484 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700485 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800486 req->r_request_ops = p;
Alex Elder175face2013-03-08 13:35:36 -0600487 data_len = 0;
Sage Weil1b83bef2013-02-25 16:11:12 -0800488 for (i = 0; i < num_ops; i++, src_op++) {
Alex Elder175face2013-03-08 13:35:36 -0600489 data_len += osd_req_encode_op(req, p, src_op);
Sage Weil1b83bef2013-02-25 16:11:12 -0800490 p += sizeof(struct ceph_osd_op);
491 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700492
Sage Weil1b83bef2013-02-25 16:11:12 -0800493 /* snaps */
494 ceph_encode_64(&p, req->r_snapid);
495 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
496 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
497 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700498 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800499 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700500 }
501 }
502
Sage Weil1b83bef2013-02-25 16:11:12 -0800503 req->r_request_attempts = p;
504 p += 4;
505
Alex Elder175face2013-03-08 13:35:36 -0600506 /* data */
Alex Elder0baa1bd2013-03-29 14:28:03 -0500507 if (flags & CEPH_OSD_FLAG_WRITE) {
508 u16 data_off;
509
510 /*
511 * The header "data_off" is a hint to the receiver
512 * allowing it to align received data into its
513 * buffers such that there's no need to re-copy
514 * it before writing it to disk (direct I/O).
515 */
516 data_off = (u16) (off & 0xffff);
517 req->r_request->hdr.data_off = cpu_to_le16(data_off);
518 }
Alex Elderf44246e2013-02-14 12:16:43 -0600519 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800520
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700521 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
522 msg_size = p - msg->front.iov_base;
523 msg->front.iov_len = msg_size;
524 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800525
526 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
527 num_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700528 return;
529}
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,
546 u64 off, u64 *plen,
547 int opcode, int flags,
548 struct ceph_snap_context *snapc,
549 int do_sync,
550 u32 truncate_seq,
551 u64 truncate_size,
552 struct timespec *mtime,
Alex Elder153e5162013-03-01 18:00:15 -0600553 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700554{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600555 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700556 struct ceph_osd_request *req;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600557 unsigned int num_op = 1;
Alex Elder75d1c942013-03-13 20:50:00 -0500558 u64 objnum = 0;
559 u64 objoff = 0;
560 u64 objlen = 0;
Sage Weil68162822012-09-24 21:01:02 -0700561 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700562
Alex Elderae7ca4a32012-11-13 21:11:15 -0600563 memset(&ops, 0, sizeof ops);
564
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700565 ops[0].op = opcode;
566 ops[0].extent.truncate_seq = truncate_seq;
567 ops[0].extent.truncate_size = truncate_size;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700568
569 if (do_sync) {
570 ops[1].op = CEPH_OSD_OP_STARTSYNC;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600571 num_op++;
572 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700573
Alex Elderae7ca4a32012-11-13 21:11:15 -0600574 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
575 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700576 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700577 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600578 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700579
580 /* calculate max write size */
Alex Eldera19dadf2013-03-13 20:50:01 -0500581 r = calc_layout(layout, off, plen, &objnum, &objoff, &objlen);
Alex Elder3ff5f382013-02-15 22:10:17 -0600582 if (r < 0) {
583 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700584 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600585 }
Alex Eldera19dadf2013-03-13 20:50:01 -0500586
587 if (op_has_extent(ops[0].op)) {
588 u32 osize = le32_to_cpu(layout->fl_object_size);
589 ops[0].extent.offset = objoff;
590 ops[0].extent.length = objlen;
591 if (ops[0].extent.truncate_size <= off - objoff) {
592 ops[0].extent.truncate_size = 0;
593 } else {
594 ops[0].extent.truncate_size -= off - objoff;
595 if (ops[0].extent.truncate_size > osize)
596 ops[0].extent.truncate_size = osize;
597 }
598 }
599 if (ops[0].op == CEPH_OSD_OP_WRITE)
600 ops[0].payload_len = *plen;
601
Sage Weilf24e9982009-10-06 11:31:10 -0700602 req->r_file_layout = *layout; /* keep a copy */
603
Alex Elder75d1c942013-03-13 20:50:00 -0500604 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx",
605 vino.ino, objnum);
Alex Elderdbe0fc42013-02-15 22:10:17 -0600606 req->r_oid_len = strlen(req->r_oid);
607
Alex Elder175face2013-03-08 13:35:36 -0600608 ceph_osdc_build_request(req, off, num_op, ops,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600609 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700610
Sage Weilf24e9982009-10-06 11:31:10 -0700611 return req;
612}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700613EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700614
615/*
616 * We keep osd requests in an rbtree, sorted by ->r_tid.
617 */
618static void __insert_request(struct ceph_osd_client *osdc,
619 struct ceph_osd_request *new)
620{
621 struct rb_node **p = &osdc->requests.rb_node;
622 struct rb_node *parent = NULL;
623 struct ceph_osd_request *req = NULL;
624
625 while (*p) {
626 parent = *p;
627 req = rb_entry(parent, struct ceph_osd_request, r_node);
628 if (new->r_tid < req->r_tid)
629 p = &(*p)->rb_left;
630 else if (new->r_tid > req->r_tid)
631 p = &(*p)->rb_right;
632 else
633 BUG();
634 }
635
636 rb_link_node(&new->r_node, parent, p);
637 rb_insert_color(&new->r_node, &osdc->requests);
638}
639
640static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
641 u64 tid)
642{
643 struct ceph_osd_request *req;
644 struct rb_node *n = osdc->requests.rb_node;
645
646 while (n) {
647 req = rb_entry(n, struct ceph_osd_request, r_node);
648 if (tid < req->r_tid)
649 n = n->rb_left;
650 else if (tid > req->r_tid)
651 n = n->rb_right;
652 else
653 return req;
654 }
655 return NULL;
656}
657
658static struct ceph_osd_request *
659__lookup_request_ge(struct ceph_osd_client *osdc,
660 u64 tid)
661{
662 struct ceph_osd_request *req;
663 struct rb_node *n = osdc->requests.rb_node;
664
665 while (n) {
666 req = rb_entry(n, struct ceph_osd_request, r_node);
667 if (tid < req->r_tid) {
668 if (!n->rb_left)
669 return req;
670 n = n->rb_left;
671 } else if (tid > req->r_tid) {
672 n = n->rb_right;
673 } else {
674 return req;
675 }
676 }
677 return NULL;
678}
679
Sage Weil6f6c7002011-01-17 20:34:08 -0800680/*
681 * Resubmit requests pending on the given osd.
682 */
683static void __kick_osd_requests(struct ceph_osd_client *osdc,
684 struct ceph_osd *osd)
685{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700686 struct ceph_osd_request *req, *nreq;
Alex Eldere02493c2013-03-25 18:16:11 -0500687 LIST_HEAD(resend);
Sage Weil6f6c7002011-01-17 20:34:08 -0800688 int err;
689
690 dout("__kick_osd_requests osd%d\n", osd->o_osd);
691 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600692 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800693 return;
Alex Eldere02493c2013-03-25 18:16:11 -0500694 /*
695 * Build up a list of requests to resend by traversing the
696 * osd's list of requests. Requests for a given object are
697 * sent in tid order, and that is also the order they're
698 * kept on this list. Therefore all requests that are in
699 * flight will be found first, followed by all requests that
700 * have not yet been sent. And to resend requests while
701 * preserving this order we will want to put any sent
702 * requests back on the front of the osd client's unsent
703 * list.
704 *
705 * So we build a separate ordered list of already-sent
706 * requests for the affected osd and splice it onto the
707 * front of the osd client's unsent list. Once we've seen a
708 * request that has not yet been sent we're done. Those
709 * requests are already sitting right where they belong.
710 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800711 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
Alex Eldere02493c2013-03-25 18:16:11 -0500712 if (!req->r_sent)
713 break;
714 list_move_tail(&req->r_req_lru_item, &resend);
715 dout("requeueing %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil6f6c7002011-01-17 20:34:08 -0800716 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700717 if (!req->r_linger)
718 req->r_flags |= CEPH_OSD_FLAG_RETRY;
719 }
Alex Eldere02493c2013-03-25 18:16:11 -0500720 list_splice(&resend, &osdc->req_unsent);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700721
Alex Eldere02493c2013-03-25 18:16:11 -0500722 /*
723 * Linger requests are re-registered before sending, which
724 * sets up a new tid for each. We add them to the unsent
725 * list at the end to keep things in tid order.
726 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700727 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
728 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700729 /*
730 * reregister request prior to unregistering linger so
731 * that r_osd is preserved.
732 */
733 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700734 __register_request(osdc, req);
Alex Eldere02493c2013-03-25 18:16:11 -0500735 list_add_tail(&req->r_req_lru_item, &osdc->req_unsent);
Alex Elderad885922013-03-25 18:16:11 -0500736 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil77f38e02011-04-06 09:09:16 -0700737 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700738 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
739 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800740 }
741}
742
Sage Weilf24e9982009-10-06 11:31:10 -0700743/*
Sage Weil81b024e2009-10-09 10:29:18 -0700744 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700745 */
746static void osd_reset(struct ceph_connection *con)
747{
748 struct ceph_osd *osd = con->private;
749 struct ceph_osd_client *osdc;
750
751 if (!osd)
752 return;
753 dout("osd_reset osd%d\n", osd->o_osd);
754 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700755 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800756 mutex_lock(&osdc->request_mutex);
757 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600758 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800759 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700760 up_read(&osdc->map_sem);
761}
762
763/*
764 * Track open sessions with osds.
765 */
Alex Eldere10006f2012-05-26 23:26:43 -0500766static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700767{
768 struct ceph_osd *osd;
769
770 osd = kzalloc(sizeof(*osd), GFP_NOFS);
771 if (!osd)
772 return NULL;
773
774 atomic_set(&osd->o_ref, 1);
775 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500776 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600777 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700778 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700779 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800780 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700781 osd->o_incarnation = 1;
782
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700783 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800784
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800785 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700786 return osd;
787}
788
789static struct ceph_osd *get_osd(struct ceph_osd *osd)
790{
791 if (atomic_inc_not_zero(&osd->o_ref)) {
792 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
793 atomic_read(&osd->o_ref));
794 return osd;
795 } else {
796 dout("get_osd %p FAIL\n", osd);
797 return NULL;
798 }
799}
800
801static void put_osd(struct ceph_osd *osd)
802{
803 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
804 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500805 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700806 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
807
Sage Weil27859f92013-03-25 10:26:14 -0700808 ceph_auth_destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700809 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700810 }
Sage Weilf24e9982009-10-06 11:31:10 -0700811}
812
813/*
814 * remove an osd from our map
815 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800816static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700817{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800818 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700819 BUG_ON(!list_empty(&osd->o_requests));
820 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800821 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700822 ceph_con_close(&osd->o_con);
823 put_osd(osd);
824}
825
Sage Weilaca420b2011-08-31 14:45:53 -0700826static void remove_all_osds(struct ceph_osd_client *osdc)
827{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500828 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700829 mutex_lock(&osdc->request_mutex);
830 while (!RB_EMPTY_ROOT(&osdc->osds)) {
831 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
832 struct ceph_osd, o_node);
833 __remove_osd(osdc, osd);
834 }
835 mutex_unlock(&osdc->request_mutex);
836}
837
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800838static void __move_osd_to_lru(struct ceph_osd_client *osdc,
839 struct ceph_osd *osd)
840{
841 dout("__move_osd_to_lru %p\n", osd);
842 BUG_ON(!list_empty(&osd->o_osd_lru));
843 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700844 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800845}
846
847static void __remove_osd_from_lru(struct ceph_osd *osd)
848{
849 dout("__remove_osd_from_lru %p\n", osd);
850 if (!list_empty(&osd->o_osd_lru))
851 list_del_init(&osd->o_osd_lru);
852}
853
Sage Weilaca420b2011-08-31 14:45:53 -0700854static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800855{
856 struct ceph_osd *osd, *nosd;
857
858 dout("__remove_old_osds %p\n", osdc);
859 mutex_lock(&osdc->request_mutex);
860 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700861 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800862 break;
863 __remove_osd(osdc, osd);
864 }
865 mutex_unlock(&osdc->request_mutex);
866}
867
Sage Weilf24e9982009-10-06 11:31:10 -0700868/*
869 * reset osd connect
870 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800871static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700872{
Alex Elderc3acb182012-12-07 09:57:58 -0600873 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700874
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800875 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700876 if (list_empty(&osd->o_requests) &&
877 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800878 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600879
880 return -ENODEV;
881 }
882
883 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
884 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
885 !ceph_con_opened(&osd->o_con)) {
886 struct ceph_osd_request *req;
887
Sage Weil87b315a2010-03-22 14:51:18 -0700888 dout(" osd addr hasn't changed and connection never opened,"
889 " letting msgr retry");
890 /* touch each r_stamp for handle_timeout()'s benfit */
891 list_for_each_entry(req, &osd->o_requests, r_osd_item)
892 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600893
894 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700895 }
Alex Elderc3acb182012-12-07 09:57:58 -0600896
897 ceph_con_close(&osd->o_con);
898 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
899 osd->o_incarnation++;
900
901 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700902}
903
904static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
905{
906 struct rb_node **p = &osdc->osds.rb_node;
907 struct rb_node *parent = NULL;
908 struct ceph_osd *osd = NULL;
909
Sage Weilaca420b2011-08-31 14:45:53 -0700910 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700911 while (*p) {
912 parent = *p;
913 osd = rb_entry(parent, struct ceph_osd, o_node);
914 if (new->o_osd < osd->o_osd)
915 p = &(*p)->rb_left;
916 else if (new->o_osd > osd->o_osd)
917 p = &(*p)->rb_right;
918 else
919 BUG();
920 }
921
922 rb_link_node(&new->o_node, parent, p);
923 rb_insert_color(&new->o_node, &osdc->osds);
924}
925
926static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
927{
928 struct ceph_osd *osd;
929 struct rb_node *n = osdc->osds.rb_node;
930
931 while (n) {
932 osd = rb_entry(n, struct ceph_osd, o_node);
933 if (o < osd->o_osd)
934 n = n->rb_left;
935 else if (o > osd->o_osd)
936 n = n->rb_right;
937 else
938 return osd;
939 }
940 return NULL;
941}
942
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800943static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
944{
945 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700946 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800947}
948
949static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
950{
951 cancel_delayed_work(&osdc->timeout_work);
952}
Sage Weilf24e9982009-10-06 11:31:10 -0700953
954/*
955 * Register request, assign tid. If this is the first request, set up
956 * the timeout event.
957 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700958static void __register_request(struct ceph_osd_client *osdc,
959 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700960{
Sage Weilf24e9982009-10-06 11:31:10 -0700961 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800962 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700963 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700964 __insert_request(osdc, req);
965 ceph_osdc_get_request(req);
966 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700967 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800968 dout(" first request, scheduling timeout\n");
969 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700970 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700971}
972
Sage Weilf24e9982009-10-06 11:31:10 -0700973/*
974 * called under osdc->request_mutex
975 */
976static void __unregister_request(struct ceph_osd_client *osdc,
977 struct ceph_osd_request *req)
978{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500979 if (RB_EMPTY_NODE(&req->r_node)) {
980 dout("__unregister_request %p tid %lld not registered\n",
981 req, req->r_tid);
982 return;
983 }
984
Sage Weilf24e9982009-10-06 11:31:10 -0700985 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
986 rb_erase(&req->r_node, &osdc->requests);
987 osdc->num_requests--;
988
Sage Weil0ba64782009-10-08 16:57:16 -0700989 if (req->r_osd) {
990 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500991 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700992
993 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700994 if (list_empty(&req->r_osd->o_requests) &&
995 list_empty(&req->r_osd->o_linger_requests)) {
996 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800997 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700998 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700999 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001000 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -07001001 }
Sage Weilf24e9982009-10-06 11:31:10 -07001002
Alex Elder7d5f2482012-11-29 08:37:03 -06001003 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001004 ceph_osdc_put_request(req);
1005
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001006 if (osdc->num_requests == 0) {
1007 dout(" no requests, canceling timeout\n");
1008 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001009 }
1010}
1011
1012/*
1013 * Cancel a previously queued request message
1014 */
1015static void __cancel_request(struct ceph_osd_request *req)
1016{
Sage Weil6bc18872010-09-27 10:18:52 -07001017 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -05001018 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 req->r_sent = 0;
1020 }
1021}
1022
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001023static void __register_linger_request(struct ceph_osd_client *osdc,
1024 struct ceph_osd_request *req)
1025{
1026 dout("__register_linger_request %p\n", req);
1027 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -07001028 if (req->r_osd)
1029 list_add_tail(&req->r_linger_osd,
1030 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001031}
1032
1033static void __unregister_linger_request(struct ceph_osd_client *osdc,
1034 struct ceph_osd_request *req)
1035{
1036 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -06001037 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001038 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001039 list_del_init(&req->r_linger_osd);
1040
1041 if (list_empty(&req->r_osd->o_requests) &&
1042 list_empty(&req->r_osd->o_linger_requests)) {
1043 dout("moving osd to %p lru\n", req->r_osd);
1044 __move_osd_to_lru(osdc, req->r_osd);
1045 }
Sage Weilfbdb9192011-03-29 12:11:06 -07001046 if (list_empty(&req->r_osd_item))
1047 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001048 }
1049}
1050
1051void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
1052 struct ceph_osd_request *req)
1053{
1054 mutex_lock(&osdc->request_mutex);
1055 if (req->r_linger) {
1056 __unregister_linger_request(osdc, req);
1057 ceph_osdc_put_request(req);
1058 }
1059 mutex_unlock(&osdc->request_mutex);
1060}
1061EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
1062
1063void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
1064 struct ceph_osd_request *req)
1065{
1066 if (!req->r_linger) {
1067 dout("set_request_linger %p\n", req);
1068 req->r_linger = 1;
1069 /*
1070 * caller is now responsible for calling
1071 * unregister_linger_request
1072 */
1073 ceph_osdc_get_request(req);
1074 }
1075}
1076EXPORT_SYMBOL(ceph_osdc_set_request_linger);
1077
Sage Weilf24e9982009-10-06 11:31:10 -07001078/*
1079 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
1080 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001081 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -08001082 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -07001083 *
1084 * Return 0 if unchanged, 1 if changed, or negative on error.
1085 *
1086 * Caller should hold map_sem for read and request_mutex.
1087 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001088static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -07001089 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -07001090{
Sage Weil5b191d92013-02-23 10:38:16 -08001091 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -07001092 int acting[CEPH_PG_MAX_SIZE];
1093 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001094 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001095
Sage Weil6f6c7002011-01-17 20:34:08 -08001096 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -06001097 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
1098 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -08001099 if (err) {
1100 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -07001101 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -08001102 }
Sage Weil7740a422010-01-08 15:58:25 -08001103 req->r_pgid = pgid;
1104
Sage Weild85b7052010-05-10 10:24:48 -07001105 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
1106 if (err > 0) {
1107 o = acting[0];
1108 num = err;
1109 }
Sage Weilf24e9982009-10-06 11:31:10 -07001110
Sage Weil38d64532011-10-14 13:33:55 -07001111 if ((!force_resend &&
1112 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -07001113 req->r_sent >= req->r_osd->o_incarnation &&
1114 req->r_num_pg_osds == num &&
1115 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001116 (req->r_osd == NULL && o == -1))
1117 return 0; /* no change */
1118
Sage Weil5b191d92013-02-23 10:38:16 -08001119 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
1120 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -07001121 req->r_osd ? req->r_osd->o_osd : -1);
1122
Sage Weild85b7052010-05-10 10:24:48 -07001123 /* record full pg acting set */
1124 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1125 req->r_num_pg_osds = num;
1126
Sage Weilf24e9982009-10-06 11:31:10 -07001127 if (req->r_osd) {
1128 __cancel_request(req);
1129 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001130 req->r_osd = NULL;
1131 }
1132
1133 req->r_osd = __lookup_osd(osdc, o);
1134 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001135 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001136 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001137 if (!req->r_osd) {
1138 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001139 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001140 }
Sage Weilf24e9982009-10-06 11:31:10 -07001141
Sage Weil6f6c7002011-01-17 20:34:08 -08001142 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001143 __insert_osd(osdc, req->r_osd);
1144
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001145 ceph_con_open(&req->r_osd->o_con,
1146 CEPH_ENTITY_TYPE_OSD, o,
1147 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001148 }
1149
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001150 if (req->r_osd) {
1151 __remove_osd_from_lru(req->r_osd);
Alex Elderad885922013-03-25 18:16:11 -05001152 list_add_tail(&req->r_osd_item, &req->r_osd->o_requests);
1153 list_move_tail(&req->r_req_lru_item, &osdc->req_unsent);
Sage Weil6f6c7002011-01-17 20:34:08 -08001154 } else {
Alex Elderad885922013-03-25 18:16:11 -05001155 list_move_tail(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001156 }
Sage Weild85b7052010-05-10 10:24:48 -07001157 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001158
1159out:
Sage Weilf24e9982009-10-06 11:31:10 -07001160 return err;
1161}
1162
1163/*
1164 * caller should hold map_sem (for read) and request_mutex
1165 */
Sage Weil56e925b2012-01-03 12:34:34 -08001166static void __send_request(struct ceph_osd_client *osdc,
1167 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001168{
Sage Weil1b83bef2013-02-25 16:11:12 -08001169 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001170
Sage Weil1b83bef2013-02-25 16:11:12 -08001171 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1172 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1173 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001174
Sage Weil1b83bef2013-02-25 16:11:12 -08001175 /* fill in message content that changes each time we send it */
1176 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1177 put_unaligned_le32(req->r_flags, req->r_request_flags);
1178 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1179 p = req->r_request_pgid;
1180 ceph_encode_64(&p, req->r_pgid.pool);
1181 ceph_encode_32(&p, req->r_pgid.seed);
1182 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1183 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1184 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001185
Sage Weil3dd72fc2010-03-22 14:42:30 -07001186 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001187 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001188
1189 ceph_msg_get(req->r_request); /* send consumes a ref */
1190 ceph_con_send(&req->r_osd->o_con, req->r_request);
1191 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001192}
1193
1194/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001195 * Send any requests in the queue (req_unsent).
1196 */
Alex Elderf9d25192013-02-15 11:42:29 -06001197static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001198{
1199 struct ceph_osd_request *req, *tmp;
1200
Alex Elderf9d25192013-02-15 11:42:29 -06001201 dout("__send_queued\n");
1202 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001203 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001204}
1205
1206/*
Sage Weilf24e9982009-10-06 11:31:10 -07001207 * Timeout callback, called every N seconds when 1 or more osd
1208 * requests has been active for more than N seconds. When this
1209 * happens, we ping all OSDs with requests who have timed out to
1210 * ensure any communications channel reset is detected. Reset the
1211 * request timeouts another N seconds in the future as we go.
1212 * Reschedule the timeout event another N seconds in future (unless
1213 * there are no open requests).
1214 */
1215static void handle_timeout(struct work_struct *work)
1216{
1217 struct ceph_osd_client *osdc =
1218 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001219 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001220 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001221 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001222 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001223 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001224 dout("timeout\n");
1225 down_read(&osdc->map_sem);
1226
1227 ceph_monc_request_next_osdmap(&osdc->client->monc);
1228
1229 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001230
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001231 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001232 * ping osds that are a bit slow. this ensures that if there
1233 * is a break in the TCP connection we will notice, and reopen
1234 * a connection with that osd (from the fault callback).
1235 */
1236 INIT_LIST_HEAD(&slow_osds);
1237 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001238 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001239 break;
1240
1241 osd = req->r_osd;
1242 BUG_ON(!osd);
1243 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001244 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001245 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1246 }
1247 while (!list_empty(&slow_osds)) {
1248 osd = list_entry(slow_osds.next, struct ceph_osd,
1249 o_keepalive_item);
1250 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001251 ceph_con_keepalive(&osd->o_con);
1252 }
1253
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001254 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001255 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001256 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001257 up_read(&osdc->map_sem);
1258}
1259
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001260static void handle_osds_timeout(struct work_struct *work)
1261{
1262 struct ceph_osd_client *osdc =
1263 container_of(work, struct ceph_osd_client,
1264 osds_timeout_work.work);
1265 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001266 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001267
1268 dout("osds timeout\n");
1269 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001270 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001271 up_read(&osdc->map_sem);
1272
1273 schedule_delayed_work(&osdc->osds_timeout_work,
1274 round_jiffies_relative(delay));
1275}
1276
Sage Weil25845472011-06-03 09:37:09 -07001277static void complete_request(struct ceph_osd_request *req)
1278{
1279 if (req->r_safe_callback)
1280 req->r_safe_callback(req, NULL);
1281 complete_all(&req->r_safe_completion); /* fsync waiter */
1282}
1283
Sage Weil1b83bef2013-02-25 16:11:12 -08001284static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1285{
1286 __u8 v;
1287
1288 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1289 v = ceph_decode_8(p);
1290 if (v > 1) {
1291 pr_warning("do not understand pg encoding %d > 1", v);
1292 return -EINVAL;
1293 }
1294 pgid->pool = ceph_decode_64(p);
1295 pgid->seed = ceph_decode_32(p);
1296 *p += 4;
1297 return 0;
1298
1299bad:
1300 pr_warning("incomplete pg encoding");
1301 return -EINVAL;
1302}
1303
Sage Weilf24e9982009-10-06 11:31:10 -07001304/*
1305 * handle osd op reply. either call the callback if it is specified,
1306 * or do the completion to wake up the waiting thread.
1307 */
Sage Weil350b1c32009-12-22 10:45:45 -08001308static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1309 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001310{
Sage Weil1b83bef2013-02-25 16:11:12 -08001311 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001312 struct ceph_osd_request *req;
1313 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001314 int object_len;
1315 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001316 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001317 s32 retry_attempt;
1318 struct ceph_pg pg;
1319 int err;
1320 u32 reassert_epoch;
1321 u64 reassert_version;
1322 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001323 int already_completed;
Sage Weil1b83bef2013-02-25 16:11:12 -08001324 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001325
Sage Weil6df058c2009-12-22 11:24:33 -08001326 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001327 dout("handle_reply %p tid %llu\n", msg, tid);
1328
1329 p = msg->front.iov_base;
1330 end = p + msg->front.iov_len;
1331
1332 ceph_decode_need(&p, end, 4, bad);
1333 object_len = ceph_decode_32(&p);
1334 ceph_decode_need(&p, end, object_len, bad);
1335 p += object_len;
1336
1337 err = __decode_pgid(&p, end, &pg);
1338 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001339 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001340
1341 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1342 flags = ceph_decode_64(&p);
1343 result = ceph_decode_32(&p);
1344 reassert_epoch = ceph_decode_32(&p);
1345 reassert_version = ceph_decode_64(&p);
1346 osdmap_epoch = ceph_decode_32(&p);
1347
Sage Weilf24e9982009-10-06 11:31:10 -07001348 /* lookup */
1349 mutex_lock(&osdc->request_mutex);
1350 req = __lookup_request(osdc, tid);
1351 if (req == NULL) {
1352 dout("handle_reply tid %llu dne\n", tid);
1353 mutex_unlock(&osdc->request_mutex);
1354 return;
1355 }
1356 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001357
1358 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1359 req, result);
1360
1361 ceph_decode_need(&p, end, 4, bad);
1362 numops = ceph_decode_32(&p);
1363 if (numops > CEPH_OSD_MAX_OP)
1364 goto bad_put;
1365 if (numops != req->r_num_ops)
1366 goto bad_put;
1367 payload_len = 0;
1368 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1369 for (i = 0; i < numops; i++) {
1370 struct ceph_osd_op *op = p;
1371 int len;
1372
1373 len = le32_to_cpu(op->payload_len);
1374 req->r_reply_op_len[i] = len;
1375 dout(" op %d has %d bytes\n", i, len);
1376 payload_len += len;
1377 p += sizeof(*op);
1378 }
1379 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1380 pr_warning("sum of op payload lens %d != data_len %d",
1381 payload_len, le32_to_cpu(msg->hdr.data_len));
1382 goto bad_put;
1383 }
1384
1385 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1386 retry_attempt = ceph_decode_32(&p);
1387 for (i = 0; i < numops; i++)
1388 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001389
Sage Weil350b1c32009-12-22 10:45:45 -08001390 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001391 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001392 * avoid a (safe but slower) revoke later.
1393 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001394 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001395 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001396 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001397 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001398 }
1399
Sage Weilf24e9982009-10-06 11:31:10 -07001400 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001401 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001402
Sage Weil1b83bef2013-02-25 16:11:12 -08001403 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001404 bytes = le32_to_cpu(msg->hdr.data_len);
1405 dout("handle_reply result %d bytes %d\n", req->r_result,
1406 bytes);
1407 if (req->r_result == 0)
1408 req->r_result = bytes;
1409
1410 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001411 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1412 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001413
1414 req->r_got_reply = 1;
1415 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1416 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001417 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001418 goto done;
1419 }
1420
1421 dout("handle_reply tid %llu flags %d\n", tid, flags);
1422
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001423 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1424 __register_linger_request(osdc, req);
1425
Sage Weilf24e9982009-10-06 11:31:10 -07001426 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001427 if (result < 0 ||
1428 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001429 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1430 __unregister_request(osdc, req);
1431
Alex Elder0d5af162013-02-27 10:26:25 -06001432 already_completed = req->r_completed;
1433 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001434 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001435 if (already_completed)
1436 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001437
1438 if (req->r_callback)
1439 req->r_callback(req, msg);
1440 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001441 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001442
Sage Weil25845472011-06-03 09:37:09 -07001443 if (flags & CEPH_OSD_FLAG_ONDISK)
1444 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001445
1446done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001447 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001448 ceph_osdc_put_request(req);
1449 return;
1450
Sage Weil1b83bef2013-02-25 16:11:12 -08001451bad_put:
1452 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001453bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001454 pr_err("corrupt osd_op_reply got %d %d\n",
1455 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001456 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001457}
1458
Sage Weil6f6c7002011-01-17 20:34:08 -08001459static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001460{
Sage Weilf24e9982009-10-06 11:31:10 -07001461 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001462
Sage Weil6f6c7002011-01-17 20:34:08 -08001463 for (p = rb_first(&osdc->osds); p; p = n) {
1464 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001465
Sage Weil6f6c7002011-01-17 20:34:08 -08001466 n = rb_next(p);
1467 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1468 memcmp(&osd->o_con.peer_addr,
1469 ceph_osd_addr(osdc->osdmap,
1470 osd->o_osd),
1471 sizeof(struct ceph_entity_addr)) != 0)
1472 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001473 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001474}
1475
1476/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001477 * Requeue requests whose mapping to an OSD has changed. If requests map to
1478 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001479 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001480 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001481 */
Sage Weil38d64532011-10-14 13:33:55 -07001482static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001483{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001484 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001485 struct rb_node *p;
1486 int needmap = 0;
1487 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001488
Sage Weil38d64532011-10-14 13:33:55 -07001489 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001490 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001491 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001492 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001493 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001494
1495 /*
1496 * For linger requests that have not yet been
1497 * registered, move them to the linger list; they'll
1498 * be sent to the osd in the loop below. Unregister
1499 * the request before re-registering it as a linger
1500 * request to ensure the __map_request() below
1501 * will decide it needs to be sent.
1502 */
1503 if (req->r_linger && list_empty(&req->r_linger_item)) {
1504 dout("%p tid %llu restart on osd%d\n",
1505 req, req->r_tid,
1506 req->r_osd ? req->r_osd->o_osd : -1);
1507 __unregister_request(osdc, req);
1508 __register_linger_request(osdc, req);
1509 continue;
1510 }
1511
Sage Weil38d64532011-10-14 13:33:55 -07001512 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001513 if (err < 0)
1514 continue; /* error */
1515 if (req->r_osd == NULL) {
1516 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1517 needmap++; /* request a newer map */
1518 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001519 if (!req->r_linger) {
1520 dout("%p tid %llu requeued on osd%d\n", req,
1521 req->r_tid,
1522 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001523 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001524 }
1525 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001526 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001527
1528 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1529 r_linger_item) {
1530 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1531
Sage Weil38d64532011-10-14 13:33:55 -07001532 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001533 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001534 if (err == 0)
1535 continue; /* no change and no osd was specified */
1536 if (err < 0)
1537 continue; /* hrm! */
1538 if (req->r_osd == NULL) {
1539 dout("tid %llu maps to no valid osd\n", req->r_tid);
1540 needmap++; /* request a newer map */
1541 continue;
1542 }
1543
1544 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1545 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001546 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001547 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001548 }
Sage Weilf24e9982009-10-06 11:31:10 -07001549 mutex_unlock(&osdc->request_mutex);
1550
1551 if (needmap) {
1552 dout("%d requests for down osds, need new map\n", needmap);
1553 ceph_monc_request_next_osdmap(&osdc->client->monc);
1554 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001555 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001556}
Sage Weil6f6c7002011-01-17 20:34:08 -08001557
1558
Sage Weilf24e9982009-10-06 11:31:10 -07001559/*
1560 * Process updated osd map.
1561 *
1562 * The message contains any number of incremental and full maps, normally
1563 * indicating some sort of topology change in the cluster. Kick requests
1564 * off to different OSDs as needed.
1565 */
1566void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1567{
1568 void *p, *end, *next;
1569 u32 nr_maps, maplen;
1570 u32 epoch;
1571 struct ceph_osdmap *newmap = NULL, *oldmap;
1572 int err;
1573 struct ceph_fsid fsid;
1574
1575 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1576 p = msg->front.iov_base;
1577 end = p + msg->front.iov_len;
1578
1579 /* verify fsid */
1580 ceph_decode_need(&p, end, sizeof(fsid), bad);
1581 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001582 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1583 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001584
1585 down_write(&osdc->map_sem);
1586
1587 /* incremental maps */
1588 ceph_decode_32_safe(&p, end, nr_maps, bad);
1589 dout(" %d inc maps\n", nr_maps);
1590 while (nr_maps > 0) {
1591 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001592 epoch = ceph_decode_32(&p);
1593 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001594 ceph_decode_need(&p, end, maplen, bad);
1595 next = p + maplen;
1596 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1597 dout("applying incremental map %u len %d\n",
1598 epoch, maplen);
1599 newmap = osdmap_apply_incremental(&p, next,
1600 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001601 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001602 if (IS_ERR(newmap)) {
1603 err = PTR_ERR(newmap);
1604 goto bad;
1605 }
Sage Weil30dc6382009-12-21 14:49:37 -08001606 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001607 if (newmap != osdc->osdmap) {
1608 ceph_osdmap_destroy(osdc->osdmap);
1609 osdc->osdmap = newmap;
1610 }
Sage Weil38d64532011-10-14 13:33:55 -07001611 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001612 } else {
1613 dout("ignoring incremental map %u len %d\n",
1614 epoch, maplen);
1615 }
1616 p = next;
1617 nr_maps--;
1618 }
1619 if (newmap)
1620 goto done;
1621
1622 /* full maps */
1623 ceph_decode_32_safe(&p, end, nr_maps, bad);
1624 dout(" %d full maps\n", nr_maps);
1625 while (nr_maps) {
1626 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001627 epoch = ceph_decode_32(&p);
1628 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001629 ceph_decode_need(&p, end, maplen, bad);
1630 if (nr_maps > 1) {
1631 dout("skipping non-latest full map %u len %d\n",
1632 epoch, maplen);
1633 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1634 dout("skipping full map %u len %d, "
1635 "older than our %u\n", epoch, maplen,
1636 osdc->osdmap->epoch);
1637 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001638 int skipped_map = 0;
1639
Sage Weilf24e9982009-10-06 11:31:10 -07001640 dout("taking full map %u len %d\n", epoch, maplen);
1641 newmap = osdmap_decode(&p, p+maplen);
1642 if (IS_ERR(newmap)) {
1643 err = PTR_ERR(newmap);
1644 goto bad;
1645 }
Sage Weil30dc6382009-12-21 14:49:37 -08001646 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001647 oldmap = osdc->osdmap;
1648 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001649 if (oldmap) {
1650 if (oldmap->epoch + 1 < newmap->epoch)
1651 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001652 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001653 }
1654 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001655 }
1656 p += maplen;
1657 nr_maps--;
1658 }
1659
1660done:
1661 downgrade_write(&osdc->map_sem);
1662 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001663
1664 /*
1665 * subscribe to subsequent osdmap updates if full to ensure
1666 * we find out when we are no longer full and stop returning
1667 * ENOSPC.
1668 */
1669 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1670 ceph_monc_request_next_osdmap(&osdc->client->monc);
1671
Alex Elderf9d25192013-02-15 11:42:29 -06001672 mutex_lock(&osdc->request_mutex);
1673 __send_queued(osdc);
1674 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001675 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001676 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001677 return;
1678
1679bad:
1680 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001681 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001682 up_write(&osdc->map_sem);
1683 return;
1684}
1685
Sage Weilf24e9982009-10-06 11:31:10 -07001686/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001687 * watch/notify callback event infrastructure
1688 *
1689 * These callbacks are used both for watch and notify operations.
1690 */
1691static void __release_event(struct kref *kref)
1692{
1693 struct ceph_osd_event *event =
1694 container_of(kref, struct ceph_osd_event, kref);
1695
1696 dout("__release_event %p\n", event);
1697 kfree(event);
1698}
1699
1700static void get_event(struct ceph_osd_event *event)
1701{
1702 kref_get(&event->kref);
1703}
1704
1705void ceph_osdc_put_event(struct ceph_osd_event *event)
1706{
1707 kref_put(&event->kref, __release_event);
1708}
1709EXPORT_SYMBOL(ceph_osdc_put_event);
1710
1711static void __insert_event(struct ceph_osd_client *osdc,
1712 struct ceph_osd_event *new)
1713{
1714 struct rb_node **p = &osdc->event_tree.rb_node;
1715 struct rb_node *parent = NULL;
1716 struct ceph_osd_event *event = NULL;
1717
1718 while (*p) {
1719 parent = *p;
1720 event = rb_entry(parent, struct ceph_osd_event, node);
1721 if (new->cookie < event->cookie)
1722 p = &(*p)->rb_left;
1723 else if (new->cookie > event->cookie)
1724 p = &(*p)->rb_right;
1725 else
1726 BUG();
1727 }
1728
1729 rb_link_node(&new->node, parent, p);
1730 rb_insert_color(&new->node, &osdc->event_tree);
1731}
1732
1733static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1734 u64 cookie)
1735{
1736 struct rb_node **p = &osdc->event_tree.rb_node;
1737 struct rb_node *parent = NULL;
1738 struct ceph_osd_event *event = NULL;
1739
1740 while (*p) {
1741 parent = *p;
1742 event = rb_entry(parent, struct ceph_osd_event, node);
1743 if (cookie < event->cookie)
1744 p = &(*p)->rb_left;
1745 else if (cookie > event->cookie)
1746 p = &(*p)->rb_right;
1747 else
1748 return event;
1749 }
1750 return NULL;
1751}
1752
1753static void __remove_event(struct ceph_osd_event *event)
1754{
1755 struct ceph_osd_client *osdc = event->osdc;
1756
1757 if (!RB_EMPTY_NODE(&event->node)) {
1758 dout("__remove_event removed %p\n", event);
1759 rb_erase(&event->node, &osdc->event_tree);
1760 ceph_osdc_put_event(event);
1761 } else {
1762 dout("__remove_event didn't remove %p\n", event);
1763 }
1764}
1765
1766int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1767 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001768 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001769{
1770 struct ceph_osd_event *event;
1771
1772 event = kmalloc(sizeof(*event), GFP_NOIO);
1773 if (!event)
1774 return -ENOMEM;
1775
1776 dout("create_event %p\n", event);
1777 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001778 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001779 event->data = data;
1780 event->osdc = osdc;
1781 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001782 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001783 kref_init(&event->kref); /* one ref for us */
1784 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001785
1786 spin_lock(&osdc->event_lock);
1787 event->cookie = ++osdc->event_count;
1788 __insert_event(osdc, event);
1789 spin_unlock(&osdc->event_lock);
1790
1791 *pevent = event;
1792 return 0;
1793}
1794EXPORT_SYMBOL(ceph_osdc_create_event);
1795
1796void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1797{
1798 struct ceph_osd_client *osdc = event->osdc;
1799
1800 dout("cancel_event %p\n", event);
1801 spin_lock(&osdc->event_lock);
1802 __remove_event(event);
1803 spin_unlock(&osdc->event_lock);
1804 ceph_osdc_put_event(event); /* caller's */
1805}
1806EXPORT_SYMBOL(ceph_osdc_cancel_event);
1807
1808
1809static void do_event_work(struct work_struct *work)
1810{
1811 struct ceph_osd_event_work *event_work =
1812 container_of(work, struct ceph_osd_event_work, work);
1813 struct ceph_osd_event *event = event_work->event;
1814 u64 ver = event_work->ver;
1815 u64 notify_id = event_work->notify_id;
1816 u8 opcode = event_work->opcode;
1817
1818 dout("do_event_work completing %p\n", event);
1819 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001820 dout("do_event_work completed %p\n", event);
1821 ceph_osdc_put_event(event);
1822 kfree(event_work);
1823}
1824
1825
1826/*
1827 * Process osd watch notifications
1828 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001829static void handle_watch_notify(struct ceph_osd_client *osdc,
1830 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001831{
1832 void *p, *end;
1833 u8 proto_ver;
1834 u64 cookie, ver, notify_id;
1835 u8 opcode;
1836 struct ceph_osd_event *event;
1837 struct ceph_osd_event_work *event_work;
1838
1839 p = msg->front.iov_base;
1840 end = p + msg->front.iov_len;
1841
1842 ceph_decode_8_safe(&p, end, proto_ver, bad);
1843 ceph_decode_8_safe(&p, end, opcode, bad);
1844 ceph_decode_64_safe(&p, end, cookie, bad);
1845 ceph_decode_64_safe(&p, end, ver, bad);
1846 ceph_decode_64_safe(&p, end, notify_id, bad);
1847
1848 spin_lock(&osdc->event_lock);
1849 event = __find_event(osdc, cookie);
1850 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001851 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001852 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001853 }
1854 spin_unlock(&osdc->event_lock);
1855 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1856 cookie, ver, event);
1857 if (event) {
1858 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001859 if (!event_work) {
1860 dout("ERROR: could not allocate event_work\n");
1861 goto done_err;
1862 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001863 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001864 event_work->event = event;
1865 event_work->ver = ver;
1866 event_work->notify_id = notify_id;
1867 event_work->opcode = opcode;
1868 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1869 dout("WARNING: failed to queue notify event work\n");
1870 goto done_err;
1871 }
1872 }
1873
1874 return;
1875
1876done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001877 ceph_osdc_put_event(event);
1878 return;
1879
1880bad:
1881 pr_err("osdc handle_watch_notify corrupt msg\n");
1882 return;
1883}
1884
Alex Elder70636772013-03-04 18:29:06 -06001885static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1886 struct ceph_osd_data *osd_data)
1887{
1888 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1889 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1890 if (osd_data->length)
1891 ceph_msg_data_set_pages(msg, osd_data->pages,
1892 osd_data->length, osd_data->alignment);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001893 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1894 BUG_ON(!osd_data->pagelist->length);
1895 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
Alex Elder70636772013-03-04 18:29:06 -06001896#ifdef CONFIG_BLOCK
1897 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1898 ceph_msg_data_set_bio(msg, osd_data->bio);
1899#endif
1900 } else {
1901 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1902 }
1903}
1904
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001905/*
Sage Weilf24e9982009-10-06 11:31:10 -07001906 * Register request, send initial attempt.
1907 */
1908int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1909 struct ceph_osd_request *req,
1910 bool nofail)
1911{
Sage Weilc1ea8822009-10-08 16:55:47 -07001912 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001913
Alex Elder70636772013-03-04 18:29:06 -06001914 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001915
Alex Elder70636772013-03-04 18:29:06 -06001916 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1917 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Sage Weilf24e9982009-10-06 11:31:10 -07001918
Sage Weilf24e9982009-10-06 11:31:10 -07001919 down_read(&osdc->map_sem);
1920 mutex_lock(&osdc->request_mutex);
Alex Elderdc4b8702013-03-25 18:16:11 -05001921 __register_request(osdc, req);
Alex Elder92451b42013-03-25 18:16:11 -05001922 WARN_ON(req->r_sent);
1923 rc = __map_request(osdc, req, 0);
1924 if (rc < 0) {
1925 if (nofail) {
1926 dout("osdc_start_request failed map, "
1927 " will retry %lld\n", req->r_tid);
1928 rc = 0;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001929 }
Alex Elder92451b42013-03-25 18:16:11 -05001930 goto out_unlock;
Sage Weilf24e9982009-10-06 11:31:10 -07001931 }
Alex Elder92451b42013-03-25 18:16:11 -05001932 if (req->r_osd == NULL) {
1933 dout("send_request %p no up osds in pg\n", req);
1934 ceph_monc_request_next_osdmap(&osdc->client->monc);
1935 } else {
Alex Elder7e2766a2013-03-25 18:16:11 -05001936 __send_queued(osdc);
Alex Elder92451b42013-03-25 18:16:11 -05001937 }
1938 rc = 0;
Dan Carpenter234af26f2011-03-29 06:25:59 +03001939out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001940 mutex_unlock(&osdc->request_mutex);
1941 up_read(&osdc->map_sem);
1942 return rc;
1943}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001944EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001945
1946/*
1947 * wait for a request to complete
1948 */
1949int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1950 struct ceph_osd_request *req)
1951{
1952 int rc;
1953
1954 rc = wait_for_completion_interruptible(&req->r_completion);
1955 if (rc < 0) {
1956 mutex_lock(&osdc->request_mutex);
1957 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001958 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001959 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001960 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001961 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001962 return rc;
1963 }
1964
1965 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1966 return req->r_result;
1967}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001968EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001969
1970/*
1971 * sync - wait for all in-flight requests to flush. avoid starvation.
1972 */
1973void ceph_osdc_sync(struct ceph_osd_client *osdc)
1974{
1975 struct ceph_osd_request *req;
1976 u64 last_tid, next_tid = 0;
1977
1978 mutex_lock(&osdc->request_mutex);
1979 last_tid = osdc->last_tid;
1980 while (1) {
1981 req = __lookup_request_ge(osdc, next_tid);
1982 if (!req)
1983 break;
1984 if (req->r_tid > last_tid)
1985 break;
1986
1987 next_tid = req->r_tid + 1;
1988 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1989 continue;
1990
1991 ceph_osdc_get_request(req);
1992 mutex_unlock(&osdc->request_mutex);
1993 dout("sync waiting on tid %llu (last is %llu)\n",
1994 req->r_tid, last_tid);
1995 wait_for_completion(&req->r_safe_completion);
1996 mutex_lock(&osdc->request_mutex);
1997 ceph_osdc_put_request(req);
1998 }
1999 mutex_unlock(&osdc->request_mutex);
2000 dout("sync done (thru tid %llu)\n", last_tid);
2001}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002002EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07002003
2004/*
2005 * init, shutdown
2006 */
2007int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
2008{
2009 int err;
2010
2011 dout("init\n");
2012 osdc->client = client;
2013 osdc->osdmap = NULL;
2014 init_rwsem(&osdc->map_sem);
2015 init_completion(&osdc->map_waiters);
2016 osdc->last_requested_map = 0;
2017 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07002018 osdc->last_tid = 0;
2019 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002020 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07002021 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08002022 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08002023 INIT_LIST_HEAD(&osdc->req_unsent);
2024 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002025 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07002026 osdc->num_requests = 0;
2027 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002028 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002029 spin_lock_init(&osdc->event_lock);
2030 osdc->event_tree = RB_ROOT;
2031 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002032
2033 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002034 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07002035
Sage Weil5f44f142009-11-18 14:52:18 -08002036 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07002037 osdc->req_mempool = mempool_create_kmalloc_pool(10,
2038 sizeof(struct ceph_osd_request));
2039 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08002040 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07002041
Sage Weild50b4092012-07-09 14:22:34 -07002042 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
2043 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07002044 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07002045 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08002046 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07002047 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07002048 OSD_OPREPLY_FRONT_LEN, 10, true,
2049 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08002050 if (err < 0)
2051 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002052
2053 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
2054 if (IS_ERR(osdc->notify_wq)) {
2055 err = PTR_ERR(osdc->notify_wq);
2056 osdc->notify_wq = NULL;
2057 goto out_msgpool;
2058 }
Sage Weilf24e9982009-10-06 11:31:10 -07002059 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08002060
Sage Weilc16e7862010-03-01 13:02:00 -08002061out_msgpool:
2062 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08002063out_mempool:
2064 mempool_destroy(osdc->req_mempool);
2065out:
2066 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07002067}
2068
2069void ceph_osdc_stop(struct ceph_osd_client *osdc)
2070{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002071 flush_workqueue(osdc->notify_wq);
2072 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07002073 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08002074 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07002075 if (osdc->osdmap) {
2076 ceph_osdmap_destroy(osdc->osdmap);
2077 osdc->osdmap = NULL;
2078 }
Sage Weilaca420b2011-08-31 14:45:53 -07002079 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07002080 mempool_destroy(osdc->req_mempool);
2081 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08002082 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07002083}
2084
2085/*
2086 * Read some contiguous pages. If we cross a stripe boundary, shorten
2087 * *plen. Return number of bytes read, or error.
2088 */
2089int ceph_osdc_readpages(struct ceph_osd_client *osdc,
2090 struct ceph_vino vino, struct ceph_file_layout *layout,
2091 u64 off, u64 *plen,
2092 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08002093 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07002094{
2095 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002096 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002097 int rc = 0;
2098
2099 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
2100 vino.snap, off, *plen);
2101 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
2102 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
2103 NULL, 0, truncate_seq, truncate_size, NULL,
Alex Elder153e5162013-03-01 18:00:15 -06002104 false);
Sage Weil68162822012-09-24 21:01:02 -07002105 if (IS_ERR(req))
2106 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002107
2108 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002109
2110 osd_data = &req->r_data_in;
2111 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2112 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002113 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06002114 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07002115
Alex Eldere0c59482013-03-07 15:38:25 -06002116 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
2117 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07002118
2119 rc = ceph_osdc_start_request(osdc, req, false);
2120 if (!rc)
2121 rc = ceph_osdc_wait_request(osdc, req);
2122
2123 ceph_osdc_put_request(req);
2124 dout("readpages result %d\n", rc);
2125 return rc;
2126}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002127EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002128
2129/*
2130 * do a synchronous write on N pages
2131 */
2132int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2133 struct ceph_file_layout *layout,
2134 struct ceph_snap_context *snapc,
2135 u64 off, u64 len,
2136 u32 truncate_seq, u64 truncate_size,
2137 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002138 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002139{
2140 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002141 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002142 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002143 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002144
2145 BUG_ON(vino.snap != CEPH_NOSNAP);
2146 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2147 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002148 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06002149 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07002150 truncate_seq, truncate_size, mtime,
Alex Elder153e5162013-03-01 18:00:15 -06002151 true);
Sage Weil68162822012-09-24 21:01:02 -07002152 if (IS_ERR(req))
2153 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002154
2155 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002156 osd_data = &req->r_data_out;
2157 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2158 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002159 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002160 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002161 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002162
Alex Elder87f979d2013-02-15 11:42:29 -06002163 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002164 if (!rc)
2165 rc = ceph_osdc_wait_request(osdc, req);
2166
2167 ceph_osdc_put_request(req);
2168 if (rc == 0)
2169 rc = len;
2170 dout("writepages result %d\n", rc);
2171 return rc;
2172}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002173EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002174
2175/*
2176 * handle incoming message
2177 */
2178static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2179{
2180 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002181 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002182 int type = le16_to_cpu(msg->hdr.type);
2183
2184 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002185 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002186 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002187
2188 switch (type) {
2189 case CEPH_MSG_OSD_MAP:
2190 ceph_osdc_handle_map(osdc, msg);
2191 break;
2192 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002193 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002194 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002195 case CEPH_MSG_WATCH_NOTIFY:
2196 handle_watch_notify(osdc, msg);
2197 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002198
2199 default:
2200 pr_err("received unknown message type %d %s\n", type,
2201 ceph_msg_type_name(type));
2202 }
Sage Weil4a32f932010-06-13 10:27:53 -07002203out:
Sage Weilf24e9982009-10-06 11:31:10 -07002204 ceph_msg_put(msg);
2205}
2206
Sage Weil5b3a4db2010-02-19 21:43:23 -08002207/*
Sage Weil21b667f2010-03-04 10:22:59 -08002208 * lookup and return message for incoming reply. set up reply message
2209 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002210 */
2211static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002212 struct ceph_msg_header *hdr,
2213 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002214{
2215 struct ceph_osd *osd = con->private;
2216 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002217 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002218 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002219 int front = le32_to_cpu(hdr->front_len);
2220 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002221 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002222
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002223 tid = le64_to_cpu(hdr->tid);
2224 mutex_lock(&osdc->request_mutex);
2225 req = __lookup_request(osdc, tid);
2226 if (!req) {
2227 *skip = 1;
2228 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002229 dout("get_reply unknown tid %llu from osd%d\n", tid,
2230 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002231 goto out;
2232 }
Sage Weilc16e7862010-03-01 13:02:00 -08002233
2234 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002235 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002236 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002237 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002238 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002239 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002240 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002241
Sage Weilc16e7862010-03-01 13:02:00 -08002242 if (front > req->r_reply->front.iov_len) {
2243 pr_warning("get_reply front %d > preallocated %d\n",
2244 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002245 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002246 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002247 goto out;
2248 ceph_msg_put(req->r_reply);
2249 req->r_reply = m;
2250 }
2251 m = ceph_msg_get(req->r_reply);
2252
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002253 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002254 struct ceph_osd_data *osd_data = &req->r_data_in;
2255
2256 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002257 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002258 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002259
Alex Eldere0c59482013-03-07 15:38:25 -06002260 pr_warning("tid %lld reply has %d bytes "
2261 "we had only %llu bytes ready\n",
2262 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002263 *skip = 1;
2264 ceph_msg_put(m);
2265 m = NULL;
2266 goto out;
2267 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002268 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002269 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002270 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002271 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002272 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002273
2274out:
2275 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002276 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002277
2278}
2279
2280static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2281 struct ceph_msg_header *hdr,
2282 int *skip)
2283{
2284 struct ceph_osd *osd = con->private;
2285 int type = le16_to_cpu(hdr->type);
2286 int front = le32_to_cpu(hdr->front_len);
2287
Alex Elder1c20f2d2012-06-04 14:43:32 -05002288 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002289 switch (type) {
2290 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002291 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002292 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002293 case CEPH_MSG_OSD_OPREPLY:
2294 return get_reply(con, hdr, skip);
2295 default:
2296 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2297 osd->o_osd);
2298 *skip = 1;
2299 return NULL;
2300 }
Sage Weilf24e9982009-10-06 11:31:10 -07002301}
2302
2303/*
2304 * Wrappers to refcount containing ceph_osd struct
2305 */
2306static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2307{
2308 struct ceph_osd *osd = con->private;
2309 if (get_osd(osd))
2310 return con;
2311 return NULL;
2312}
2313
2314static void put_osd_con(struct ceph_connection *con)
2315{
2316 struct ceph_osd *osd = con->private;
2317 put_osd(osd);
2318}
2319
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002320/*
2321 * authentication
2322 */
Alex Eldera3530df2012-05-16 15:16:39 -05002323/*
2324 * Note: returned pointer is the address of a structure that's
2325 * managed separately. Caller must *not* attempt to free it.
2326 */
2327static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002328 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002329{
2330 struct ceph_osd *o = con->private;
2331 struct ceph_osd_client *osdc = o->o_osdc;
2332 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002333 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002334
Alex Elder74f18692012-05-16 15:16:39 -05002335 if (force_new && auth->authorizer) {
Sage Weil27859f92013-03-25 10:26:14 -07002336 ceph_auth_destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002337 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002338 }
Sage Weil27859f92013-03-25 10:26:14 -07002339 if (!auth->authorizer) {
2340 int ret = ceph_auth_create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2341 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002342 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002343 return ERR_PTR(ret);
Sage Weil27859f92013-03-25 10:26:14 -07002344 } else {
2345 int ret = ceph_auth_update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
Sage Weil0bed9b52013-03-25 10:26:01 -07002346 auth);
2347 if (ret)
2348 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002349 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002350 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002351
Alex Eldera3530df2012-05-16 15:16:39 -05002352 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002353}
2354
2355
2356static int verify_authorizer_reply(struct ceph_connection *con, int len)
2357{
2358 struct ceph_osd *o = con->private;
2359 struct ceph_osd_client *osdc = o->o_osdc;
2360 struct ceph_auth_client *ac = osdc->client->monc.auth;
2361
Sage Weil27859f92013-03-25 10:26:14 -07002362 return ceph_auth_verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002363}
2364
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002365static int invalidate_authorizer(struct ceph_connection *con)
2366{
2367 struct ceph_osd *o = con->private;
2368 struct ceph_osd_client *osdc = o->o_osdc;
2369 struct ceph_auth_client *ac = osdc->client->monc.auth;
2370
Sage Weil27859f92013-03-25 10:26:14 -07002371 ceph_auth_invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002372 return ceph_monc_validate_auth(&osdc->client->monc);
2373}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002374
Tobias Klauser9e327892010-05-20 10:40:19 +02002375static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002376 .get = get_osd_con,
2377 .put = put_osd_con,
2378 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002379 .get_authorizer = get_authorizer,
2380 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002381 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002382 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002383 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002384};