blob: b6776cb627cc23148adc18e1a76d054ee1f58648 [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
Sage Weil6f6c7002011-01-17 20:34:08 -080026static void send_queued(struct ceph_osd_client *osdc);
27static 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);
32static int __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_needs_trail(int op)
36{
37 switch (op) {
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070042 case CEPH_OSD_OP_NOTIFY:
Yehuda Sadeh68b44762010-04-06 15:01:27 -070043 return 1;
44 default:
45 return 0;
46 }
47}
48
49static int op_has_extent(int op)
50{
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
53}
54
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070055void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
57 u64 snapid,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070058 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070061{
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Yehuda Sadeh68b44762010-04-06 15:01:27 -070063 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070064 u64 objoff, objlen; /* extent in object */
65
66 reqhead->snapid = cpu_to_le64(snapid);
67
68 /* object extent? */
Yehuda Sadeh68b44762010-04-06 15:01:27 -070069 ceph_calc_file_object_mapping(layout, off, plen, bno,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070070 &objoff, &objlen);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070071 if (*plen < orig_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070072 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070073 orig_len - *plen, off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070074
Yehuda Sadeh68b44762010-04-06 15:01:27 -070075 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
78 }
79 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080080 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070081 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070083
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
86
87}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070088EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070089
Sage Weilf24e9982009-10-06 11:31:10 -070090/*
91 * Implement client access to distributed object storage cluster.
92 *
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
97 * access to storage.
98 *
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
101 *
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
106 */
107
108/*
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
111 * object boundary.
112 *
113 * fill osd op in request message.
114 */
115static void calc_layout(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
Sage Weilf24e9982009-10-06 11:31:10 -0700118 u64 off, u64 *plen,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700121{
Sage Weilf24e9982009-10-06 11:31:10 -0700122 u64 bno;
123
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
Sage Weilf24e9982009-10-06 11:31:10 -0700126
127 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
128 req->r_oid_len = strlen(req->r_oid);
Sage Weilf24e9982009-10-06 11:31:10 -0700129}
130
Sage Weilf24e9982009-10-06 11:31:10 -0700131/*
132 * requests
133 */
Sage Weil415e49a2009-12-07 13:37:03 -0800134void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700135{
Sage Weil415e49a2009-12-07 13:37:03 -0800136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
138 r_kref);
139
140 if (req->r_request)
141 ceph_msg_put(req->r_request);
142 if (req->r_reply)
143 ceph_msg_put(req->r_reply);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800144 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -0800145 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800146 req->r_pages, req->r_con_filling_msg);
147 ceph_con_revoke_message(req->r_con_filling_msg,
148 req->r_reply);
149 ceph_con_put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800150 }
Sage Weil415e49a2009-12-07 13:37:03 -0800151 if (req->r_own_pages)
152 ceph_release_page_vector(req->r_pages,
153 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700154#ifdef CONFIG_BLOCK
155 if (req->r_bio)
156 bio_put(req->r_bio);
157#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800158 ceph_put_snap_context(req->r_snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700159 if (req->r_trail) {
160 ceph_pagelist_release(req->r_trail);
161 kfree(req->r_trail);
162 }
Sage Weil415e49a2009-12-07 13:37:03 -0800163 if (req->r_mempool)
164 mempool_free(req, req->r_osdc->req_mempool);
165 else
166 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700167}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700168EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700169
170static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
171{
172 int i = 0;
173
174 if (needs_trail)
175 *needs_trail = 0;
176 while (ops[i].op) {
177 if (needs_trail && op_needs_trail(ops[i].op))
178 *needs_trail = 1;
179 i++;
180 }
181
182 return i;
183}
184
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700185struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
186 int flags,
187 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700188 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700189 bool use_mempool,
190 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700191 struct page **pages,
192 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700193{
194 struct ceph_osd_request *req;
195 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700196 int needs_trail;
197 int num_op = get_num_ops(ops, &needs_trail);
198 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700199
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700200 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700201
202 if (use_mempool) {
203 req = mempool_alloc(osdc->req_mempool, gfp_flags);
204 memset(req, 0, sizeof(*req));
205 } else {
206 req = kzalloc(sizeof(*req), gfp_flags);
207 }
208 if (req == NULL)
209 return NULL;
210
211 req->r_osdc = osdc;
212 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700213
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700214 kref_init(&req->r_kref);
215 init_completion(&req->r_completion);
216 init_completion(&req->r_safe_completion);
217 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700218 INIT_LIST_HEAD(&req->r_linger_item);
219 INIT_LIST_HEAD(&req->r_linger_osd);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700220 req->r_flags = flags;
221
222 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
223
224 /* create reply message */
225 if (use_mempool)
226 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
227 else
228 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
229 OSD_OPREPLY_FRONT_LEN, gfp_flags);
230 if (!msg) {
231 ceph_osdc_put_request(req);
232 return NULL;
233 }
234 req->r_reply = msg;
235
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700236 /* allocate space for the trailing data */
237 if (needs_trail) {
238 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
239 if (!req->r_trail) {
240 ceph_osdc_put_request(req);
241 return NULL;
242 }
243 ceph_pagelist_init(req->r_trail);
244 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700245 /* create request message; allow space for oid */
246 msg_size += 40;
247 if (snapc)
248 msg_size += sizeof(u64) * snapc->num_snaps;
249 if (use_mempool)
250 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
251 else
252 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
253 if (!msg) {
254 ceph_osdc_put_request(req);
255 return NULL;
256 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700257
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700258 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
259 memset(msg->front.iov_base, 0, msg->front.iov_len);
260
261 req->r_request = msg;
262 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700263#ifdef CONFIG_BLOCK
264 if (bio) {
265 req->r_bio = bio;
266 bio_get(req->r_bio);
267 }
268#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700269
270 return req;
271}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700272EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700273
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700274static void osd_req_encode_op(struct ceph_osd_request *req,
275 struct ceph_osd_op *dst,
276 struct ceph_osd_req_op *src)
277{
278 dst->op = cpu_to_le16(src->op);
279
280 switch (dst->op) {
281 case CEPH_OSD_OP_READ:
282 case CEPH_OSD_OP_WRITE:
283 dst->extent.offset =
284 cpu_to_le64(src->extent.offset);
285 dst->extent.length =
286 cpu_to_le64(src->extent.length);
287 dst->extent.truncate_size =
288 cpu_to_le64(src->extent.truncate_size);
289 dst->extent.truncate_seq =
290 cpu_to_le32(src->extent.truncate_seq);
291 break;
292
293 case CEPH_OSD_OP_GETXATTR:
294 case CEPH_OSD_OP_SETXATTR:
295 case CEPH_OSD_OP_CMPXATTR:
296 BUG_ON(!req->r_trail);
297
298 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
299 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
300 dst->xattr.cmp_op = src->xattr.cmp_op;
301 dst->xattr.cmp_mode = src->xattr.cmp_mode;
302 ceph_pagelist_append(req->r_trail, src->xattr.name,
303 src->xattr.name_len);
304 ceph_pagelist_append(req->r_trail, src->xattr.val,
305 src->xattr.value_len);
306 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700307 case CEPH_OSD_OP_CALL:
308 BUG_ON(!req->r_trail);
309
310 dst->cls.class_len = src->cls.class_len;
311 dst->cls.method_len = src->cls.method_len;
312 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
313
314 ceph_pagelist_append(req->r_trail, src->cls.class_name,
315 src->cls.class_len);
316 ceph_pagelist_append(req->r_trail, src->cls.method_name,
317 src->cls.method_len);
318 ceph_pagelist_append(req->r_trail, src->cls.indata,
319 src->cls.indata_len);
320 break;
321 case CEPH_OSD_OP_ROLLBACK:
322 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
323 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700324 case CEPH_OSD_OP_STARTSYNC:
325 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700326 case CEPH_OSD_OP_NOTIFY:
327 {
328 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
329 __le32 timeout = cpu_to_le32(src->watch.timeout);
330
331 BUG_ON(!req->r_trail);
332
333 ceph_pagelist_append(req->r_trail,
334 &prot_ver, sizeof(prot_ver));
335 ceph_pagelist_append(req->r_trail,
336 &timeout, sizeof(timeout));
337 }
338 case CEPH_OSD_OP_NOTIFY_ACK:
339 case CEPH_OSD_OP_WATCH:
340 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
341 dst->watch.ver = cpu_to_le64(src->watch.ver);
342 dst->watch.flag = src->watch.flag;
343 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700344 default:
345 pr_err("unrecognized osd opcode %d\n", dst->op);
346 WARN_ON(1);
347 break;
348 }
349 dst->payload_len = cpu_to_le32(src->payload_len);
350}
351
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700352/*
353 * build new request AND message
354 *
355 */
356void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700357 u64 off, u64 *plen,
358 struct ceph_osd_req_op *src_ops,
359 struct ceph_snap_context *snapc,
360 struct timespec *mtime,
361 const char *oid,
362 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363{
364 struct ceph_msg *msg = req->r_request;
365 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700366 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367 struct ceph_osd_op *op;
368 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700369 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700370 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700371 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700372 u64 data_len = 0;
373 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
375 head = msg->front.iov_base;
376 op = (void *)(head + 1);
377 p = (void *)(op + num_op);
378
379 req->r_snapc = ceph_get_snap_context(snapc);
380
381 head->client_inc = cpu_to_le32(1); /* always, for now. */
382 head->flags = cpu_to_le32(flags);
383 if (flags & CEPH_OSD_FLAG_WRITE)
384 ceph_encode_timespec(&head->mtime, mtime);
385 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700386
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387
388 /* fill in oid */
389 head->object_len = cpu_to_le32(oid_len);
390 memcpy(p, oid, oid_len);
391 p += oid_len;
392
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700393 src_op = src_ops;
394 while (src_op->op) {
395 osd_req_encode_op(req, op, src_op);
396 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700398 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700399
400 if (req->r_trail)
401 data_len += req->r_trail->length;
402
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700403 if (snapc) {
404 head->snap_seq = cpu_to_le64(snapc->seq);
405 head->num_snaps = cpu_to_le32(snapc->num_snaps);
406 for (i = 0; i < snapc->num_snaps; i++) {
407 put_unaligned_le64(snapc->snaps[i], p);
408 p += sizeof(u64);
409 }
410 }
411
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700412 if (flags & CEPH_OSD_FLAG_WRITE) {
413 req->r_request->hdr.data_off = cpu_to_le16(off);
414 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
415 } else if (data_len) {
416 req->r_request->hdr.data_off = 0;
417 req->r_request->hdr.data_len = cpu_to_le32(data_len);
418 }
419
Sage Weilc5c6b192010-11-09 12:40:00 -0800420 req->r_request->page_alignment = req->r_page_alignment;
421
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700422 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
423 msg_size = p - msg->front.iov_base;
424 msg->front.iov_len = msg_size;
425 msg->hdr.front_len = cpu_to_le32(msg_size);
426 return;
427}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700428EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700429
Sage Weilf24e9982009-10-06 11:31:10 -0700430/*
431 * build new request AND message, calculate layout, and adjust file
432 * extent as needed.
433 *
434 * if the file was recently truncated, we include information about its
435 * old and new size so that the object can be updated appropriately. (we
436 * avoid synchronously deleting truncated objects because it's slow.)
437 *
438 * if @do_sync, include a 'startsync' command so that the osd will flush
439 * data quickly.
440 */
441struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
442 struct ceph_file_layout *layout,
443 struct ceph_vino vino,
444 u64 off, u64 *plen,
445 int opcode, int flags,
446 struct ceph_snap_context *snapc,
447 int do_sync,
448 u32 truncate_seq,
449 u64 truncate_size,
450 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800451 bool use_mempool, int num_reply,
452 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700453{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700454 struct ceph_osd_req_op ops[3];
455 struct ceph_osd_request *req;
456
457 ops[0].op = opcode;
458 ops[0].extent.truncate_seq = truncate_seq;
459 ops[0].extent.truncate_size = truncate_size;
460 ops[0].payload_len = 0;
461
462 if (do_sync) {
463 ops[1].op = CEPH_OSD_OP_STARTSYNC;
464 ops[1].payload_len = 0;
465 ops[2].op = 0;
466 } else
467 ops[1].op = 0;
468
469 req = ceph_osdc_alloc_request(osdc, flags,
470 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700471 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700472 GFP_NOFS, NULL, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700473 if (IS_ERR(req))
474 return req;
Sage Weilf24e9982009-10-06 11:31:10 -0700475
476 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700477 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700478 req->r_file_layout = *layout; /* keep a copy */
479
Sage Weilb7495fc2010-11-09 12:43:12 -0800480 /* in case it differs from natural alignment that calc_layout
481 filled in for us */
482 req->r_page_alignment = page_align;
483
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700484 ceph_osdc_build_request(req, off, plen, ops,
485 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700486 mtime,
487 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700488
Sage Weilf24e9982009-10-06 11:31:10 -0700489 return req;
490}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700491EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700492
493/*
494 * We keep osd requests in an rbtree, sorted by ->r_tid.
495 */
496static void __insert_request(struct ceph_osd_client *osdc,
497 struct ceph_osd_request *new)
498{
499 struct rb_node **p = &osdc->requests.rb_node;
500 struct rb_node *parent = NULL;
501 struct ceph_osd_request *req = NULL;
502
503 while (*p) {
504 parent = *p;
505 req = rb_entry(parent, struct ceph_osd_request, r_node);
506 if (new->r_tid < req->r_tid)
507 p = &(*p)->rb_left;
508 else if (new->r_tid > req->r_tid)
509 p = &(*p)->rb_right;
510 else
511 BUG();
512 }
513
514 rb_link_node(&new->r_node, parent, p);
515 rb_insert_color(&new->r_node, &osdc->requests);
516}
517
518static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
519 u64 tid)
520{
521 struct ceph_osd_request *req;
522 struct rb_node *n = osdc->requests.rb_node;
523
524 while (n) {
525 req = rb_entry(n, struct ceph_osd_request, r_node);
526 if (tid < req->r_tid)
527 n = n->rb_left;
528 else if (tid > req->r_tid)
529 n = n->rb_right;
530 else
531 return req;
532 }
533 return NULL;
534}
535
536static struct ceph_osd_request *
537__lookup_request_ge(struct ceph_osd_client *osdc,
538 u64 tid)
539{
540 struct ceph_osd_request *req;
541 struct rb_node *n = osdc->requests.rb_node;
542
543 while (n) {
544 req = rb_entry(n, struct ceph_osd_request, r_node);
545 if (tid < req->r_tid) {
546 if (!n->rb_left)
547 return req;
548 n = n->rb_left;
549 } else if (tid > req->r_tid) {
550 n = n->rb_right;
551 } else {
552 return req;
553 }
554 }
555 return NULL;
556}
557
Sage Weil6f6c7002011-01-17 20:34:08 -0800558/*
559 * Resubmit requests pending on the given osd.
560 */
561static void __kick_osd_requests(struct ceph_osd_client *osdc,
562 struct ceph_osd *osd)
563{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700564 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800565 int err;
566
567 dout("__kick_osd_requests osd%d\n", osd->o_osd);
568 err = __reset_osd(osdc, osd);
569 if (err == -EAGAIN)
570 return;
571
572 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
573 list_move(&req->r_req_lru_item, &osdc->req_unsent);
574 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
575 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700576 if (!req->r_linger)
577 req->r_flags |= CEPH_OSD_FLAG_RETRY;
578 }
579
580 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
581 r_linger_osd) {
582 __unregister_linger_request(osdc, req);
583 __register_request(osdc, req);
584 list_move(&req->r_req_lru_item, &osdc->req_unsent);
585 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
586 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800587 }
588}
589
590static void kick_osd_requests(struct ceph_osd_client *osdc,
591 struct ceph_osd *kickosd)
592{
593 mutex_lock(&osdc->request_mutex);
594 __kick_osd_requests(osdc, kickosd);
595 mutex_unlock(&osdc->request_mutex);
596}
Sage Weilf24e9982009-10-06 11:31:10 -0700597
598/*
Sage Weil81b024e2009-10-09 10:29:18 -0700599 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700600 */
601static void osd_reset(struct ceph_connection *con)
602{
603 struct ceph_osd *osd = con->private;
604 struct ceph_osd_client *osdc;
605
606 if (!osd)
607 return;
608 dout("osd_reset osd%d\n", osd->o_osd);
609 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700610 down_read(&osdc->map_sem);
Sage Weil6f6c7002011-01-17 20:34:08 -0800611 kick_osd_requests(osdc, osd);
612 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700613 up_read(&osdc->map_sem);
614}
615
616/*
617 * Track open sessions with osds.
618 */
619static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
620{
621 struct ceph_osd *osd;
622
623 osd = kzalloc(sizeof(*osd), GFP_NOFS);
624 if (!osd)
625 return NULL;
626
627 atomic_set(&osd->o_ref, 1);
628 osd->o_osdc = osdc;
629 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700630 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800631 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700632 osd->o_incarnation = 1;
633
634 ceph_con_init(osdc->client->msgr, &osd->o_con);
635 osd->o_con.private = osd;
636 osd->o_con.ops = &osd_con_ops;
637 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800638
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800639 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700640 return osd;
641}
642
643static struct ceph_osd *get_osd(struct ceph_osd *osd)
644{
645 if (atomic_inc_not_zero(&osd->o_ref)) {
646 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
647 atomic_read(&osd->o_ref));
648 return osd;
649 } else {
650 dout("get_osd %p FAIL\n", osd);
651 return NULL;
652 }
653}
654
655static void put_osd(struct ceph_osd *osd)
656{
657 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
658 atomic_read(&osd->o_ref) - 1);
Sage Weil79494d12010-05-27 14:15:49 -0700659 if (atomic_dec_and_test(&osd->o_ref)) {
660 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
661
662 if (osd->o_authorizer)
663 ac->ops->destroy_authorizer(ac, osd->o_authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700664 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700665 }
Sage Weilf24e9982009-10-06 11:31:10 -0700666}
667
668/*
669 * remove an osd from our map
670 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800671static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700672{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800673 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700674 BUG_ON(!list_empty(&osd->o_requests));
675 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800676 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700677 ceph_con_close(&osd->o_con);
678 put_osd(osd);
679}
680
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800681static void __move_osd_to_lru(struct ceph_osd_client *osdc,
682 struct ceph_osd *osd)
683{
684 dout("__move_osd_to_lru %p\n", osd);
685 BUG_ON(!list_empty(&osd->o_osd_lru));
686 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700687 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800688}
689
690static void __remove_osd_from_lru(struct ceph_osd *osd)
691{
692 dout("__remove_osd_from_lru %p\n", osd);
693 if (!list_empty(&osd->o_osd_lru))
694 list_del_init(&osd->o_osd_lru);
695}
696
697static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
698{
699 struct ceph_osd *osd, *nosd;
700
701 dout("__remove_old_osds %p\n", osdc);
702 mutex_lock(&osdc->request_mutex);
703 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
704 if (!remove_all && time_before(jiffies, osd->lru_ttl))
705 break;
706 __remove_osd(osdc, osd);
707 }
708 mutex_unlock(&osdc->request_mutex);
709}
710
Sage Weilf24e9982009-10-06 11:31:10 -0700711/*
712 * reset osd connect
713 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800714static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700715{
Sage Weil87b315a2010-03-22 14:51:18 -0700716 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700717 int ret = 0;
718
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800719 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700720 if (list_empty(&osd->o_requests) &&
721 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800722 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700723 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
724 &osd->o_con.peer_addr,
725 sizeof(osd->o_con.peer_addr)) == 0 &&
726 !ceph_con_opened(&osd->o_con)) {
727 dout(" osd addr hasn't changed and connection never opened,"
728 " letting msgr retry");
729 /* touch each r_stamp for handle_timeout()'s benfit */
730 list_for_each_entry(req, &osd->o_requests, r_osd_item)
731 req->r_stamp = jiffies;
732 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700733 } else {
734 ceph_con_close(&osd->o_con);
735 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
736 osd->o_incarnation++;
737 }
738 return ret;
739}
740
741static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
742{
743 struct rb_node **p = &osdc->osds.rb_node;
744 struct rb_node *parent = NULL;
745 struct ceph_osd *osd = NULL;
746
747 while (*p) {
748 parent = *p;
749 osd = rb_entry(parent, struct ceph_osd, o_node);
750 if (new->o_osd < osd->o_osd)
751 p = &(*p)->rb_left;
752 else if (new->o_osd > osd->o_osd)
753 p = &(*p)->rb_right;
754 else
755 BUG();
756 }
757
758 rb_link_node(&new->o_node, parent, p);
759 rb_insert_color(&new->o_node, &osdc->osds);
760}
761
762static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
763{
764 struct ceph_osd *osd;
765 struct rb_node *n = osdc->osds.rb_node;
766
767 while (n) {
768 osd = rb_entry(n, struct ceph_osd, o_node);
769 if (o < osd->o_osd)
770 n = n->rb_left;
771 else if (o > osd->o_osd)
772 n = n->rb_right;
773 else
774 return osd;
775 }
776 return NULL;
777}
778
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800779static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
780{
781 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700782 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800783}
784
785static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
786{
787 cancel_delayed_work(&osdc->timeout_work);
788}
Sage Weilf24e9982009-10-06 11:31:10 -0700789
790/*
791 * Register request, assign tid. If this is the first request, set up
792 * the timeout event.
793 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700794static void __register_request(struct ceph_osd_client *osdc,
795 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700796{
Sage Weilf24e9982009-10-06 11:31:10 -0700797 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800798 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800799 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700800
801 dout("register_request %p tid %lld\n", req, req->r_tid);
802 __insert_request(osdc, req);
803 ceph_osdc_get_request(req);
804 osdc->num_requests++;
805
Sage Weilf24e9982009-10-06 11:31:10 -0700806 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800807 dout(" first request, scheduling timeout\n");
808 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700809 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700810}
811
812static void register_request(struct ceph_osd_client *osdc,
813 struct ceph_osd_request *req)
814{
815 mutex_lock(&osdc->request_mutex);
816 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700817 mutex_unlock(&osdc->request_mutex);
818}
819
820/*
821 * called under osdc->request_mutex
822 */
823static void __unregister_request(struct ceph_osd_client *osdc,
824 struct ceph_osd_request *req)
825{
826 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
827 rb_erase(&req->r_node, &osdc->requests);
828 osdc->num_requests--;
829
Sage Weil0ba64782009-10-08 16:57:16 -0700830 if (req->r_osd) {
831 /* make sure the original request isn't in flight. */
832 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
833
834 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700835 if (list_empty(&req->r_osd->o_requests) &&
836 list_empty(&req->r_osd->o_linger_requests)) {
837 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800838 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700839 }
840 if (list_empty(&req->r_osd_item) &&
841 list_empty(&req->r_linger_item))
842 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700843 }
Sage Weilf24e9982009-10-06 11:31:10 -0700844
845 ceph_osdc_put_request(req);
846
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800847 list_del_init(&req->r_req_lru_item);
848 if (osdc->num_requests == 0) {
849 dout(" no requests, canceling timeout\n");
850 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700851 }
852}
853
854/*
855 * Cancel a previously queued request message
856 */
857static void __cancel_request(struct ceph_osd_request *req)
858{
Sage Weil6bc18872010-09-27 10:18:52 -0700859 if (req->r_sent && req->r_osd) {
Sage Weilf24e9982009-10-06 11:31:10 -0700860 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
861 req->r_sent = 0;
862 }
863}
864
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700865static void __register_linger_request(struct ceph_osd_client *osdc,
866 struct ceph_osd_request *req)
867{
868 dout("__register_linger_request %p\n", req);
869 list_add_tail(&req->r_linger_item, &osdc->req_linger);
870 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
871}
872
873static void __unregister_linger_request(struct ceph_osd_client *osdc,
874 struct ceph_osd_request *req)
875{
876 dout("__unregister_linger_request %p\n", req);
877 if (req->r_osd) {
878 list_del_init(&req->r_linger_item);
879 list_del_init(&req->r_linger_osd);
880
881 if (list_empty(&req->r_osd->o_requests) &&
882 list_empty(&req->r_osd->o_linger_requests)) {
883 dout("moving osd to %p lru\n", req->r_osd);
884 __move_osd_to_lru(osdc, req->r_osd);
885 }
886 req->r_osd = NULL;
887 }
888}
889
890void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
891 struct ceph_osd_request *req)
892{
893 mutex_lock(&osdc->request_mutex);
894 if (req->r_linger) {
895 __unregister_linger_request(osdc, req);
896 ceph_osdc_put_request(req);
897 }
898 mutex_unlock(&osdc->request_mutex);
899}
900EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
901
902void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
903 struct ceph_osd_request *req)
904{
905 if (!req->r_linger) {
906 dout("set_request_linger %p\n", req);
907 req->r_linger = 1;
908 /*
909 * caller is now responsible for calling
910 * unregister_linger_request
911 */
912 ceph_osdc_get_request(req);
913 }
914}
915EXPORT_SYMBOL(ceph_osdc_set_request_linger);
916
Sage Weilf24e9982009-10-06 11:31:10 -0700917/*
918 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
919 * (as needed), and set the request r_osd appropriately. If there is
Sage Weil6f6c7002011-01-17 20:34:08 -0800920 * no up osd, set r_osd to NULL. Move the request to the appropiate list
921 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700922 *
923 * Return 0 if unchanged, 1 if changed, or negative on error.
924 *
925 * Caller should hold map_sem for read and request_mutex.
926 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800927static int __map_request(struct ceph_osd_client *osdc,
928 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700929{
930 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800931 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700932 int acting[CEPH_PG_MAX_SIZE];
933 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700934 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700935
Sage Weil6f6c7002011-01-17 20:34:08 -0800936 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700937 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
938 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800939 if (err) {
940 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700941 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800942 }
Sage Weil51042122009-11-04 11:39:12 -0800943 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800944 req->r_pgid = pgid;
945
Sage Weild85b7052010-05-10 10:24:48 -0700946 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
947 if (err > 0) {
948 o = acting[0];
949 num = err;
950 }
Sage Weilf24e9982009-10-06 11:31:10 -0700951
952 if ((req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700953 req->r_sent >= req->r_osd->o_incarnation &&
954 req->r_num_pg_osds == num &&
955 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700956 (req->r_osd == NULL && o == -1))
957 return 0; /* no change */
958
Sage Weil6f6c7002011-01-17 20:34:08 -0800959 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800960 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700961 req->r_osd ? req->r_osd->o_osd : -1);
962
Sage Weild85b7052010-05-10 10:24:48 -0700963 /* record full pg acting set */
964 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
965 req->r_num_pg_osds = num;
966
Sage Weilf24e9982009-10-06 11:31:10 -0700967 if (req->r_osd) {
968 __cancel_request(req);
969 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700970 req->r_osd = NULL;
971 }
972
973 req->r_osd = __lookup_osd(osdc, o);
974 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800975 err = -ENOMEM;
976 req->r_osd = create_osd(osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -0800977 if (!req->r_osd) {
978 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800979 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -0800980 }
Sage Weilf24e9982009-10-06 11:31:10 -0700981
Sage Weil6f6c7002011-01-17 20:34:08 -0800982 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -0700983 req->r_osd->o_osd = o;
984 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
985 __insert_osd(osdc, req->r_osd);
986
987 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
988 }
989
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800990 if (req->r_osd) {
991 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700992 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -0800993 list_move(&req->r_req_lru_item, &osdc->req_unsent);
994 } else {
995 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800996 }
Sage Weild85b7052010-05-10 10:24:48 -0700997 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -0700998
999out:
Sage Weilf24e9982009-10-06 11:31:10 -07001000 return err;
1001}
1002
1003/*
1004 * caller should hold map_sem (for read) and request_mutex
1005 */
1006static int __send_request(struct ceph_osd_client *osdc,
1007 struct ceph_osd_request *req)
1008{
1009 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001010
1011 dout("send_request %p tid %llu to osd%d flags %d\n",
1012 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1013
1014 reqhead = req->r_request->front.iov_base;
1015 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1016 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1017 reqhead->reassert_version = req->r_reassert_version;
1018
Sage Weil3dd72fc2010-03-22 14:42:30 -07001019 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001020 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001021
1022 ceph_msg_get(req->r_request); /* send consumes a ref */
1023 ceph_con_send(&req->r_osd->o_con, req->r_request);
1024 req->r_sent = req->r_osd->o_incarnation;
1025 return 0;
1026}
1027
1028/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001029 * Send any requests in the queue (req_unsent).
1030 */
1031static void send_queued(struct ceph_osd_client *osdc)
1032{
1033 struct ceph_osd_request *req, *tmp;
1034
1035 dout("send_queued\n");
1036 mutex_lock(&osdc->request_mutex);
1037 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1038 __send_request(osdc, req);
1039 }
1040 mutex_unlock(&osdc->request_mutex);
1041}
1042
1043/*
Sage Weilf24e9982009-10-06 11:31:10 -07001044 * Timeout callback, called every N seconds when 1 or more osd
1045 * requests has been active for more than N seconds. When this
1046 * happens, we ping all OSDs with requests who have timed out to
1047 * ensure any communications channel reset is detected. Reset the
1048 * request timeouts another N seconds in the future as we go.
1049 * Reschedule the timeout event another N seconds in future (unless
1050 * there are no open requests).
1051 */
1052static void handle_timeout(struct work_struct *work)
1053{
1054 struct ceph_osd_client *osdc =
1055 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001056 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001057 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001058 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001059 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001060 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001061 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001062 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001063 dout("timeout\n");
1064 down_read(&osdc->map_sem);
1065
1066 ceph_monc_request_next_osdmap(&osdc->client->monc);
1067
1068 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001069
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001070 /*
1071 * reset osds that appear to be _really_ unresponsive. this
1072 * is a failsafe measure.. we really shouldn't be getting to
1073 * this point if the system is working properly. the monitors
1074 * should mark the osd as failed and we should find out about
1075 * it from an updated osd map.
1076 */
Sage Weilf26e6812010-04-21 11:09:38 -07001077 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001078 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1079 r_req_lru_item);
1080
Sage Weil3dd72fc2010-03-22 14:42:30 -07001081 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001082 break;
1083
Sage Weil3dd72fc2010-03-22 14:42:30 -07001084 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001085 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001086 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001087
1088 osd = req->r_osd;
1089 BUG_ON(!osd);
1090 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1091 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001092 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001093 }
1094
1095 /*
1096 * ping osds that are a bit slow. this ensures that if there
1097 * is a break in the TCP connection we will notice, and reopen
1098 * a connection with that osd (from the fault callback).
1099 */
1100 INIT_LIST_HEAD(&slow_osds);
1101 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001102 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001103 break;
1104
1105 osd = req->r_osd;
1106 BUG_ON(!osd);
1107 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001108 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001109 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1110 }
1111 while (!list_empty(&slow_osds)) {
1112 osd = list_entry(slow_osds.next, struct ceph_osd,
1113 o_keepalive_item);
1114 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001115 ceph_con_keepalive(&osd->o_con);
1116 }
1117
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001118 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001119 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001120 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001121 up_read(&osdc->map_sem);
1122}
1123
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001124static void handle_osds_timeout(struct work_struct *work)
1125{
1126 struct ceph_osd_client *osdc =
1127 container_of(work, struct ceph_osd_client,
1128 osds_timeout_work.work);
1129 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001130 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001131
1132 dout("osds timeout\n");
1133 down_read(&osdc->map_sem);
1134 remove_old_osds(osdc, 0);
1135 up_read(&osdc->map_sem);
1136
1137 schedule_delayed_work(&osdc->osds_timeout_work,
1138 round_jiffies_relative(delay));
1139}
1140
Sage Weilf24e9982009-10-06 11:31:10 -07001141/*
1142 * handle osd op reply. either call the callback if it is specified,
1143 * or do the completion to wake up the waiting thread.
1144 */
Sage Weil350b1c32009-12-22 10:45:45 -08001145static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1146 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001147{
1148 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1149 struct ceph_osd_request *req;
1150 u64 tid;
1151 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001152 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001153
Sage Weil6df058c2009-12-22 11:24:33 -08001154 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001155 if (msg->front.iov_len < sizeof(*rhead))
1156 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001157 numops = le32_to_cpu(rhead->num_ops);
1158 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001159 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001160 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1161 numops * sizeof(struct ceph_osd_op))
1162 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001163 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001164 /* lookup */
1165 mutex_lock(&osdc->request_mutex);
1166 req = __lookup_request(osdc, tid);
1167 if (req == NULL) {
1168 dout("handle_reply tid %llu dne\n", tid);
1169 mutex_unlock(&osdc->request_mutex);
1170 return;
1171 }
1172 ceph_osdc_get_request(req);
1173 flags = le32_to_cpu(rhead->flags);
1174
Sage Weil350b1c32009-12-22 10:45:45 -08001175 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001176 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001177 * avoid a (safe but slower) revoke later.
1178 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001179 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001180 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001181 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -08001182 ceph_con_put(con);
1183 }
1184
Sage Weilf24e9982009-10-06 11:31:10 -07001185 if (!req->r_got_reply) {
1186 unsigned bytes;
1187
1188 req->r_result = le32_to_cpu(rhead->result);
1189 bytes = le32_to_cpu(msg->hdr.data_len);
1190 dout("handle_reply result %d bytes %d\n", req->r_result,
1191 bytes);
1192 if (req->r_result == 0)
1193 req->r_result = bytes;
1194
1195 /* in case this is a write and we need to replay, */
1196 req->r_reassert_version = rhead->reassert_version;
1197
1198 req->r_got_reply = 1;
1199 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1200 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001201 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001202 goto done;
1203 }
1204
1205 dout("handle_reply tid %llu flags %d\n", tid, flags);
1206
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001207 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1208 __register_linger_request(osdc, req);
1209
Sage Weilf24e9982009-10-06 11:31:10 -07001210 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001211 if (result < 0 ||
1212 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001213 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1214 __unregister_request(osdc, req);
1215
1216 mutex_unlock(&osdc->request_mutex);
1217
1218 if (req->r_callback)
1219 req->r_callback(req, msg);
1220 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001221 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001222
1223 if (flags & CEPH_OSD_FLAG_ONDISK) {
1224 if (req->r_safe_callback)
1225 req->r_safe_callback(req, msg);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001226 complete_all(&req->r_safe_completion); /* fsync waiter */
Sage Weilf24e9982009-10-06 11:31:10 -07001227 }
1228
1229done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001230 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001231 ceph_osdc_put_request(req);
1232 return;
1233
1234bad:
1235 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1236 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1237 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001238 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001239}
1240
Sage Weil6f6c7002011-01-17 20:34:08 -08001241static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001242{
Sage Weilf24e9982009-10-06 11:31:10 -07001243 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001244
Sage Weil6f6c7002011-01-17 20:34:08 -08001245 for (p = rb_first(&osdc->osds); p; p = n) {
1246 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001247
Sage Weil6f6c7002011-01-17 20:34:08 -08001248 n = rb_next(p);
1249 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1250 memcmp(&osd->o_con.peer_addr,
1251 ceph_osd_addr(osdc->osdmap,
1252 osd->o_osd),
1253 sizeof(struct ceph_entity_addr)) != 0)
1254 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001255 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001256}
1257
1258/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001259 * Requeue requests whose mapping to an OSD has changed. If requests map to
1260 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001261 *
1262 * Caller should hold map_sem for read and request_mutex.
1263 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001264static void kick_requests(struct ceph_osd_client *osdc)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001265{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001266 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001267 struct rb_node *p;
1268 int needmap = 0;
1269 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001270
Sage Weil6f6c7002011-01-17 20:34:08 -08001271 dout("kick_requests\n");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001272 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001273 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1274 req = rb_entry(p, struct ceph_osd_request, r_node);
1275 err = __map_request(osdc, req);
1276 if (err < 0)
1277 continue; /* error */
1278 if (req->r_osd == NULL) {
1279 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1280 needmap++; /* request a newer map */
1281 } else if (err > 0) {
1282 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1283 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001284 if (!req->r_linger)
1285 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001286 }
1287 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001288
1289 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1290 r_linger_item) {
1291 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1292
1293 err = __map_request(osdc, req);
1294 if (err == 0)
1295 continue; /* no change and no osd was specified */
1296 if (err < 0)
1297 continue; /* hrm! */
1298 if (req->r_osd == NULL) {
1299 dout("tid %llu maps to no valid osd\n", req->r_tid);
1300 needmap++; /* request a newer map */
1301 continue;
1302 }
1303
1304 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1305 req->r_osd ? req->r_osd->o_osd : -1);
1306 __unregister_linger_request(osdc, req);
1307 __register_request(osdc, req);
1308 }
Sage Weilf24e9982009-10-06 11:31:10 -07001309 mutex_unlock(&osdc->request_mutex);
1310
1311 if (needmap) {
1312 dout("%d requests for down osds, need new map\n", needmap);
1313 ceph_monc_request_next_osdmap(&osdc->client->monc);
1314 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001315}
Sage Weil6f6c7002011-01-17 20:34:08 -08001316
1317
Sage Weilf24e9982009-10-06 11:31:10 -07001318/*
1319 * Process updated osd map.
1320 *
1321 * The message contains any number of incremental and full maps, normally
1322 * indicating some sort of topology change in the cluster. Kick requests
1323 * off to different OSDs as needed.
1324 */
1325void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1326{
1327 void *p, *end, *next;
1328 u32 nr_maps, maplen;
1329 u32 epoch;
1330 struct ceph_osdmap *newmap = NULL, *oldmap;
1331 int err;
1332 struct ceph_fsid fsid;
1333
1334 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1335 p = msg->front.iov_base;
1336 end = p + msg->front.iov_len;
1337
1338 /* verify fsid */
1339 ceph_decode_need(&p, end, sizeof(fsid), bad);
1340 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001341 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1342 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001343
1344 down_write(&osdc->map_sem);
1345
1346 /* incremental maps */
1347 ceph_decode_32_safe(&p, end, nr_maps, bad);
1348 dout(" %d inc maps\n", nr_maps);
1349 while (nr_maps > 0) {
1350 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001351 epoch = ceph_decode_32(&p);
1352 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001353 ceph_decode_need(&p, end, maplen, bad);
1354 next = p + maplen;
1355 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1356 dout("applying incremental map %u len %d\n",
1357 epoch, maplen);
1358 newmap = osdmap_apply_incremental(&p, next,
1359 osdc->osdmap,
1360 osdc->client->msgr);
1361 if (IS_ERR(newmap)) {
1362 err = PTR_ERR(newmap);
1363 goto bad;
1364 }
Sage Weil30dc6382009-12-21 14:49:37 -08001365 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001366 if (newmap != osdc->osdmap) {
1367 ceph_osdmap_destroy(osdc->osdmap);
1368 osdc->osdmap = newmap;
1369 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001370 kick_requests(osdc);
1371 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001372 } else {
1373 dout("ignoring incremental map %u len %d\n",
1374 epoch, maplen);
1375 }
1376 p = next;
1377 nr_maps--;
1378 }
1379 if (newmap)
1380 goto done;
1381
1382 /* full maps */
1383 ceph_decode_32_safe(&p, end, nr_maps, bad);
1384 dout(" %d full maps\n", nr_maps);
1385 while (nr_maps) {
1386 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001387 epoch = ceph_decode_32(&p);
1388 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001389 ceph_decode_need(&p, end, maplen, bad);
1390 if (nr_maps > 1) {
1391 dout("skipping non-latest full map %u len %d\n",
1392 epoch, maplen);
1393 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1394 dout("skipping full map %u len %d, "
1395 "older than our %u\n", epoch, maplen,
1396 osdc->osdmap->epoch);
1397 } else {
1398 dout("taking full map %u len %d\n", epoch, maplen);
1399 newmap = osdmap_decode(&p, p+maplen);
1400 if (IS_ERR(newmap)) {
1401 err = PTR_ERR(newmap);
1402 goto bad;
1403 }
Sage Weil30dc6382009-12-21 14:49:37 -08001404 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001405 oldmap = osdc->osdmap;
1406 osdc->osdmap = newmap;
1407 if (oldmap)
1408 ceph_osdmap_destroy(oldmap);
Sage Weil6f6c7002011-01-17 20:34:08 -08001409 kick_requests(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001410 }
1411 p += maplen;
1412 nr_maps--;
1413 }
1414
1415done:
1416 downgrade_write(&osdc->map_sem);
1417 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weil6f6c7002011-01-17 20:34:08 -08001418 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001419 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001420 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001421 return;
1422
1423bad:
1424 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001425 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001426 up_write(&osdc->map_sem);
1427 return;
1428}
1429
Sage Weilf24e9982009-10-06 11:31:10 -07001430/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001431 * watch/notify callback event infrastructure
1432 *
1433 * These callbacks are used both for watch and notify operations.
1434 */
1435static void __release_event(struct kref *kref)
1436{
1437 struct ceph_osd_event *event =
1438 container_of(kref, struct ceph_osd_event, kref);
1439
1440 dout("__release_event %p\n", event);
1441 kfree(event);
1442}
1443
1444static void get_event(struct ceph_osd_event *event)
1445{
1446 kref_get(&event->kref);
1447}
1448
1449void ceph_osdc_put_event(struct ceph_osd_event *event)
1450{
1451 kref_put(&event->kref, __release_event);
1452}
1453EXPORT_SYMBOL(ceph_osdc_put_event);
1454
1455static void __insert_event(struct ceph_osd_client *osdc,
1456 struct ceph_osd_event *new)
1457{
1458 struct rb_node **p = &osdc->event_tree.rb_node;
1459 struct rb_node *parent = NULL;
1460 struct ceph_osd_event *event = NULL;
1461
1462 while (*p) {
1463 parent = *p;
1464 event = rb_entry(parent, struct ceph_osd_event, node);
1465 if (new->cookie < event->cookie)
1466 p = &(*p)->rb_left;
1467 else if (new->cookie > event->cookie)
1468 p = &(*p)->rb_right;
1469 else
1470 BUG();
1471 }
1472
1473 rb_link_node(&new->node, parent, p);
1474 rb_insert_color(&new->node, &osdc->event_tree);
1475}
1476
1477static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1478 u64 cookie)
1479{
1480 struct rb_node **p = &osdc->event_tree.rb_node;
1481 struct rb_node *parent = NULL;
1482 struct ceph_osd_event *event = NULL;
1483
1484 while (*p) {
1485 parent = *p;
1486 event = rb_entry(parent, struct ceph_osd_event, node);
1487 if (cookie < event->cookie)
1488 p = &(*p)->rb_left;
1489 else if (cookie > event->cookie)
1490 p = &(*p)->rb_right;
1491 else
1492 return event;
1493 }
1494 return NULL;
1495}
1496
1497static void __remove_event(struct ceph_osd_event *event)
1498{
1499 struct ceph_osd_client *osdc = event->osdc;
1500
1501 if (!RB_EMPTY_NODE(&event->node)) {
1502 dout("__remove_event removed %p\n", event);
1503 rb_erase(&event->node, &osdc->event_tree);
1504 ceph_osdc_put_event(event);
1505 } else {
1506 dout("__remove_event didn't remove %p\n", event);
1507 }
1508}
1509
1510int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1511 void (*event_cb)(u64, u64, u8, void *),
1512 int one_shot, void *data,
1513 struct ceph_osd_event **pevent)
1514{
1515 struct ceph_osd_event *event;
1516
1517 event = kmalloc(sizeof(*event), GFP_NOIO);
1518 if (!event)
1519 return -ENOMEM;
1520
1521 dout("create_event %p\n", event);
1522 event->cb = event_cb;
1523 event->one_shot = one_shot;
1524 event->data = data;
1525 event->osdc = osdc;
1526 INIT_LIST_HEAD(&event->osd_node);
1527 kref_init(&event->kref); /* one ref for us */
1528 kref_get(&event->kref); /* one ref for the caller */
1529 init_completion(&event->completion);
1530
1531 spin_lock(&osdc->event_lock);
1532 event->cookie = ++osdc->event_count;
1533 __insert_event(osdc, event);
1534 spin_unlock(&osdc->event_lock);
1535
1536 *pevent = event;
1537 return 0;
1538}
1539EXPORT_SYMBOL(ceph_osdc_create_event);
1540
1541void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1542{
1543 struct ceph_osd_client *osdc = event->osdc;
1544
1545 dout("cancel_event %p\n", event);
1546 spin_lock(&osdc->event_lock);
1547 __remove_event(event);
1548 spin_unlock(&osdc->event_lock);
1549 ceph_osdc_put_event(event); /* caller's */
1550}
1551EXPORT_SYMBOL(ceph_osdc_cancel_event);
1552
1553
1554static void do_event_work(struct work_struct *work)
1555{
1556 struct ceph_osd_event_work *event_work =
1557 container_of(work, struct ceph_osd_event_work, work);
1558 struct ceph_osd_event *event = event_work->event;
1559 u64 ver = event_work->ver;
1560 u64 notify_id = event_work->notify_id;
1561 u8 opcode = event_work->opcode;
1562
1563 dout("do_event_work completing %p\n", event);
1564 event->cb(ver, notify_id, opcode, event->data);
1565 complete(&event->completion);
1566 dout("do_event_work completed %p\n", event);
1567 ceph_osdc_put_event(event);
1568 kfree(event_work);
1569}
1570
1571
1572/*
1573 * Process osd watch notifications
1574 */
1575void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1576{
1577 void *p, *end;
1578 u8 proto_ver;
1579 u64 cookie, ver, notify_id;
1580 u8 opcode;
1581 struct ceph_osd_event *event;
1582 struct ceph_osd_event_work *event_work;
1583
1584 p = msg->front.iov_base;
1585 end = p + msg->front.iov_len;
1586
1587 ceph_decode_8_safe(&p, end, proto_ver, bad);
1588 ceph_decode_8_safe(&p, end, opcode, bad);
1589 ceph_decode_64_safe(&p, end, cookie, bad);
1590 ceph_decode_64_safe(&p, end, ver, bad);
1591 ceph_decode_64_safe(&p, end, notify_id, bad);
1592
1593 spin_lock(&osdc->event_lock);
1594 event = __find_event(osdc, cookie);
1595 if (event) {
1596 get_event(event);
1597 if (event->one_shot)
1598 __remove_event(event);
1599 }
1600 spin_unlock(&osdc->event_lock);
1601 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1602 cookie, ver, event);
1603 if (event) {
1604 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001605 if (!event_work) {
1606 dout("ERROR: could not allocate event_work\n");
1607 goto done_err;
1608 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001609 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001610 event_work->event = event;
1611 event_work->ver = ver;
1612 event_work->notify_id = notify_id;
1613 event_work->opcode = opcode;
1614 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1615 dout("WARNING: failed to queue notify event work\n");
1616 goto done_err;
1617 }
1618 }
1619
1620 return;
1621
1622done_err:
1623 complete(&event->completion);
1624 ceph_osdc_put_event(event);
1625 return;
1626
1627bad:
1628 pr_err("osdc handle_watch_notify corrupt msg\n");
1629 return;
1630}
1631
1632int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1633{
1634 int err;
1635
1636 dout("wait_event %p\n", event);
1637 err = wait_for_completion_interruptible_timeout(&event->completion,
1638 timeout * HZ);
1639 ceph_osdc_put_event(event);
1640 if (err > 0)
1641 err = 0;
1642 dout("wait_event %p returns %d\n", event, err);
1643 return err;
1644}
1645EXPORT_SYMBOL(ceph_osdc_wait_event);
1646
1647/*
Sage Weilf24e9982009-10-06 11:31:10 -07001648 * Register request, send initial attempt.
1649 */
1650int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1651 struct ceph_osd_request *req,
1652 bool nofail)
1653{
Sage Weilc1ea8822009-10-08 16:55:47 -07001654 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001655
1656 req->r_request->pages = req->r_pages;
1657 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001658#ifdef CONFIG_BLOCK
1659 req->r_request->bio = req->r_bio;
1660#endif
1661 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001662
1663 register_request(osdc, req);
1664
1665 down_read(&osdc->map_sem);
1666 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001667 /*
1668 * a racing kick_requests() may have sent the message for us
1669 * while we dropped request_mutex above, so only send now if
1670 * the request still han't been touched yet.
1671 */
1672 if (req->r_sent == 0) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001673 rc = __map_request(osdc, req);
1674 if (rc < 0)
1675 return rc;
1676 if (req->r_osd == NULL) {
1677 dout("send_request %p no up osds in pg\n", req);
1678 ceph_monc_request_next_osdmap(&osdc->client->monc);
1679 } else {
1680 rc = __send_request(osdc, req);
1681 if (rc) {
1682 if (nofail) {
1683 dout("osdc_start_request failed send, "
1684 " will retry %lld\n", req->r_tid);
1685 rc = 0;
1686 } else {
1687 __unregister_request(osdc, req);
1688 }
Sage Weilc1ea8822009-10-08 16:55:47 -07001689 }
Sage Weilf24e9982009-10-06 11:31:10 -07001690 }
1691 }
1692 mutex_unlock(&osdc->request_mutex);
1693 up_read(&osdc->map_sem);
1694 return rc;
1695}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001696EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001697
1698/*
1699 * wait for a request to complete
1700 */
1701int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1702 struct ceph_osd_request *req)
1703{
1704 int rc;
1705
1706 rc = wait_for_completion_interruptible(&req->r_completion);
1707 if (rc < 0) {
1708 mutex_lock(&osdc->request_mutex);
1709 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001710 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001711 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001712 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001713 return rc;
1714 }
1715
1716 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1717 return req->r_result;
1718}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001719EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001720
1721/*
1722 * sync - wait for all in-flight requests to flush. avoid starvation.
1723 */
1724void ceph_osdc_sync(struct ceph_osd_client *osdc)
1725{
1726 struct ceph_osd_request *req;
1727 u64 last_tid, next_tid = 0;
1728
1729 mutex_lock(&osdc->request_mutex);
1730 last_tid = osdc->last_tid;
1731 while (1) {
1732 req = __lookup_request_ge(osdc, next_tid);
1733 if (!req)
1734 break;
1735 if (req->r_tid > last_tid)
1736 break;
1737
1738 next_tid = req->r_tid + 1;
1739 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1740 continue;
1741
1742 ceph_osdc_get_request(req);
1743 mutex_unlock(&osdc->request_mutex);
1744 dout("sync waiting on tid %llu (last is %llu)\n",
1745 req->r_tid, last_tid);
1746 wait_for_completion(&req->r_safe_completion);
1747 mutex_lock(&osdc->request_mutex);
1748 ceph_osdc_put_request(req);
1749 }
1750 mutex_unlock(&osdc->request_mutex);
1751 dout("sync done (thru tid %llu)\n", last_tid);
1752}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001753EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001754
1755/*
1756 * init, shutdown
1757 */
1758int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1759{
1760 int err;
1761
1762 dout("init\n");
1763 osdc->client = client;
1764 osdc->osdmap = NULL;
1765 init_rwsem(&osdc->map_sem);
1766 init_completion(&osdc->map_waiters);
1767 osdc->last_requested_map = 0;
1768 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001769 osdc->last_tid = 0;
1770 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001771 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001772 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001773 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001774 INIT_LIST_HEAD(&osdc->req_unsent);
1775 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001776 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001777 osdc->num_requests = 0;
1778 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001779 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001780 spin_lock_init(&osdc->event_lock);
1781 osdc->event_tree = RB_ROOT;
1782 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001783
1784 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001785 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001786
Sage Weil5f44f142009-11-18 14:52:18 -08001787 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001788 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1789 sizeof(struct ceph_osd_request));
1790 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001791 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001792
Sage Weil4f482802010-04-24 09:56:35 -07001793 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1794 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001795 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001796 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001797 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001798 OSD_OPREPLY_FRONT_LEN, 10, true,
1799 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001800 if (err < 0)
1801 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001802
1803 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1804 if (IS_ERR(osdc->notify_wq)) {
1805 err = PTR_ERR(osdc->notify_wq);
1806 osdc->notify_wq = NULL;
1807 goto out_msgpool;
1808 }
Sage Weilf24e9982009-10-06 11:31:10 -07001809 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001810
Sage Weilc16e7862010-03-01 13:02:00 -08001811out_msgpool:
1812 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001813out_mempool:
1814 mempool_destroy(osdc->req_mempool);
1815out:
1816 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001817}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001818EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001819
1820void ceph_osdc_stop(struct ceph_osd_client *osdc)
1821{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001822 flush_workqueue(osdc->notify_wq);
1823 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001824 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001825 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001826 if (osdc->osdmap) {
1827 ceph_osdmap_destroy(osdc->osdmap);
1828 osdc->osdmap = NULL;
1829 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001830 remove_old_osds(osdc, 1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001831 WARN_ON(!RB_EMPTY_ROOT(&osdc->osds));
Sage Weilf24e9982009-10-06 11:31:10 -07001832 mempool_destroy(osdc->req_mempool);
1833 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001834 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001835}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001836EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001837
1838/*
1839 * Read some contiguous pages. If we cross a stripe boundary, shorten
1840 * *plen. Return number of bytes read, or error.
1841 */
1842int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1843 struct ceph_vino vino, struct ceph_file_layout *layout,
1844 u64 off, u64 *plen,
1845 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001846 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001847{
1848 struct ceph_osd_request *req;
1849 int rc = 0;
1850
1851 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1852 vino.snap, off, *plen);
1853 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1854 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1855 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001856 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001857 if (!req)
1858 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001859
1860 /* it may be a short read due to an object boundary */
1861 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001862
Sage Weilb7495fc2010-11-09 12:43:12 -08001863 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1864 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001865
1866 rc = ceph_osdc_start_request(osdc, req, false);
1867 if (!rc)
1868 rc = ceph_osdc_wait_request(osdc, req);
1869
1870 ceph_osdc_put_request(req);
1871 dout("readpages result %d\n", rc);
1872 return rc;
1873}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001874EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001875
1876/*
1877 * do a synchronous write on N pages
1878 */
1879int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1880 struct ceph_file_layout *layout,
1881 struct ceph_snap_context *snapc,
1882 u64 off, u64 len,
1883 u32 truncate_seq, u64 truncate_size,
1884 struct timespec *mtime,
1885 struct page **pages, int num_pages,
1886 int flags, int do_sync, bool nofail)
1887{
1888 struct ceph_osd_request *req;
1889 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001890 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001891
1892 BUG_ON(vino.snap != CEPH_NOSNAP);
1893 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1894 CEPH_OSD_OP_WRITE,
1895 flags | CEPH_OSD_FLAG_ONDISK |
1896 CEPH_OSD_FLAG_WRITE,
1897 snapc, do_sync,
1898 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001899 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001900 if (!req)
1901 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001902
1903 /* it may be a short write due to an object boundary */
1904 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001905 dout("writepages %llu~%llu (%d pages)\n", off, len,
1906 req->r_num_pages);
1907
1908 rc = ceph_osdc_start_request(osdc, req, nofail);
1909 if (!rc)
1910 rc = ceph_osdc_wait_request(osdc, req);
1911
1912 ceph_osdc_put_request(req);
1913 if (rc == 0)
1914 rc = len;
1915 dout("writepages result %d\n", rc);
1916 return rc;
1917}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001918EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001919
1920/*
1921 * handle incoming message
1922 */
1923static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1924{
1925 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001926 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001927 int type = le16_to_cpu(msg->hdr.type);
1928
1929 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001930 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001931 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001932
1933 switch (type) {
1934 case CEPH_MSG_OSD_MAP:
1935 ceph_osdc_handle_map(osdc, msg);
1936 break;
1937 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001938 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001939 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001940 case CEPH_MSG_WATCH_NOTIFY:
1941 handle_watch_notify(osdc, msg);
1942 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001943
1944 default:
1945 pr_err("received unknown message type %d %s\n", type,
1946 ceph_msg_type_name(type));
1947 }
Sage Weil4a32f932010-06-13 10:27:53 -07001948out:
Sage Weilf24e9982009-10-06 11:31:10 -07001949 ceph_msg_put(msg);
1950}
1951
Sage Weil5b3a4db2010-02-19 21:43:23 -08001952/*
Sage Weil21b667f2010-03-04 10:22:59 -08001953 * lookup and return message for incoming reply. set up reply message
1954 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001955 */
1956static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001957 struct ceph_msg_header *hdr,
1958 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001959{
1960 struct ceph_osd *osd = con->private;
1961 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001962 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001963 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001964 int front = le32_to_cpu(hdr->front_len);
1965 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001966 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07001967
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001968 tid = le64_to_cpu(hdr->tid);
1969 mutex_lock(&osdc->request_mutex);
1970 req = __lookup_request(osdc, tid);
1971 if (!req) {
1972 *skip = 1;
1973 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08001974 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08001975 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001976 goto out;
1977 }
Sage Weilc16e7862010-03-01 13:02:00 -08001978
1979 if (req->r_con_filling_msg) {
1980 dout("get_reply revoking msg %p from old con %p\n",
1981 req->r_reply, req->r_con_filling_msg);
1982 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1983 ceph_con_put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07001984 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001985 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001986
Sage Weilc16e7862010-03-01 13:02:00 -08001987 if (front > req->r_reply->front.iov_len) {
1988 pr_warning("get_reply front %d > preallocated %d\n",
1989 front, (int)req->r_reply->front.iov_len);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001990 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001991 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08001992 goto out;
1993 ceph_msg_put(req->r_reply);
1994 req->r_reply = m;
1995 }
1996 m = ceph_msg_get(req->r_reply);
1997
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001998 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08001999 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002000
2001 if (unlikely(req->r_num_pages < want)) {
2002 pr_warning("tid %lld reply %d > expected %d pages\n",
2003 tid, want, m->nr_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002004 *skip = 1;
2005 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002006 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002007 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002008 }
Sage Weil21b667f2010-03-04 10:22:59 -08002009 m->pages = req->r_pages;
2010 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002011 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002012#ifdef CONFIG_BLOCK
2013 m->bio = req->r_bio;
2014#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002015 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002016 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002017 req->r_con_filling_msg = ceph_con_get(con);
2018 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002019
2020out:
2021 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002022 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002023
2024}
2025
2026static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2027 struct ceph_msg_header *hdr,
2028 int *skip)
2029{
2030 struct ceph_osd *osd = con->private;
2031 int type = le16_to_cpu(hdr->type);
2032 int front = le32_to_cpu(hdr->front_len);
2033
2034 switch (type) {
2035 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002036 case CEPH_MSG_WATCH_NOTIFY:
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002037 return ceph_msg_new(type, front, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002038 case CEPH_MSG_OSD_OPREPLY:
2039 return get_reply(con, hdr, skip);
2040 default:
2041 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2042 osd->o_osd);
2043 *skip = 1;
2044 return NULL;
2045 }
Sage Weilf24e9982009-10-06 11:31:10 -07002046}
2047
2048/*
2049 * Wrappers to refcount containing ceph_osd struct
2050 */
2051static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2052{
2053 struct ceph_osd *osd = con->private;
2054 if (get_osd(osd))
2055 return con;
2056 return NULL;
2057}
2058
2059static void put_osd_con(struct ceph_connection *con)
2060{
2061 struct ceph_osd *osd = con->private;
2062 put_osd(osd);
2063}
2064
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002065/*
2066 * authentication
2067 */
2068static int get_authorizer(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002069 void **buf, int *len, int *proto,
2070 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002071{
2072 struct ceph_osd *o = con->private;
2073 struct ceph_osd_client *osdc = o->o_osdc;
2074 struct ceph_auth_client *ac = osdc->client->monc.auth;
2075 int ret = 0;
2076
2077 if (force_new && o->o_authorizer) {
2078 ac->ops->destroy_authorizer(ac, o->o_authorizer);
2079 o->o_authorizer = NULL;
2080 }
2081 if (o->o_authorizer == NULL) {
2082 ret = ac->ops->create_authorizer(
2083 ac, CEPH_ENTITY_TYPE_OSD,
2084 &o->o_authorizer,
2085 &o->o_authorizer_buf,
2086 &o->o_authorizer_buf_len,
2087 &o->o_authorizer_reply_buf,
2088 &o->o_authorizer_reply_buf_len);
2089 if (ret)
Sage Weil213c99e2010-08-03 10:25:11 -07002090 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002091 }
2092
2093 *proto = ac->protocol;
2094 *buf = o->o_authorizer_buf;
2095 *len = o->o_authorizer_buf_len;
2096 *reply_buf = o->o_authorizer_reply_buf;
2097 *reply_len = o->o_authorizer_reply_buf_len;
2098 return 0;
2099}
2100
2101
2102static int verify_authorizer_reply(struct ceph_connection *con, int len)
2103{
2104 struct ceph_osd *o = con->private;
2105 struct ceph_osd_client *osdc = o->o_osdc;
2106 struct ceph_auth_client *ac = osdc->client->monc.auth;
2107
2108 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
2109}
2110
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002111static int invalidate_authorizer(struct ceph_connection *con)
2112{
2113 struct ceph_osd *o = con->private;
2114 struct ceph_osd_client *osdc = o->o_osdc;
2115 struct ceph_auth_client *ac = osdc->client->monc.auth;
2116
2117 if (ac->ops->invalidate_authorizer)
2118 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2119
2120 return ceph_monc_validate_auth(&osdc->client->monc);
2121}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002122
Tobias Klauser9e327892010-05-20 10:40:19 +02002123static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002124 .get = get_osd_con,
2125 .put = put_osd_con,
2126 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002127 .get_authorizer = get_authorizer,
2128 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002129 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002130 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002131 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002132};