blob: 0edb43f1ef263f4f7889d11ac2069ba17ad32070 [file] [log] [blame]
Sage Weilf24e9982009-10-06 11:31:10 -07001#include "ceph_debug.h"
2
3#include <linux/err.h>
4#include <linux/highmem.h>
5#include <linux/mm.h>
6#include <linux/pagemap.h>
7#include <linux/slab.h>
8#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -07009#ifdef CONFIG_BLOCK
10#include <linux/bio.h>
11#endif
Sage Weilf24e9982009-10-06 11:31:10 -070012
13#include "super.h"
14#include "osd_client.h"
15#include "messenger.h"
16#include "decode.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080017#include "auth.h"
Yehuda Sadeh68b44762010-04-06 15:01:27 -070018#include "pagelist.h"
Sage Weilf24e9982009-10-06 11:31:10 -070019
Sage Weilc16e7862010-03-01 13:02:00 -080020#define OSD_OP_FRONT_LEN 4096
21#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080022
Tobias Klauser9e327892010-05-20 10:40:19 +020023static const struct ceph_connection_operations osd_con_ops;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -080024static int __kick_requests(struct ceph_osd_client *osdc,
25 struct ceph_osd *kickosd);
Sage Weilf24e9982009-10-06 11:31:10 -070026
27static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
28
Yehuda Sadeh68b44762010-04-06 15:01:27 -070029static int op_needs_trail(int op)
30{
31 switch (op) {
32 case CEPH_OSD_OP_GETXATTR:
33 case CEPH_OSD_OP_SETXATTR:
34 case CEPH_OSD_OP_CMPXATTR:
35 case CEPH_OSD_OP_CALL:
36 return 1;
37 default:
38 return 0;
39 }
40}
41
42static int op_has_extent(int op)
43{
44 return (op == CEPH_OSD_OP_READ ||
45 op == CEPH_OSD_OP_WRITE);
46}
47
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070048void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
49 struct ceph_file_layout *layout,
50 u64 snapid,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070051 u64 off, u64 *plen, u64 *bno,
52 struct ceph_osd_request *req,
53 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070054{
55 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Yehuda Sadeh68b44762010-04-06 15:01:27 -070056 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070057 u64 objoff, objlen; /* extent in object */
58
59 reqhead->snapid = cpu_to_le64(snapid);
60
61 /* object extent? */
Yehuda Sadeh68b44762010-04-06 15:01:27 -070062 ceph_calc_file_object_mapping(layout, off, plen, bno,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070063 &objoff, &objlen);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070064 if (*plen < orig_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070065 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070066 orig_len - *plen, off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070067
Yehuda Sadeh68b44762010-04-06 15:01:27 -070068 if (op_has_extent(op->op)) {
69 op->extent.offset = objoff;
70 op->extent.length = objlen;
71 }
72 req->r_num_pages = calc_pages_for(off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070073
74 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
75 *bno, objoff, objlen, req->r_num_pages);
76
77}
78
Sage Weilf24e9982009-10-06 11:31:10 -070079/*
80 * Implement client access to distributed object storage cluster.
81 *
82 * All data objects are stored within a cluster/cloud of OSDs, or
83 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
84 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
85 * remote daemons serving up and coordinating consistent and safe
86 * access to storage.
87 *
88 * Cluster membership and the mapping of data objects onto storage devices
89 * are described by the osd map.
90 *
91 * We keep track of pending OSD requests (read, write), resubmit
92 * requests to different OSDs when the cluster topology/data layout
93 * change, or retry the affected requests when the communications
94 * channel with an OSD is reset.
95 */
96
97/*
98 * calculate the mapping of a file extent onto an object, and fill out the
99 * request accordingly. shorten extent as necessary if it crosses an
100 * object boundary.
101 *
102 * fill osd op in request message.
103 */
104static void calc_layout(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700105 struct ceph_vino vino,
106 struct ceph_file_layout *layout,
Sage Weilf24e9982009-10-06 11:31:10 -0700107 u64 off, u64 *plen,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700108 struct ceph_osd_request *req,
109 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700110{
Sage Weilf24e9982009-10-06 11:31:10 -0700111 u64 bno;
112
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700113 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
114 plen, &bno, req, op);
Sage Weilf24e9982009-10-06 11:31:10 -0700115
116 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
117 req->r_oid_len = strlen(req->r_oid);
Sage Weilf24e9982009-10-06 11:31:10 -0700118}
119
Sage Weilf24e9982009-10-06 11:31:10 -0700120/*
121 * requests
122 */
Sage Weil415e49a2009-12-07 13:37:03 -0800123void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700124{
Sage Weil415e49a2009-12-07 13:37:03 -0800125 struct ceph_osd_request *req = container_of(kref,
126 struct ceph_osd_request,
127 r_kref);
128
129 if (req->r_request)
130 ceph_msg_put(req->r_request);
131 if (req->r_reply)
132 ceph_msg_put(req->r_reply);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800133 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -0800134 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800135 req->r_pages, req->r_con_filling_msg);
136 ceph_con_revoke_message(req->r_con_filling_msg,
137 req->r_reply);
138 ceph_con_put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800139 }
Sage Weil415e49a2009-12-07 13:37:03 -0800140 if (req->r_own_pages)
141 ceph_release_page_vector(req->r_pages,
142 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700143#ifdef CONFIG_BLOCK
144 if (req->r_bio)
145 bio_put(req->r_bio);
146#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800147 ceph_put_snap_context(req->r_snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700148 if (req->r_trail) {
149 ceph_pagelist_release(req->r_trail);
150 kfree(req->r_trail);
151 }
Sage Weil415e49a2009-12-07 13:37:03 -0800152 if (req->r_mempool)
153 mempool_free(req, req->r_osdc->req_mempool);
154 else
155 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700156}
157
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700158static int op_needs_trail(int op)
159{
160 switch (op) {
161 case CEPH_OSD_OP_GETXATTR:
162 case CEPH_OSD_OP_SETXATTR:
163 case CEPH_OSD_OP_CMPXATTR:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700164 case CEPH_OSD_OP_CALL:
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700165 return 1;
166 default:
167 return 0;
168 }
169}
170
171static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
172{
173 int i = 0;
174
175 if (needs_trail)
176 *needs_trail = 0;
177 while (ops[i].op) {
178 if (needs_trail && op_needs_trail(ops[i].op))
179 *needs_trail = 1;
180 i++;
181 }
182
183 return i;
184}
185
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700186struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
187 int flags,
188 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700189 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700190 bool use_mempool,
191 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700192 struct page **pages,
193 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700194{
195 struct ceph_osd_request *req;
196 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700197 int needs_trail;
198 int num_op = get_num_ops(ops, &needs_trail);
199 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700200
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700201 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700202
203 if (use_mempool) {
204 req = mempool_alloc(osdc->req_mempool, gfp_flags);
205 memset(req, 0, sizeof(*req));
206 } else {
207 req = kzalloc(sizeof(*req), gfp_flags);
208 }
209 if (req == NULL)
210 return NULL;
211
212 req->r_osdc = osdc;
213 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700214
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700215 kref_init(&req->r_kref);
216 init_completion(&req->r_completion);
217 init_completion(&req->r_safe_completion);
218 INIT_LIST_HEAD(&req->r_unsafe_item);
219 req->r_flags = flags;
220
221 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
222
223 /* create reply message */
224 if (use_mempool)
225 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
226 else
227 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
228 OSD_OPREPLY_FRONT_LEN, gfp_flags);
229 if (!msg) {
230 ceph_osdc_put_request(req);
231 return NULL;
232 }
233 req->r_reply = msg;
234
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700235 /* allocate space for the trailing data */
236 if (needs_trail) {
237 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
238 if (!req->r_trail) {
239 ceph_osdc_put_request(req);
240 return NULL;
241 }
242 ceph_pagelist_init(req->r_trail);
243 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700244 /* create request message; allow space for oid */
245 msg_size += 40;
246 if (snapc)
247 msg_size += sizeof(u64) * snapc->num_snaps;
248 if (use_mempool)
249 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
250 else
251 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
252 if (!msg) {
253 ceph_osdc_put_request(req);
254 return NULL;
255 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700256
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700257 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
258 memset(msg->front.iov_base, 0, msg->front.iov_len);
259
260 req->r_request = msg;
261 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700262#ifdef CONFIG_BLOCK
263 if (bio) {
264 req->r_bio = bio;
265 bio_get(req->r_bio);
266 }
267#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700268
269 return req;
270}
271
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700272static void osd_req_encode_op(struct ceph_osd_request *req,
273 struct ceph_osd_op *dst,
274 struct ceph_osd_req_op *src)
275{
276 dst->op = cpu_to_le16(src->op);
277
278 switch (dst->op) {
279 case CEPH_OSD_OP_READ:
280 case CEPH_OSD_OP_WRITE:
281 dst->extent.offset =
282 cpu_to_le64(src->extent.offset);
283 dst->extent.length =
284 cpu_to_le64(src->extent.length);
285 dst->extent.truncate_size =
286 cpu_to_le64(src->extent.truncate_size);
287 dst->extent.truncate_seq =
288 cpu_to_le32(src->extent.truncate_seq);
289 break;
290
291 case CEPH_OSD_OP_GETXATTR:
292 case CEPH_OSD_OP_SETXATTR:
293 case CEPH_OSD_OP_CMPXATTR:
294 BUG_ON(!req->r_trail);
295
296 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
297 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
298 dst->xattr.cmp_op = src->xattr.cmp_op;
299 dst->xattr.cmp_mode = src->xattr.cmp_mode;
300 ceph_pagelist_append(req->r_trail, src->xattr.name,
301 src->xattr.name_len);
302 ceph_pagelist_append(req->r_trail, src->xattr.val,
303 src->xattr.value_len);
304 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700305 case CEPH_OSD_OP_CALL:
306 BUG_ON(!req->r_trail);
307
308 dst->cls.class_len = src->cls.class_len;
309 dst->cls.method_len = src->cls.method_len;
310 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
311
312 ceph_pagelist_append(req->r_trail, src->cls.class_name,
313 src->cls.class_len);
314 ceph_pagelist_append(req->r_trail, src->cls.method_name,
315 src->cls.method_len);
316 ceph_pagelist_append(req->r_trail, src->cls.indata,
317 src->cls.indata_len);
318 break;
319 case CEPH_OSD_OP_ROLLBACK:
320 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
321 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700322 case CEPH_OSD_OP_STARTSYNC:
323 break;
324 default:
325 pr_err("unrecognized osd opcode %d\n", dst->op);
326 WARN_ON(1);
327 break;
328 }
329 dst->payload_len = cpu_to_le32(src->payload_len);
330}
331
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700332/*
333 * build new request AND message
334 *
335 */
336void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700337 u64 off, u64 *plen,
338 struct ceph_osd_req_op *src_ops,
339 struct ceph_snap_context *snapc,
340 struct timespec *mtime,
341 const char *oid,
342 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700343{
344 struct ceph_msg *msg = req->r_request;
345 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700346 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700347 struct ceph_osd_op *op;
348 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700349 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700350 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700351 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700352 u64 data_len = 0;
353 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700354
355 head = msg->front.iov_base;
356 op = (void *)(head + 1);
357 p = (void *)(op + num_op);
358
359 req->r_snapc = ceph_get_snap_context(snapc);
360
361 head->client_inc = cpu_to_le32(1); /* always, for now. */
362 head->flags = cpu_to_le32(flags);
363 if (flags & CEPH_OSD_FLAG_WRITE)
364 ceph_encode_timespec(&head->mtime, mtime);
365 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700366
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367
368 /* fill in oid */
369 head->object_len = cpu_to_le32(oid_len);
370 memcpy(p, oid, oid_len);
371 p += oid_len;
372
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700373 src_op = src_ops;
374 while (src_op->op) {
375 osd_req_encode_op(req, op, src_op);
376 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700377 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700379
380 if (req->r_trail)
381 data_len += req->r_trail->length;
382
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700383 if (snapc) {
384 head->snap_seq = cpu_to_le64(snapc->seq);
385 head->num_snaps = cpu_to_le32(snapc->num_snaps);
386 for (i = 0; i < snapc->num_snaps; i++) {
387 put_unaligned_le64(snapc->snaps[i], p);
388 p += sizeof(u64);
389 }
390 }
391
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700392 if (flags & CEPH_OSD_FLAG_WRITE) {
393 req->r_request->hdr.data_off = cpu_to_le16(off);
394 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
395 } else if (data_len) {
396 req->r_request->hdr.data_off = 0;
397 req->r_request->hdr.data_len = cpu_to_le32(data_len);
398 }
399
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700400 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
401 msg_size = p - msg->front.iov_base;
402 msg->front.iov_len = msg_size;
403 msg->hdr.front_len = cpu_to_le32(msg_size);
404 return;
405}
406
Sage Weilf24e9982009-10-06 11:31:10 -0700407/*
408 * build new request AND message, calculate layout, and adjust file
409 * extent as needed.
410 *
411 * if the file was recently truncated, we include information about its
412 * old and new size so that the object can be updated appropriately. (we
413 * avoid synchronously deleting truncated objects because it's slow.)
414 *
415 * if @do_sync, include a 'startsync' command so that the osd will flush
416 * data quickly.
417 */
418struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
419 struct ceph_file_layout *layout,
420 struct ceph_vino vino,
421 u64 off, u64 *plen,
422 int opcode, int flags,
423 struct ceph_snap_context *snapc,
424 int do_sync,
425 u32 truncate_seq,
426 u64 truncate_size,
427 struct timespec *mtime,
428 bool use_mempool, int num_reply)
429{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700430 struct ceph_osd_req_op ops[3];
431 struct ceph_osd_request *req;
432
433 ops[0].op = opcode;
434 ops[0].extent.truncate_seq = truncate_seq;
435 ops[0].extent.truncate_size = truncate_size;
436 ops[0].payload_len = 0;
437
438 if (do_sync) {
439 ops[1].op = CEPH_OSD_OP_STARTSYNC;
440 ops[1].payload_len = 0;
441 ops[2].op = 0;
442 } else
443 ops[1].op = 0;
444
445 req = ceph_osdc_alloc_request(osdc, flags,
446 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700447 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700448 GFP_NOFS, NULL, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700449 if (IS_ERR(req))
450 return req;
Sage Weilf24e9982009-10-06 11:31:10 -0700451
452 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700453 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700454 req->r_file_layout = *layout; /* keep a copy */
455
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700456 ceph_osdc_build_request(req, off, plen, ops,
457 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700458 mtime,
459 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700460
Sage Weilf24e9982009-10-06 11:31:10 -0700461 return req;
462}
463
464/*
465 * We keep osd requests in an rbtree, sorted by ->r_tid.
466 */
467static void __insert_request(struct ceph_osd_client *osdc,
468 struct ceph_osd_request *new)
469{
470 struct rb_node **p = &osdc->requests.rb_node;
471 struct rb_node *parent = NULL;
472 struct ceph_osd_request *req = NULL;
473
474 while (*p) {
475 parent = *p;
476 req = rb_entry(parent, struct ceph_osd_request, r_node);
477 if (new->r_tid < req->r_tid)
478 p = &(*p)->rb_left;
479 else if (new->r_tid > req->r_tid)
480 p = &(*p)->rb_right;
481 else
482 BUG();
483 }
484
485 rb_link_node(&new->r_node, parent, p);
486 rb_insert_color(&new->r_node, &osdc->requests);
487}
488
489static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
490 u64 tid)
491{
492 struct ceph_osd_request *req;
493 struct rb_node *n = osdc->requests.rb_node;
494
495 while (n) {
496 req = rb_entry(n, struct ceph_osd_request, r_node);
497 if (tid < req->r_tid)
498 n = n->rb_left;
499 else if (tid > req->r_tid)
500 n = n->rb_right;
501 else
502 return req;
503 }
504 return NULL;
505}
506
507static struct ceph_osd_request *
508__lookup_request_ge(struct ceph_osd_client *osdc,
509 u64 tid)
510{
511 struct ceph_osd_request *req;
512 struct rb_node *n = osdc->requests.rb_node;
513
514 while (n) {
515 req = rb_entry(n, struct ceph_osd_request, r_node);
516 if (tid < req->r_tid) {
517 if (!n->rb_left)
518 return req;
519 n = n->rb_left;
520 } else if (tid > req->r_tid) {
521 n = n->rb_right;
522 } else {
523 return req;
524 }
525 }
526 return NULL;
527}
528
529
530/*
Sage Weil81b024e2009-10-09 10:29:18 -0700531 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700532 */
533static void osd_reset(struct ceph_connection *con)
534{
535 struct ceph_osd *osd = con->private;
536 struct ceph_osd_client *osdc;
537
538 if (!osd)
539 return;
540 dout("osd_reset osd%d\n", osd->o_osd);
541 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700542 down_read(&osdc->map_sem);
543 kick_requests(osdc, osd);
544 up_read(&osdc->map_sem);
545}
546
547/*
548 * Track open sessions with osds.
549 */
550static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
551{
552 struct ceph_osd *osd;
553
554 osd = kzalloc(sizeof(*osd), GFP_NOFS);
555 if (!osd)
556 return NULL;
557
558 atomic_set(&osd->o_ref, 1);
559 osd->o_osdc = osdc;
560 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800561 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700562 osd->o_incarnation = 1;
563
564 ceph_con_init(osdc->client->msgr, &osd->o_con);
565 osd->o_con.private = osd;
566 osd->o_con.ops = &osd_con_ops;
567 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800568
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800569 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700570 return osd;
571}
572
573static struct ceph_osd *get_osd(struct ceph_osd *osd)
574{
575 if (atomic_inc_not_zero(&osd->o_ref)) {
576 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
577 atomic_read(&osd->o_ref));
578 return osd;
579 } else {
580 dout("get_osd %p FAIL\n", osd);
581 return NULL;
582 }
583}
584
585static void put_osd(struct ceph_osd *osd)
586{
587 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
588 atomic_read(&osd->o_ref) - 1);
Sage Weil79494d12010-05-27 14:15:49 -0700589 if (atomic_dec_and_test(&osd->o_ref)) {
590 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
591
592 if (osd->o_authorizer)
593 ac->ops->destroy_authorizer(ac, osd->o_authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700594 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700595 }
Sage Weilf24e9982009-10-06 11:31:10 -0700596}
597
598/*
599 * remove an osd from our map
600 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800601static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700602{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800603 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700604 BUG_ON(!list_empty(&osd->o_requests));
605 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800606 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700607 ceph_con_close(&osd->o_con);
608 put_osd(osd);
609}
610
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800611static void __move_osd_to_lru(struct ceph_osd_client *osdc,
612 struct ceph_osd *osd)
613{
614 dout("__move_osd_to_lru %p\n", osd);
615 BUG_ON(!list_empty(&osd->o_osd_lru));
616 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
617 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
618}
619
620static void __remove_osd_from_lru(struct ceph_osd *osd)
621{
622 dout("__remove_osd_from_lru %p\n", osd);
623 if (!list_empty(&osd->o_osd_lru))
624 list_del_init(&osd->o_osd_lru);
625}
626
627static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
628{
629 struct ceph_osd *osd, *nosd;
630
631 dout("__remove_old_osds %p\n", osdc);
632 mutex_lock(&osdc->request_mutex);
633 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
634 if (!remove_all && time_before(jiffies, osd->lru_ttl))
635 break;
636 __remove_osd(osdc, osd);
637 }
638 mutex_unlock(&osdc->request_mutex);
639}
640
Sage Weilf24e9982009-10-06 11:31:10 -0700641/*
642 * reset osd connect
643 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800644static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700645{
Sage Weil87b315a2010-03-22 14:51:18 -0700646 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700647 int ret = 0;
648
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800649 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700650 if (list_empty(&osd->o_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800651 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700652 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
653 &osd->o_con.peer_addr,
654 sizeof(osd->o_con.peer_addr)) == 0 &&
655 !ceph_con_opened(&osd->o_con)) {
656 dout(" osd addr hasn't changed and connection never opened,"
657 " letting msgr retry");
658 /* touch each r_stamp for handle_timeout()'s benfit */
659 list_for_each_entry(req, &osd->o_requests, r_osd_item)
660 req->r_stamp = jiffies;
661 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700662 } else {
663 ceph_con_close(&osd->o_con);
664 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
665 osd->o_incarnation++;
666 }
667 return ret;
668}
669
670static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
671{
672 struct rb_node **p = &osdc->osds.rb_node;
673 struct rb_node *parent = NULL;
674 struct ceph_osd *osd = NULL;
675
676 while (*p) {
677 parent = *p;
678 osd = rb_entry(parent, struct ceph_osd, o_node);
679 if (new->o_osd < osd->o_osd)
680 p = &(*p)->rb_left;
681 else if (new->o_osd > osd->o_osd)
682 p = &(*p)->rb_right;
683 else
684 BUG();
685 }
686
687 rb_link_node(&new->o_node, parent, p);
688 rb_insert_color(&new->o_node, &osdc->osds);
689}
690
691static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
692{
693 struct ceph_osd *osd;
694 struct rb_node *n = osdc->osds.rb_node;
695
696 while (n) {
697 osd = rb_entry(n, struct ceph_osd, o_node);
698 if (o < osd->o_osd)
699 n = n->rb_left;
700 else if (o > osd->o_osd)
701 n = n->rb_right;
702 else
703 return osd;
704 }
705 return NULL;
706}
707
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800708static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
709{
710 schedule_delayed_work(&osdc->timeout_work,
711 osdc->client->mount_args->osd_keepalive_timeout * HZ);
712}
713
714static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
715{
716 cancel_delayed_work(&osdc->timeout_work);
717}
Sage Weilf24e9982009-10-06 11:31:10 -0700718
719/*
720 * Register request, assign tid. If this is the first request, set up
721 * the timeout event.
722 */
723static void register_request(struct ceph_osd_client *osdc,
724 struct ceph_osd_request *req)
725{
Sage Weilf24e9982009-10-06 11:31:10 -0700726 mutex_lock(&osdc->request_mutex);
727 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800728 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800729 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700730
731 dout("register_request %p tid %lld\n", req, req->r_tid);
732 __insert_request(osdc, req);
733 ceph_osdc_get_request(req);
734 osdc->num_requests++;
735
Sage Weilf24e9982009-10-06 11:31:10 -0700736 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800737 dout(" first request, scheduling timeout\n");
738 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700739 }
740 mutex_unlock(&osdc->request_mutex);
741}
742
743/*
744 * called under osdc->request_mutex
745 */
746static void __unregister_request(struct ceph_osd_client *osdc,
747 struct ceph_osd_request *req)
748{
749 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
750 rb_erase(&req->r_node, &osdc->requests);
751 osdc->num_requests--;
752
Sage Weil0ba64782009-10-08 16:57:16 -0700753 if (req->r_osd) {
754 /* make sure the original request isn't in flight. */
755 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
756
757 list_del_init(&req->r_osd_item);
758 if (list_empty(&req->r_osd->o_requests))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800759 __move_osd_to_lru(osdc, req->r_osd);
Sage Weil0ba64782009-10-08 16:57:16 -0700760 req->r_osd = NULL;
761 }
Sage Weilf24e9982009-10-06 11:31:10 -0700762
763 ceph_osdc_put_request(req);
764
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800765 list_del_init(&req->r_req_lru_item);
766 if (osdc->num_requests == 0) {
767 dout(" no requests, canceling timeout\n");
768 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700769 }
770}
771
772/*
773 * Cancel a previously queued request message
774 */
775static void __cancel_request(struct ceph_osd_request *req)
776{
Sage Weil6bc18872010-09-27 10:18:52 -0700777 if (req->r_sent && req->r_osd) {
Sage Weilf24e9982009-10-06 11:31:10 -0700778 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
779 req->r_sent = 0;
780 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800781 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700782}
783
784/*
785 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
786 * (as needed), and set the request r_osd appropriately. If there is
787 * no up osd, set r_osd to NULL.
788 *
789 * Return 0 if unchanged, 1 if changed, or negative on error.
790 *
791 * Caller should hold map_sem for read and request_mutex.
792 */
793static int __map_osds(struct ceph_osd_client *osdc,
794 struct ceph_osd_request *req)
795{
796 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800797 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700798 int acting[CEPH_PG_MAX_SIZE];
799 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700800 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700801
802 dout("map_osds %p tid %lld\n", req, req->r_tid);
803 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
804 &req->r_file_layout, osdc->osdmap);
805 if (err)
806 return err;
Sage Weil51042122009-11-04 11:39:12 -0800807 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800808 req->r_pgid = pgid;
809
Sage Weild85b7052010-05-10 10:24:48 -0700810 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
811 if (err > 0) {
812 o = acting[0];
813 num = err;
814 }
Sage Weilf24e9982009-10-06 11:31:10 -0700815
816 if ((req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700817 req->r_sent >= req->r_osd->o_incarnation &&
818 req->r_num_pg_osds == num &&
819 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700820 (req->r_osd == NULL && o == -1))
821 return 0; /* no change */
822
Sage Weil51042122009-11-04 11:39:12 -0800823 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
824 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700825 req->r_osd ? req->r_osd->o_osd : -1);
826
Sage Weild85b7052010-05-10 10:24:48 -0700827 /* record full pg acting set */
828 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
829 req->r_num_pg_osds = num;
830
Sage Weilf24e9982009-10-06 11:31:10 -0700831 if (req->r_osd) {
832 __cancel_request(req);
833 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700834 req->r_osd = NULL;
835 }
836
837 req->r_osd = __lookup_osd(osdc, o);
838 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800839 err = -ENOMEM;
840 req->r_osd = create_osd(osdc);
841 if (!req->r_osd)
842 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -0700843
844 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
845 req->r_osd->o_osd = o;
846 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
847 __insert_osd(osdc, req->r_osd);
848
849 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
850 }
851
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800852 if (req->r_osd) {
853 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700854 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800855 }
Sage Weild85b7052010-05-10 10:24:48 -0700856 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -0700857
858out:
Sage Weilf24e9982009-10-06 11:31:10 -0700859 return err;
860}
861
862/*
863 * caller should hold map_sem (for read) and request_mutex
864 */
865static int __send_request(struct ceph_osd_client *osdc,
866 struct ceph_osd_request *req)
867{
868 struct ceph_osd_request_head *reqhead;
869 int err;
870
871 err = __map_osds(osdc, req);
872 if (err < 0)
873 return err;
874 if (req->r_osd == NULL) {
875 dout("send_request %p no up osds in pg\n", req);
876 ceph_monc_request_next_osdmap(&osdc->client->monc);
877 return 0;
878 }
879
880 dout("send_request %p tid %llu to osd%d flags %d\n",
881 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
882
883 reqhead = req->r_request->front.iov_base;
884 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
885 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
886 reqhead->reassert_version = req->r_reassert_version;
887
Sage Weil3dd72fc2010-03-22 14:42:30 -0700888 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -0700889 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700890
891 ceph_msg_get(req->r_request); /* send consumes a ref */
892 ceph_con_send(&req->r_osd->o_con, req->r_request);
893 req->r_sent = req->r_osd->o_incarnation;
894 return 0;
895}
896
897/*
898 * Timeout callback, called every N seconds when 1 or more osd
899 * requests has been active for more than N seconds. When this
900 * happens, we ping all OSDs with requests who have timed out to
901 * ensure any communications channel reset is detected. Reset the
902 * request timeouts another N seconds in the future as we go.
903 * Reschedule the timeout event another N seconds in future (unless
904 * there are no open requests).
905 */
906static void handle_timeout(struct work_struct *work)
907{
908 struct ceph_osd_client *osdc =
909 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800910 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700911 struct ceph_osd *osd;
Sage Weil6b805182009-10-27 11:50:50 -0700912 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800913 unsigned long keepalive =
914 osdc->client->mount_args->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -0700915 unsigned long last_stamp = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700916 struct rb_node *p;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800917 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -0700918
919 dout("timeout\n");
920 down_read(&osdc->map_sem);
921
922 ceph_monc_request_next_osdmap(&osdc->client->monc);
923
924 mutex_lock(&osdc->request_mutex);
925 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
926 req = rb_entry(p, struct ceph_osd_request, r_node);
927
928 if (req->r_resend) {
929 int err;
930
931 dout("osdc resending prev failed %lld\n", req->r_tid);
932 err = __send_request(osdc, req);
933 if (err)
934 dout("osdc failed again on %lld\n", req->r_tid);
935 else
936 req->r_resend = false;
937 continue;
938 }
939 }
Sage Weilf24e9982009-10-06 11:31:10 -0700940
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800941 /*
942 * reset osds that appear to be _really_ unresponsive. this
943 * is a failsafe measure.. we really shouldn't be getting to
944 * this point if the system is working properly. the monitors
945 * should mark the osd as failed and we should find out about
946 * it from an updated osd map.
947 */
Sage Weilf26e6812010-04-21 11:09:38 -0700948 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800949 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
950 r_req_lru_item);
951
Sage Weil3dd72fc2010-03-22 14:42:30 -0700952 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800953 break;
954
Sage Weil3dd72fc2010-03-22 14:42:30 -0700955 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800956 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -0700957 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800958
959 osd = req->r_osd;
960 BUG_ON(!osd);
961 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
962 req->r_tid, osd->o_osd);
963 __kick_requests(osdc, osd);
964 }
965
966 /*
967 * ping osds that are a bit slow. this ensures that if there
968 * is a break in the TCP connection we will notice, and reopen
969 * a connection with that osd (from the fault callback).
970 */
971 INIT_LIST_HEAD(&slow_osds);
972 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -0700973 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800974 break;
975
976 osd = req->r_osd;
977 BUG_ON(!osd);
978 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -0700979 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800980 list_move_tail(&osd->o_keepalive_item, &slow_osds);
981 }
982 while (!list_empty(&slow_osds)) {
983 osd = list_entry(slow_osds.next, struct ceph_osd,
984 o_keepalive_item);
985 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700986 ceph_con_keepalive(&osd->o_con);
987 }
988
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800989 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700990 mutex_unlock(&osdc->request_mutex);
991
992 up_read(&osdc->map_sem);
993}
994
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800995static void handle_osds_timeout(struct work_struct *work)
996{
997 struct ceph_osd_client *osdc =
998 container_of(work, struct ceph_osd_client,
999 osds_timeout_work.work);
1000 unsigned long delay =
1001 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
1002
1003 dout("osds timeout\n");
1004 down_read(&osdc->map_sem);
1005 remove_old_osds(osdc, 0);
1006 up_read(&osdc->map_sem);
1007
1008 schedule_delayed_work(&osdc->osds_timeout_work,
1009 round_jiffies_relative(delay));
1010}
1011
Sage Weilf24e9982009-10-06 11:31:10 -07001012/*
1013 * handle osd op reply. either call the callback if it is specified,
1014 * or do the completion to wake up the waiting thread.
1015 */
Sage Weil350b1c32009-12-22 10:45:45 -08001016static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1017 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001018{
1019 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1020 struct ceph_osd_request *req;
1021 u64 tid;
1022 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001023 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001024
Sage Weil6df058c2009-12-22 11:24:33 -08001025 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001026 if (msg->front.iov_len < sizeof(*rhead))
1027 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001028 numops = le32_to_cpu(rhead->num_ops);
1029 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001030 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001031 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1032 numops * sizeof(struct ceph_osd_op))
1033 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001034 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001035
1036 /* lookup */
1037 mutex_lock(&osdc->request_mutex);
1038 req = __lookup_request(osdc, tid);
1039 if (req == NULL) {
1040 dout("handle_reply tid %llu dne\n", tid);
1041 mutex_unlock(&osdc->request_mutex);
1042 return;
1043 }
1044 ceph_osdc_get_request(req);
1045 flags = le32_to_cpu(rhead->flags);
1046
Sage Weil350b1c32009-12-22 10:45:45 -08001047 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001048 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001049 * avoid a (safe but slower) revoke later.
1050 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001051 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001052 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001053 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -08001054 ceph_con_put(con);
1055 }
1056
Sage Weilf24e9982009-10-06 11:31:10 -07001057 if (!req->r_got_reply) {
1058 unsigned bytes;
1059
1060 req->r_result = le32_to_cpu(rhead->result);
1061 bytes = le32_to_cpu(msg->hdr.data_len);
1062 dout("handle_reply result %d bytes %d\n", req->r_result,
1063 bytes);
1064 if (req->r_result == 0)
1065 req->r_result = bytes;
1066
1067 /* in case this is a write and we need to replay, */
1068 req->r_reassert_version = rhead->reassert_version;
1069
1070 req->r_got_reply = 1;
1071 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1072 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001073 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001074 goto done;
1075 }
1076
1077 dout("handle_reply tid %llu flags %d\n", tid, flags);
1078
1079 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001080 if (result < 0 ||
1081 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001082 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1083 __unregister_request(osdc, req);
1084
1085 mutex_unlock(&osdc->request_mutex);
1086
1087 if (req->r_callback)
1088 req->r_callback(req, msg);
1089 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001090 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001091
1092 if (flags & CEPH_OSD_FLAG_ONDISK) {
1093 if (req->r_safe_callback)
1094 req->r_safe_callback(req, msg);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001095 complete_all(&req->r_safe_completion); /* fsync waiter */
Sage Weilf24e9982009-10-06 11:31:10 -07001096 }
1097
1098done:
1099 ceph_osdc_put_request(req);
1100 return;
1101
1102bad:
1103 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1104 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1105 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001106 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001107}
1108
1109
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001110static int __kick_requests(struct ceph_osd_client *osdc,
Sage Weilf24e9982009-10-06 11:31:10 -07001111 struct ceph_osd *kickosd)
1112{
1113 struct ceph_osd_request *req;
1114 struct rb_node *p, *n;
1115 int needmap = 0;
1116 int err;
1117
1118 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
Sage Weil153a0082010-02-15 12:11:51 -08001119 if (kickosd) {
Sage Weil87b315a2010-03-22 14:51:18 -07001120 err = __reset_osd(osdc, kickosd);
1121 if (err == -EAGAIN)
1122 return 1;
Sage Weil153a0082010-02-15 12:11:51 -08001123 } else {
Sage Weilf24e9982009-10-06 11:31:10 -07001124 for (p = rb_first(&osdc->osds); p; p = n) {
1125 struct ceph_osd *osd =
1126 rb_entry(p, struct ceph_osd, o_node);
1127
1128 n = rb_next(p);
1129 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
Sage Weil103e2d32010-01-07 16:12:36 -08001130 memcmp(&osd->o_con.peer_addr,
1131 ceph_osd_addr(osdc->osdmap,
1132 osd->o_osd),
1133 sizeof(struct ceph_entity_addr)) != 0)
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001134 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001135 }
1136 }
1137
1138 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1139 req = rb_entry(p, struct ceph_osd_request, r_node);
1140
1141 if (req->r_resend) {
1142 dout(" r_resend set on tid %llu\n", req->r_tid);
Sage Weil266673d2009-10-09 10:31:32 -07001143 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001144 goto kick;
1145 }
Sage Weil266673d2009-10-09 10:31:32 -07001146 if (req->r_osd && kickosd == req->r_osd) {
1147 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001148 goto kick;
Sage Weil266673d2009-10-09 10:31:32 -07001149 }
Sage Weilf24e9982009-10-06 11:31:10 -07001150
1151 err = __map_osds(osdc, req);
1152 if (err == 0)
1153 continue; /* no change */
1154 if (err < 0) {
1155 /*
1156 * FIXME: really, we should set the request
1157 * error and fail if this isn't a 'nofail'
1158 * request, but that's a fair bit more
1159 * complicated to do. So retry!
1160 */
1161 dout(" setting r_resend on %llu\n", req->r_tid);
1162 req->r_resend = true;
1163 continue;
1164 }
1165 if (req->r_osd == NULL) {
1166 dout("tid %llu maps to no valid osd\n", req->r_tid);
1167 needmap++; /* request a newer map */
1168 continue;
1169 }
1170
1171kick:
Sage Weilc1ea8822009-10-08 16:55:47 -07001172 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil12eadc12010-03-15 22:20:39 -07001173 req->r_osd ? req->r_osd->o_osd : -1);
Sage Weilf24e9982009-10-06 11:31:10 -07001174 req->r_flags |= CEPH_OSD_FLAG_RETRY;
1175 err = __send_request(osdc, req);
1176 if (err) {
1177 dout(" setting r_resend on %llu\n", req->r_tid);
1178 req->r_resend = true;
1179 }
1180 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001181
1182 return needmap;
1183}
1184
1185/*
1186 * Resubmit osd requests whose osd or osd address has changed. Request
1187 * a new osd map if osds are down, or we are otherwise unable to determine
1188 * how to direct a request.
1189 *
1190 * Close connections to down osds.
1191 *
1192 * If @who is specified, resubmit requests for that specific osd.
1193 *
1194 * Caller should hold map_sem for read and request_mutex.
1195 */
1196static void kick_requests(struct ceph_osd_client *osdc,
1197 struct ceph_osd *kickosd)
1198{
1199 int needmap;
1200
1201 mutex_lock(&osdc->request_mutex);
1202 needmap = __kick_requests(osdc, kickosd);
Sage Weilf24e9982009-10-06 11:31:10 -07001203 mutex_unlock(&osdc->request_mutex);
1204
1205 if (needmap) {
1206 dout("%d requests for down osds, need new map\n", needmap);
1207 ceph_monc_request_next_osdmap(&osdc->client->monc);
1208 }
Sage Weilf24e9982009-10-06 11:31:10 -07001209
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001210}
Sage Weilf24e9982009-10-06 11:31:10 -07001211/*
1212 * Process updated osd map.
1213 *
1214 * The message contains any number of incremental and full maps, normally
1215 * indicating some sort of topology change in the cluster. Kick requests
1216 * off to different OSDs as needed.
1217 */
1218void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1219{
1220 void *p, *end, *next;
1221 u32 nr_maps, maplen;
1222 u32 epoch;
1223 struct ceph_osdmap *newmap = NULL, *oldmap;
1224 int err;
1225 struct ceph_fsid fsid;
1226
1227 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1228 p = msg->front.iov_base;
1229 end = p + msg->front.iov_len;
1230
1231 /* verify fsid */
1232 ceph_decode_need(&p, end, sizeof(fsid), bad);
1233 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001234 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1235 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001236
1237 down_write(&osdc->map_sem);
1238
1239 /* incremental maps */
1240 ceph_decode_32_safe(&p, end, nr_maps, bad);
1241 dout(" %d inc maps\n", nr_maps);
1242 while (nr_maps > 0) {
1243 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001244 epoch = ceph_decode_32(&p);
1245 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001246 ceph_decode_need(&p, end, maplen, bad);
1247 next = p + maplen;
1248 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1249 dout("applying incremental map %u len %d\n",
1250 epoch, maplen);
1251 newmap = osdmap_apply_incremental(&p, next,
1252 osdc->osdmap,
1253 osdc->client->msgr);
1254 if (IS_ERR(newmap)) {
1255 err = PTR_ERR(newmap);
1256 goto bad;
1257 }
Sage Weil30dc6382009-12-21 14:49:37 -08001258 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001259 if (newmap != osdc->osdmap) {
1260 ceph_osdmap_destroy(osdc->osdmap);
1261 osdc->osdmap = newmap;
1262 }
1263 } else {
1264 dout("ignoring incremental map %u len %d\n",
1265 epoch, maplen);
1266 }
1267 p = next;
1268 nr_maps--;
1269 }
1270 if (newmap)
1271 goto done;
1272
1273 /* full maps */
1274 ceph_decode_32_safe(&p, end, nr_maps, bad);
1275 dout(" %d full maps\n", nr_maps);
1276 while (nr_maps) {
1277 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001278 epoch = ceph_decode_32(&p);
1279 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001280 ceph_decode_need(&p, end, maplen, bad);
1281 if (nr_maps > 1) {
1282 dout("skipping non-latest full map %u len %d\n",
1283 epoch, maplen);
1284 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1285 dout("skipping full map %u len %d, "
1286 "older than our %u\n", epoch, maplen,
1287 osdc->osdmap->epoch);
1288 } else {
1289 dout("taking full map %u len %d\n", epoch, maplen);
1290 newmap = osdmap_decode(&p, p+maplen);
1291 if (IS_ERR(newmap)) {
1292 err = PTR_ERR(newmap);
1293 goto bad;
1294 }
Sage Weil30dc6382009-12-21 14:49:37 -08001295 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001296 oldmap = osdc->osdmap;
1297 osdc->osdmap = newmap;
1298 if (oldmap)
1299 ceph_osdmap_destroy(oldmap);
1300 }
1301 p += maplen;
1302 nr_maps--;
1303 }
1304
1305done:
1306 downgrade_write(&osdc->map_sem);
1307 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1308 if (newmap)
1309 kick_requests(osdc, NULL);
1310 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001311 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001312 return;
1313
1314bad:
1315 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001316 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001317 up_write(&osdc->map_sem);
1318 return;
1319}
1320
Sage Weilf24e9982009-10-06 11:31:10 -07001321/*
1322 * Register request, send initial attempt.
1323 */
1324int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1325 struct ceph_osd_request *req,
1326 bool nofail)
1327{
Sage Weilc1ea8822009-10-08 16:55:47 -07001328 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001329
1330 req->r_request->pages = req->r_pages;
1331 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001332#ifdef CONFIG_BLOCK
1333 req->r_request->bio = req->r_bio;
1334#endif
1335 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001336
1337 register_request(osdc, req);
1338
1339 down_read(&osdc->map_sem);
1340 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001341 /*
1342 * a racing kick_requests() may have sent the message for us
1343 * while we dropped request_mutex above, so only send now if
1344 * the request still han't been touched yet.
1345 */
1346 if (req->r_sent == 0) {
1347 rc = __send_request(osdc, req);
1348 if (rc) {
1349 if (nofail) {
1350 dout("osdc_start_request failed send, "
1351 " marking %lld\n", req->r_tid);
1352 req->r_resend = true;
1353 rc = 0;
1354 } else {
1355 __unregister_request(osdc, req);
1356 }
Sage Weilf24e9982009-10-06 11:31:10 -07001357 }
1358 }
1359 mutex_unlock(&osdc->request_mutex);
1360 up_read(&osdc->map_sem);
1361 return rc;
1362}
1363
1364/*
1365 * wait for a request to complete
1366 */
1367int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1368 struct ceph_osd_request *req)
1369{
1370 int rc;
1371
1372 rc = wait_for_completion_interruptible(&req->r_completion);
1373 if (rc < 0) {
1374 mutex_lock(&osdc->request_mutex);
1375 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001376 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001377 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001378 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001379 return rc;
1380 }
1381
1382 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1383 return req->r_result;
1384}
1385
1386/*
1387 * sync - wait for all in-flight requests to flush. avoid starvation.
1388 */
1389void ceph_osdc_sync(struct ceph_osd_client *osdc)
1390{
1391 struct ceph_osd_request *req;
1392 u64 last_tid, next_tid = 0;
1393
1394 mutex_lock(&osdc->request_mutex);
1395 last_tid = osdc->last_tid;
1396 while (1) {
1397 req = __lookup_request_ge(osdc, next_tid);
1398 if (!req)
1399 break;
1400 if (req->r_tid > last_tid)
1401 break;
1402
1403 next_tid = req->r_tid + 1;
1404 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1405 continue;
1406
1407 ceph_osdc_get_request(req);
1408 mutex_unlock(&osdc->request_mutex);
1409 dout("sync waiting on tid %llu (last is %llu)\n",
1410 req->r_tid, last_tid);
1411 wait_for_completion(&req->r_safe_completion);
1412 mutex_lock(&osdc->request_mutex);
1413 ceph_osdc_put_request(req);
1414 }
1415 mutex_unlock(&osdc->request_mutex);
1416 dout("sync done (thru tid %llu)\n", last_tid);
1417}
1418
1419/*
1420 * init, shutdown
1421 */
1422int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1423{
1424 int err;
1425
1426 dout("init\n");
1427 osdc->client = client;
1428 osdc->osdmap = NULL;
1429 init_rwsem(&osdc->map_sem);
1430 init_completion(&osdc->map_waiters);
1431 osdc->last_requested_map = 0;
1432 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001433 osdc->last_tid = 0;
1434 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001435 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001436 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001437 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001438 osdc->num_requests = 0;
1439 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001440 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1441
1442 schedule_delayed_work(&osdc->osds_timeout_work,
1443 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001444
Sage Weil5f44f142009-11-18 14:52:18 -08001445 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001446 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1447 sizeof(struct ceph_osd_request));
1448 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001449 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001450
Sage Weil4f482802010-04-24 09:56:35 -07001451 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1452 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001453 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001454 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001455 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001456 OSD_OPREPLY_FRONT_LEN, 10, true,
1457 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001458 if (err < 0)
1459 goto out_msgpool;
Sage Weilf24e9982009-10-06 11:31:10 -07001460 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001461
Sage Weilc16e7862010-03-01 13:02:00 -08001462out_msgpool:
1463 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001464out_mempool:
1465 mempool_destroy(osdc->req_mempool);
1466out:
1467 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001468}
1469
1470void ceph_osdc_stop(struct ceph_osd_client *osdc)
1471{
1472 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001473 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001474 if (osdc->osdmap) {
1475 ceph_osdmap_destroy(osdc->osdmap);
1476 osdc->osdmap = NULL;
1477 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001478 remove_old_osds(osdc, 1);
Sage Weilf24e9982009-10-06 11:31:10 -07001479 mempool_destroy(osdc->req_mempool);
1480 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001481 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001482}
1483
1484/*
1485 * Read some contiguous pages. If we cross a stripe boundary, shorten
1486 * *plen. Return number of bytes read, or error.
1487 */
1488int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1489 struct ceph_vino vino, struct ceph_file_layout *layout,
1490 u64 off, u64 *plen,
1491 u32 truncate_seq, u64 truncate_size,
1492 struct page **pages, int num_pages)
1493{
1494 struct ceph_osd_request *req;
1495 int rc = 0;
1496
1497 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1498 vino.snap, off, *plen);
1499 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1500 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1501 NULL, 0, truncate_seq, truncate_size, NULL,
1502 false, 1);
Sage Weila79832f2010-04-01 16:06:19 -07001503 if (!req)
1504 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001505
1506 /* it may be a short read due to an object boundary */
1507 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001508
1509 dout("readpages final extent is %llu~%llu (%d pages)\n",
1510 off, *plen, req->r_num_pages);
1511
1512 rc = ceph_osdc_start_request(osdc, req, false);
1513 if (!rc)
1514 rc = ceph_osdc_wait_request(osdc, req);
1515
1516 ceph_osdc_put_request(req);
1517 dout("readpages result %d\n", rc);
1518 return rc;
1519}
1520
1521/*
1522 * do a synchronous write on N pages
1523 */
1524int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1525 struct ceph_file_layout *layout,
1526 struct ceph_snap_context *snapc,
1527 u64 off, u64 len,
1528 u32 truncate_seq, u64 truncate_size,
1529 struct timespec *mtime,
1530 struct page **pages, int num_pages,
1531 int flags, int do_sync, bool nofail)
1532{
1533 struct ceph_osd_request *req;
1534 int rc = 0;
1535
1536 BUG_ON(vino.snap != CEPH_NOSNAP);
1537 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1538 CEPH_OSD_OP_WRITE,
1539 flags | CEPH_OSD_FLAG_ONDISK |
1540 CEPH_OSD_FLAG_WRITE,
1541 snapc, do_sync,
1542 truncate_seq, truncate_size, mtime,
1543 nofail, 1);
Sage Weila79832f2010-04-01 16:06:19 -07001544 if (!req)
1545 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001546
1547 /* it may be a short write due to an object boundary */
1548 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001549 dout("writepages %llu~%llu (%d pages)\n", off, len,
1550 req->r_num_pages);
1551
1552 rc = ceph_osdc_start_request(osdc, req, nofail);
1553 if (!rc)
1554 rc = ceph_osdc_wait_request(osdc, req);
1555
1556 ceph_osdc_put_request(req);
1557 if (rc == 0)
1558 rc = len;
1559 dout("writepages result %d\n", rc);
1560 return rc;
1561}
1562
1563/*
1564 * handle incoming message
1565 */
1566static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1567{
1568 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001569 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001570 int type = le16_to_cpu(msg->hdr.type);
1571
1572 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001573 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001574 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001575
1576 switch (type) {
1577 case CEPH_MSG_OSD_MAP:
1578 ceph_osdc_handle_map(osdc, msg);
1579 break;
1580 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001581 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001582 break;
1583
1584 default:
1585 pr_err("received unknown message type %d %s\n", type,
1586 ceph_msg_type_name(type));
1587 }
Sage Weil4a32f932010-06-13 10:27:53 -07001588out:
Sage Weilf24e9982009-10-06 11:31:10 -07001589 ceph_msg_put(msg);
1590}
1591
Sage Weil5b3a4db2010-02-19 21:43:23 -08001592/*
Sage Weil21b667f2010-03-04 10:22:59 -08001593 * lookup and return message for incoming reply. set up reply message
1594 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001595 */
1596static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001597 struct ceph_msg_header *hdr,
1598 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001599{
1600 struct ceph_osd *osd = con->private;
1601 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001602 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001603 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001604 int front = le32_to_cpu(hdr->front_len);
1605 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001606 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07001607
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001608 tid = le64_to_cpu(hdr->tid);
1609 mutex_lock(&osdc->request_mutex);
1610 req = __lookup_request(osdc, tid);
1611 if (!req) {
1612 *skip = 1;
1613 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08001614 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08001615 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001616 goto out;
1617 }
Sage Weilc16e7862010-03-01 13:02:00 -08001618
1619 if (req->r_con_filling_msg) {
1620 dout("get_reply revoking msg %p from old con %p\n",
1621 req->r_reply, req->r_con_filling_msg);
1622 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1623 ceph_con_put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07001624 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001625 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001626
Sage Weilc16e7862010-03-01 13:02:00 -08001627 if (front > req->r_reply->front.iov_len) {
1628 pr_warning("get_reply front %d > preallocated %d\n",
1629 front, (int)req->r_reply->front.iov_len);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001630 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07001631 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08001632 goto out;
1633 ceph_msg_put(req->r_reply);
1634 req->r_reply = m;
1635 }
1636 m = ceph_msg_get(req->r_reply);
1637
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001638 if (data_len > 0) {
Sage Weil21b667f2010-03-04 10:22:59 -08001639 unsigned data_off = le16_to_cpu(hdr->data_off);
1640 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
1641
1642 if (unlikely(req->r_num_pages < want)) {
1643 pr_warning("tid %lld reply %d > expected %d pages\n",
1644 tid, want, m->nr_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001645 *skip = 1;
1646 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07001647 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08001648 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001649 }
Sage Weil21b667f2010-03-04 10:22:59 -08001650 m->pages = req->r_pages;
1651 m->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001652#ifdef CONFIG_BLOCK
1653 m->bio = req->r_bio;
1654#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001655 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08001656 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08001657 req->r_con_filling_msg = ceph_con_get(con);
1658 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001659
1660out:
1661 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001662 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001663
1664}
1665
1666static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1667 struct ceph_msg_header *hdr,
1668 int *skip)
1669{
1670 struct ceph_osd *osd = con->private;
1671 int type = le16_to_cpu(hdr->type);
1672 int front = le32_to_cpu(hdr->front_len);
1673
1674 switch (type) {
1675 case CEPH_MSG_OSD_MAP:
Yehuda Sadeh34d23762010-04-06 14:33:58 -07001676 return ceph_msg_new(type, front, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -08001677 case CEPH_MSG_OSD_OPREPLY:
1678 return get_reply(con, hdr, skip);
1679 default:
1680 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1681 osd->o_osd);
1682 *skip = 1;
1683 return NULL;
1684 }
Sage Weilf24e9982009-10-06 11:31:10 -07001685}
1686
1687/*
1688 * Wrappers to refcount containing ceph_osd struct
1689 */
1690static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1691{
1692 struct ceph_osd *osd = con->private;
1693 if (get_osd(osd))
1694 return con;
1695 return NULL;
1696}
1697
1698static void put_osd_con(struct ceph_connection *con)
1699{
1700 struct ceph_osd *osd = con->private;
1701 put_osd(osd);
1702}
1703
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001704/*
1705 * authentication
1706 */
1707static int get_authorizer(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07001708 void **buf, int *len, int *proto,
1709 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001710{
1711 struct ceph_osd *o = con->private;
1712 struct ceph_osd_client *osdc = o->o_osdc;
1713 struct ceph_auth_client *ac = osdc->client->monc.auth;
1714 int ret = 0;
1715
1716 if (force_new && o->o_authorizer) {
1717 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1718 o->o_authorizer = NULL;
1719 }
1720 if (o->o_authorizer == NULL) {
1721 ret = ac->ops->create_authorizer(
1722 ac, CEPH_ENTITY_TYPE_OSD,
1723 &o->o_authorizer,
1724 &o->o_authorizer_buf,
1725 &o->o_authorizer_buf_len,
1726 &o->o_authorizer_reply_buf,
1727 &o->o_authorizer_reply_buf_len);
1728 if (ret)
Sage Weil213c99e2010-08-03 10:25:11 -07001729 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001730 }
1731
1732 *proto = ac->protocol;
1733 *buf = o->o_authorizer_buf;
1734 *len = o->o_authorizer_buf_len;
1735 *reply_buf = o->o_authorizer_reply_buf;
1736 *reply_len = o->o_authorizer_reply_buf_len;
1737 return 0;
1738}
1739
1740
1741static int verify_authorizer_reply(struct ceph_connection *con, int len)
1742{
1743 struct ceph_osd *o = con->private;
1744 struct ceph_osd_client *osdc = o->o_osdc;
1745 struct ceph_auth_client *ac = osdc->client->monc.auth;
1746
1747 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1748}
1749
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001750static int invalidate_authorizer(struct ceph_connection *con)
1751{
1752 struct ceph_osd *o = con->private;
1753 struct ceph_osd_client *osdc = o->o_osdc;
1754 struct ceph_auth_client *ac = osdc->client->monc.auth;
1755
1756 if (ac->ops->invalidate_authorizer)
1757 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1758
1759 return ceph_monc_validate_auth(&osdc->client->monc);
1760}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001761
Tobias Klauser9e327892010-05-20 10:40:19 +02001762static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07001763 .get = get_osd_con,
1764 .put = put_osd_con,
1765 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001766 .get_authorizer = get_authorizer,
1767 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001768 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07001769 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07001770 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07001771};