blob: 3a631f27cc9e32752e5850be3792aee054fa503b [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>
9
10#include "super.h"
11#include "osd_client.h"
12#include "messenger.h"
13#include "decode.h"
Sage Weil4e7a5dc2009-11-18 16:19:57 -080014#include "auth.h"
Sage Weilf24e9982009-10-06 11:31:10 -070015
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080016#define OSD_REPLY_RESERVE_FRONT_LEN 512
17
Sage Weilf24e9982009-10-06 11:31:10 -070018const static struct ceph_connection_operations osd_con_ops;
19
20static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
21
22/*
23 * Implement client access to distributed object storage cluster.
24 *
25 * All data objects are stored within a cluster/cloud of OSDs, or
26 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
27 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
28 * remote daemons serving up and coordinating consistent and safe
29 * access to storage.
30 *
31 * Cluster membership and the mapping of data objects onto storage devices
32 * are described by the osd map.
33 *
34 * We keep track of pending OSD requests (read, write), resubmit
35 * requests to different OSDs when the cluster topology/data layout
36 * change, or retry the affected requests when the communications
37 * channel with an OSD is reset.
38 */
39
40/*
41 * calculate the mapping of a file extent onto an object, and fill out the
42 * request accordingly. shorten extent as necessary if it crosses an
43 * object boundary.
44 *
45 * fill osd op in request message.
46 */
47static void calc_layout(struct ceph_osd_client *osdc,
48 struct ceph_vino vino, struct ceph_file_layout *layout,
49 u64 off, u64 *plen,
50 struct ceph_osd_request *req)
51{
52 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
53 struct ceph_osd_op *op = (void *)(reqhead + 1);
54 u64 orig_len = *plen;
55 u64 objoff, objlen; /* extent in object */
56 u64 bno;
57
58 reqhead->snapid = cpu_to_le64(vino.snap);
59
60 /* object extent? */
61 ceph_calc_file_object_mapping(layout, off, plen, &bno,
62 &objoff, &objlen);
63 if (*plen < orig_len)
64 dout(" skipping last %llu, final file extent %llu~%llu\n",
65 orig_len - *plen, off, *plen);
66
67 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
68 req->r_oid_len = strlen(req->r_oid);
69
70 op->extent.offset = cpu_to_le64(objoff);
71 op->extent.length = cpu_to_le64(objlen);
72 req->r_num_pages = calc_pages_for(off, *plen);
73
74 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
75 req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
76}
77
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080078static void remove_replies(struct ceph_osd_request *req)
79{
80 int i;
81 int max = ARRAY_SIZE(req->replies);
82
83 for (i=0; i<max; i++) {
84 if (req->replies[i])
85 ceph_msg_put(req->replies[i]);
86 }
87}
Sage Weilf24e9982009-10-06 11:31:10 -070088
89/*
90 * requests
91 */
Sage Weil415e49a2009-12-07 13:37:03 -080092void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -070093{
Sage Weil415e49a2009-12-07 13:37:03 -080094 struct ceph_osd_request *req = container_of(kref,
95 struct ceph_osd_request,
96 r_kref);
97
98 if (req->r_request)
99 ceph_msg_put(req->r_request);
100 if (req->r_reply)
101 ceph_msg_put(req->r_reply);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800102 remove_replies(req);
103 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -0800104 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800105 req->r_pages, req->r_con_filling_msg);
106 ceph_con_revoke_message(req->r_con_filling_msg,
107 req->r_reply);
108 ceph_con_put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800109 }
Sage Weil415e49a2009-12-07 13:37:03 -0800110 if (req->r_own_pages)
111 ceph_release_page_vector(req->r_pages,
112 req->r_num_pages);
113 ceph_put_snap_context(req->r_snapc);
114 if (req->r_mempool)
115 mempool_free(req, req->r_osdc->req_mempool);
116 else
117 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700118}
119
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800120static int alloc_replies(struct ceph_osd_request *req, int num_reply)
121{
122 int i;
123 int max = ARRAY_SIZE(req->replies);
124
125 BUG_ON(num_reply > max);
126
127 for (i=0; i<num_reply; i++) {
128 req->replies[i] = ceph_msg_new(0, OSD_REPLY_RESERVE_FRONT_LEN, 0, 0, NULL);
129 if (IS_ERR(req->replies[i])) {
130 int j;
131 int err = PTR_ERR(req->replies[i]);
132 for (j = 0; j<=i; j++) {
133 ceph_msg_put(req->replies[j]);
134 }
135 return err;
136 }
137 }
138
139 for (; i<max; i++) {
140 req->replies[i] = NULL;
141 }
142
143 req->cur_reply = 0;
144
145 return 0;
146}
147
148static struct ceph_msg *__get_next_reply(struct ceph_connection *con,
149 struct ceph_osd_request *req,
150 int front_len)
151{
152 struct ceph_msg *reply;
153 if (req->r_con_filling_msg) {
154 dout("revoking reply msg %p from old con %p\n", req->r_reply,
155 req->r_con_filling_msg);
156 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
157 ceph_con_put(req->r_con_filling_msg);
158 req->cur_reply = 0;
159 }
160 reply = req->replies[req->cur_reply];
161 if (!reply || front_len > OSD_REPLY_RESERVE_FRONT_LEN) {
162 /* maybe we can allocate it now? */
163 reply = ceph_msg_new(0, front_len, 0, 0, NULL);
164 if (!reply || IS_ERR(reply)) {
165 pr_err(" reply alloc failed, front_len=%d\n", front_len);
166 return ERR_PTR(-ENOMEM);
167 }
168 }
169 req->r_con_filling_msg = ceph_con_get(con);
170 req->r_reply = ceph_msg_get(reply); /* for duration of read over socket */
171 return ceph_msg_get(reply);
172}
173
Sage Weilf24e9982009-10-06 11:31:10 -0700174/*
175 * build new request AND message, calculate layout, and adjust file
176 * extent as needed.
177 *
178 * if the file was recently truncated, we include information about its
179 * old and new size so that the object can be updated appropriately. (we
180 * avoid synchronously deleting truncated objects because it's slow.)
181 *
182 * if @do_sync, include a 'startsync' command so that the osd will flush
183 * data quickly.
184 */
185struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
186 struct ceph_file_layout *layout,
187 struct ceph_vino vino,
188 u64 off, u64 *plen,
189 int opcode, int flags,
190 struct ceph_snap_context *snapc,
191 int do_sync,
192 u32 truncate_seq,
193 u64 truncate_size,
194 struct timespec *mtime,
195 bool use_mempool, int num_reply)
196{
197 struct ceph_osd_request *req;
198 struct ceph_msg *msg;
199 struct ceph_osd_request_head *head;
200 struct ceph_osd_op *op;
201 void *p;
Yehuda Sadeh0c948992010-02-01 16:10:45 -0800202 int num_op = 1 + do_sync;
Sage Weilf24e9982009-10-06 11:31:10 -0700203 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
204 int err, i;
Sage Weilf24e9982009-10-06 11:31:10 -0700205
206 if (use_mempool) {
207 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
208 memset(req, 0, sizeof(*req));
209 } else {
210 req = kzalloc(sizeof(*req), GFP_NOFS);
211 }
212 if (req == NULL)
213 return ERR_PTR(-ENOMEM);
214
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800215 err = alloc_replies(req, num_reply);
Sage Weilf24e9982009-10-06 11:31:10 -0700216 if (err) {
217 ceph_osdc_put_request(req);
218 return ERR_PTR(-ENOMEM);
219 }
Yehuda Sadeh93c20d92009-12-15 09:50:36 -0800220 req->r_num_prealloc_reply = num_reply;
Sage Weilf24e9982009-10-06 11:31:10 -0700221
222 req->r_osdc = osdc;
223 req->r_mempool = use_mempool;
Sage Weil415e49a2009-12-07 13:37:03 -0800224 kref_init(&req->r_kref);
Sage Weilf24e9982009-10-06 11:31:10 -0700225 init_completion(&req->r_completion);
226 init_completion(&req->r_safe_completion);
227 INIT_LIST_HEAD(&req->r_unsafe_item);
228 req->r_flags = flags;
229
230 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
231
232 /* create message; allow space for oid */
233 msg_size += 40;
234 if (snapc)
235 msg_size += sizeof(u64) * snapc->num_snaps;
236 if (use_mempool)
Sage Weil8f3bc052009-10-14 17:36:07 -0700237 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
Sage Weilf24e9982009-10-06 11:31:10 -0700238 else
239 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
240 if (IS_ERR(msg)) {
Sage Weilf24e9982009-10-06 11:31:10 -0700241 ceph_osdc_put_request(req);
242 return ERR_PTR(PTR_ERR(msg));
243 }
244 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
245 memset(msg->front.iov_base, 0, msg->front.iov_len);
246 head = msg->front.iov_base;
247 op = (void *)(head + 1);
248 p = (void *)(op + num_op);
249
250 req->r_request = msg;
251 req->r_snapc = ceph_get_snap_context(snapc);
252
253 head->client_inc = cpu_to_le32(1); /* always, for now. */
254 head->flags = cpu_to_le32(flags);
255 if (flags & CEPH_OSD_FLAG_WRITE)
256 ceph_encode_timespec(&head->mtime, mtime);
257 head->num_ops = cpu_to_le16(num_op);
258 op->op = cpu_to_le16(opcode);
259
260 /* calculate max write size */
261 calc_layout(osdc, vino, layout, off, plen, req);
262 req->r_file_layout = *layout; /* keep a copy */
263
264 if (flags & CEPH_OSD_FLAG_WRITE) {
265 req->r_request->hdr.data_off = cpu_to_le16(off);
266 req->r_request->hdr.data_len = cpu_to_le32(*plen);
267 op->payload_len = cpu_to_le32(*plen);
268 }
Yehuda Sadeh0c948992010-02-01 16:10:45 -0800269 op->extent.truncate_size = cpu_to_le64(truncate_size);
270 op->extent.truncate_seq = cpu_to_le32(truncate_seq);
Sage Weilf24e9982009-10-06 11:31:10 -0700271
272 /* fill in oid */
273 head->object_len = cpu_to_le32(req->r_oid_len);
274 memcpy(p, req->r_oid, req->r_oid_len);
275 p += req->r_oid_len;
276
Sage Weilf24e9982009-10-06 11:31:10 -0700277 if (do_sync) {
278 op++;
279 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
280 }
281 if (snapc) {
282 head->snap_seq = cpu_to_le64(snapc->seq);
283 head->num_snaps = cpu_to_le32(snapc->num_snaps);
284 for (i = 0; i < snapc->num_snaps; i++) {
285 put_unaligned_le64(snapc->snaps[i], p);
286 p += sizeof(u64);
287 }
288 }
289
290 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
291 return req;
292}
293
294/*
295 * We keep osd requests in an rbtree, sorted by ->r_tid.
296 */
297static void __insert_request(struct ceph_osd_client *osdc,
298 struct ceph_osd_request *new)
299{
300 struct rb_node **p = &osdc->requests.rb_node;
301 struct rb_node *parent = NULL;
302 struct ceph_osd_request *req = NULL;
303
304 while (*p) {
305 parent = *p;
306 req = rb_entry(parent, struct ceph_osd_request, r_node);
307 if (new->r_tid < req->r_tid)
308 p = &(*p)->rb_left;
309 else if (new->r_tid > req->r_tid)
310 p = &(*p)->rb_right;
311 else
312 BUG();
313 }
314
315 rb_link_node(&new->r_node, parent, p);
316 rb_insert_color(&new->r_node, &osdc->requests);
317}
318
319static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
320 u64 tid)
321{
322 struct ceph_osd_request *req;
323 struct rb_node *n = osdc->requests.rb_node;
324
325 while (n) {
326 req = rb_entry(n, struct ceph_osd_request, r_node);
327 if (tid < req->r_tid)
328 n = n->rb_left;
329 else if (tid > req->r_tid)
330 n = n->rb_right;
331 else
332 return req;
333 }
334 return NULL;
335}
336
337static struct ceph_osd_request *
338__lookup_request_ge(struct ceph_osd_client *osdc,
339 u64 tid)
340{
341 struct ceph_osd_request *req;
342 struct rb_node *n = osdc->requests.rb_node;
343
344 while (n) {
345 req = rb_entry(n, struct ceph_osd_request, r_node);
346 if (tid < req->r_tid) {
347 if (!n->rb_left)
348 return req;
349 n = n->rb_left;
350 } else if (tid > req->r_tid) {
351 n = n->rb_right;
352 } else {
353 return req;
354 }
355 }
356 return NULL;
357}
358
359
360/*
Sage Weil81b024e2009-10-09 10:29:18 -0700361 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700362 */
363static void osd_reset(struct ceph_connection *con)
364{
365 struct ceph_osd *osd = con->private;
366 struct ceph_osd_client *osdc;
367
368 if (!osd)
369 return;
370 dout("osd_reset osd%d\n", osd->o_osd);
371 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700372 down_read(&osdc->map_sem);
373 kick_requests(osdc, osd);
374 up_read(&osdc->map_sem);
375}
376
377/*
378 * Track open sessions with osds.
379 */
380static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
381{
382 struct ceph_osd *osd;
383
384 osd = kzalloc(sizeof(*osd), GFP_NOFS);
385 if (!osd)
386 return NULL;
387
388 atomic_set(&osd->o_ref, 1);
389 osd->o_osdc = osdc;
390 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800391 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700392 osd->o_incarnation = 1;
393
394 ceph_con_init(osdc->client->msgr, &osd->o_con);
395 osd->o_con.private = osd;
396 osd->o_con.ops = &osd_con_ops;
397 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800398
Sage Weilf24e9982009-10-06 11:31:10 -0700399 return osd;
400}
401
402static struct ceph_osd *get_osd(struct ceph_osd *osd)
403{
404 if (atomic_inc_not_zero(&osd->o_ref)) {
405 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
406 atomic_read(&osd->o_ref));
407 return osd;
408 } else {
409 dout("get_osd %p FAIL\n", osd);
410 return NULL;
411 }
412}
413
414static void put_osd(struct ceph_osd *osd)
415{
416 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
417 atomic_read(&osd->o_ref) - 1);
Sage Weil42ce56e2009-11-18 11:22:36 -0800418 if (atomic_dec_and_test(&osd->o_ref))
Sage Weilf24e9982009-10-06 11:31:10 -0700419 kfree(osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700420}
421
422/*
423 * remove an osd from our map
424 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800425static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700426{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800427 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700428 BUG_ON(!list_empty(&osd->o_requests));
429 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800430 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700431 ceph_con_close(&osd->o_con);
432 put_osd(osd);
433}
434
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800435static void __move_osd_to_lru(struct ceph_osd_client *osdc,
436 struct ceph_osd *osd)
437{
438 dout("__move_osd_to_lru %p\n", osd);
439 BUG_ON(!list_empty(&osd->o_osd_lru));
440 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
441 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
442}
443
444static void __remove_osd_from_lru(struct ceph_osd *osd)
445{
446 dout("__remove_osd_from_lru %p\n", osd);
447 if (!list_empty(&osd->o_osd_lru))
448 list_del_init(&osd->o_osd_lru);
449}
450
451static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
452{
453 struct ceph_osd *osd, *nosd;
454
455 dout("__remove_old_osds %p\n", osdc);
456 mutex_lock(&osdc->request_mutex);
457 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
458 if (!remove_all && time_before(jiffies, osd->lru_ttl))
459 break;
460 __remove_osd(osdc, osd);
461 }
462 mutex_unlock(&osdc->request_mutex);
463}
464
Sage Weilf24e9982009-10-06 11:31:10 -0700465/*
466 * reset osd connect
467 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800468static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700469{
470 int ret = 0;
471
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800472 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700473 if (list_empty(&osd->o_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800474 __remove_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700475 } else {
476 ceph_con_close(&osd->o_con);
477 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
478 osd->o_incarnation++;
479 }
480 return ret;
481}
482
483static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
484{
485 struct rb_node **p = &osdc->osds.rb_node;
486 struct rb_node *parent = NULL;
487 struct ceph_osd *osd = NULL;
488
489 while (*p) {
490 parent = *p;
491 osd = rb_entry(parent, struct ceph_osd, o_node);
492 if (new->o_osd < osd->o_osd)
493 p = &(*p)->rb_left;
494 else if (new->o_osd > osd->o_osd)
495 p = &(*p)->rb_right;
496 else
497 BUG();
498 }
499
500 rb_link_node(&new->o_node, parent, p);
501 rb_insert_color(&new->o_node, &osdc->osds);
502}
503
504static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
505{
506 struct ceph_osd *osd;
507 struct rb_node *n = osdc->osds.rb_node;
508
509 while (n) {
510 osd = rb_entry(n, struct ceph_osd, o_node);
511 if (o < osd->o_osd)
512 n = n->rb_left;
513 else if (o > osd->o_osd)
514 n = n->rb_right;
515 else
516 return osd;
517 }
518 return NULL;
519}
520
521
522/*
523 * Register request, assign tid. If this is the first request, set up
524 * the timeout event.
525 */
526static void register_request(struct ceph_osd_client *osdc,
527 struct ceph_osd_request *req)
528{
Sage Weilf24e9982009-10-06 11:31:10 -0700529 mutex_lock(&osdc->request_mutex);
530 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800531 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700532
533 dout("register_request %p tid %lld\n", req, req->r_tid);
534 __insert_request(osdc, req);
535 ceph_osdc_get_request(req);
536 osdc->num_requests++;
537
538 req->r_timeout_stamp =
Sage Weil6b805182009-10-27 11:50:50 -0700539 jiffies + osdc->client->mount_args->osd_timeout*HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700540
541 if (osdc->num_requests == 1) {
542 osdc->timeout_tid = req->r_tid;
543 dout(" timeout on tid %llu at %lu\n", req->r_tid,
544 req->r_timeout_stamp);
545 schedule_delayed_work(&osdc->timeout_work,
546 round_jiffies_relative(req->r_timeout_stamp - jiffies));
547 }
548 mutex_unlock(&osdc->request_mutex);
549}
550
551/*
552 * called under osdc->request_mutex
553 */
554static void __unregister_request(struct ceph_osd_client *osdc,
555 struct ceph_osd_request *req)
556{
557 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
558 rb_erase(&req->r_node, &osdc->requests);
559 osdc->num_requests--;
560
Sage Weil0ba64782009-10-08 16:57:16 -0700561 if (req->r_osd) {
562 /* make sure the original request isn't in flight. */
563 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
564
565 list_del_init(&req->r_osd_item);
566 if (list_empty(&req->r_osd->o_requests))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800567 __move_osd_to_lru(osdc, req->r_osd);
Sage Weil0ba64782009-10-08 16:57:16 -0700568 req->r_osd = NULL;
569 }
Sage Weilf24e9982009-10-06 11:31:10 -0700570
571 ceph_osdc_put_request(req);
572
573 if (req->r_tid == osdc->timeout_tid) {
574 if (osdc->num_requests == 0) {
575 dout("no requests, canceling timeout\n");
576 osdc->timeout_tid = 0;
577 cancel_delayed_work(&osdc->timeout_work);
578 } else {
579 req = rb_entry(rb_first(&osdc->requests),
580 struct ceph_osd_request, r_node);
581 osdc->timeout_tid = req->r_tid;
582 dout("rescheduled timeout on tid %llu at %lu\n",
583 req->r_tid, req->r_timeout_stamp);
584 schedule_delayed_work(&osdc->timeout_work,
585 round_jiffies_relative(req->r_timeout_stamp -
586 jiffies));
587 }
588 }
589}
590
591/*
592 * Cancel a previously queued request message
593 */
594static void __cancel_request(struct ceph_osd_request *req)
595{
596 if (req->r_sent) {
597 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
598 req->r_sent = 0;
599 }
600}
601
602/*
603 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
604 * (as needed), and set the request r_osd appropriately. If there is
605 * no up osd, set r_osd to NULL.
606 *
607 * Return 0 if unchanged, 1 if changed, or negative on error.
608 *
609 * Caller should hold map_sem for read and request_mutex.
610 */
611static int __map_osds(struct ceph_osd_client *osdc,
612 struct ceph_osd_request *req)
613{
614 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800615 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700616 int o = -1;
617 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700618
619 dout("map_osds %p tid %lld\n", req, req->r_tid);
620 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
621 &req->r_file_layout, osdc->osdmap);
622 if (err)
623 return err;
Sage Weil51042122009-11-04 11:39:12 -0800624 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800625 req->r_pgid = pgid;
626
Sage Weilf24e9982009-10-06 11:31:10 -0700627 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
628
629 if ((req->r_osd && req->r_osd->o_osd == o &&
630 req->r_sent >= req->r_osd->o_incarnation) ||
631 (req->r_osd == NULL && o == -1))
632 return 0; /* no change */
633
Sage Weil51042122009-11-04 11:39:12 -0800634 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
635 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700636 req->r_osd ? req->r_osd->o_osd : -1);
637
638 if (req->r_osd) {
639 __cancel_request(req);
640 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700641 req->r_osd = NULL;
642 }
643
644 req->r_osd = __lookup_osd(osdc, o);
645 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800646 err = -ENOMEM;
647 req->r_osd = create_osd(osdc);
648 if (!req->r_osd)
649 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -0700650
651 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
652 req->r_osd->o_osd = o;
653 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
654 __insert_osd(osdc, req->r_osd);
655
656 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
657 }
658
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800659 if (req->r_osd) {
660 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700661 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800662 }
Sage Weilf24e9982009-10-06 11:31:10 -0700663 err = 1; /* osd changed */
664
665out:
Sage Weilf24e9982009-10-06 11:31:10 -0700666 return err;
667}
668
669/*
670 * caller should hold map_sem (for read) and request_mutex
671 */
672static int __send_request(struct ceph_osd_client *osdc,
673 struct ceph_osd_request *req)
674{
675 struct ceph_osd_request_head *reqhead;
676 int err;
677
678 err = __map_osds(osdc, req);
679 if (err < 0)
680 return err;
681 if (req->r_osd == NULL) {
682 dout("send_request %p no up osds in pg\n", req);
683 ceph_monc_request_next_osdmap(&osdc->client->monc);
684 return 0;
685 }
686
687 dout("send_request %p tid %llu to osd%d flags %d\n",
688 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
689
690 reqhead = req->r_request->front.iov_base;
691 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
692 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
693 reqhead->reassert_version = req->r_reassert_version;
694
Sage Weil6b805182009-10-27 11:50:50 -0700695 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700696
697 ceph_msg_get(req->r_request); /* send consumes a ref */
698 ceph_con_send(&req->r_osd->o_con, req->r_request);
699 req->r_sent = req->r_osd->o_incarnation;
700 return 0;
701}
702
703/*
704 * Timeout callback, called every N seconds when 1 or more osd
705 * requests has been active for more than N seconds. When this
706 * happens, we ping all OSDs with requests who have timed out to
707 * ensure any communications channel reset is detected. Reset the
708 * request timeouts another N seconds in the future as we go.
709 * Reschedule the timeout event another N seconds in future (unless
710 * there are no open requests).
711 */
712static void handle_timeout(struct work_struct *work)
713{
714 struct ceph_osd_client *osdc =
715 container_of(work, struct ceph_osd_client, timeout_work.work);
716 struct ceph_osd_request *req;
717 struct ceph_osd *osd;
Sage Weil6b805182009-10-27 11:50:50 -0700718 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700719 unsigned long next_timeout = timeout + jiffies;
720 struct rb_node *p;
721
722 dout("timeout\n");
723 down_read(&osdc->map_sem);
724
725 ceph_monc_request_next_osdmap(&osdc->client->monc);
726
727 mutex_lock(&osdc->request_mutex);
728 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
729 req = rb_entry(p, struct ceph_osd_request, r_node);
730
731 if (req->r_resend) {
732 int err;
733
734 dout("osdc resending prev failed %lld\n", req->r_tid);
735 err = __send_request(osdc, req);
736 if (err)
737 dout("osdc failed again on %lld\n", req->r_tid);
738 else
739 req->r_resend = false;
740 continue;
741 }
742 }
743 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
744 osd = rb_entry(p, struct ceph_osd, o_node);
745 if (list_empty(&osd->o_requests))
746 continue;
747 req = list_first_entry(&osd->o_requests,
748 struct ceph_osd_request, r_osd_item);
749 if (time_before(jiffies, req->r_timeout_stamp))
750 continue;
751
752 dout(" tid %llu (at least) timed out on osd%d\n",
753 req->r_tid, osd->o_osd);
754 req->r_timeout_stamp = next_timeout;
755 ceph_con_keepalive(&osd->o_con);
756 }
757
758 if (osdc->timeout_tid)
759 schedule_delayed_work(&osdc->timeout_work,
760 round_jiffies_relative(timeout));
761
762 mutex_unlock(&osdc->request_mutex);
763
764 up_read(&osdc->map_sem);
765}
766
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800767static void handle_osds_timeout(struct work_struct *work)
768{
769 struct ceph_osd_client *osdc =
770 container_of(work, struct ceph_osd_client,
771 osds_timeout_work.work);
772 unsigned long delay =
773 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
774
775 dout("osds timeout\n");
776 down_read(&osdc->map_sem);
777 remove_old_osds(osdc, 0);
778 up_read(&osdc->map_sem);
779
780 schedule_delayed_work(&osdc->osds_timeout_work,
781 round_jiffies_relative(delay));
782}
783
Sage Weilf24e9982009-10-06 11:31:10 -0700784/*
785 * handle osd op reply. either call the callback if it is specified,
786 * or do the completion to wake up the waiting thread.
787 */
Sage Weil350b1c32009-12-22 10:45:45 -0800788static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
789 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -0700790{
791 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
792 struct ceph_osd_request *req;
793 u64 tid;
794 int numops, object_len, flags;
795
Sage Weil6df058c2009-12-22 11:24:33 -0800796 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700797 if (msg->front.iov_len < sizeof(*rhead))
798 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700799 numops = le32_to_cpu(rhead->num_ops);
800 object_len = le32_to_cpu(rhead->object_len);
801 if (msg->front.iov_len != sizeof(*rhead) + object_len +
802 numops * sizeof(struct ceph_osd_op))
803 goto bad;
804 dout("handle_reply %p tid %llu\n", msg, tid);
805
806 /* lookup */
807 mutex_lock(&osdc->request_mutex);
808 req = __lookup_request(osdc, tid);
809 if (req == NULL) {
810 dout("handle_reply tid %llu dne\n", tid);
811 mutex_unlock(&osdc->request_mutex);
812 return;
813 }
814 ceph_osdc_get_request(req);
815 flags = le32_to_cpu(rhead->flags);
816
Sage Weil350b1c32009-12-22 10:45:45 -0800817 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800818 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -0800819 * avoid a (safe but slower) revoke later.
820 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800821 if (req->r_con_filling_msg == con && req->r_reply == msg) {
822 dout(" got pages, dropping con_filling_msg ref %p\n", con);
823 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800824 ceph_con_put(con);
825 }
826
Sage Weilf24e9982009-10-06 11:31:10 -0700827 if (req->r_reply) {
828 /*
829 * once we see the message has been received, we don't
830 * need a ref (which is only needed for revoking
831 * pages)
832 */
833 ceph_msg_put(req->r_reply);
834 req->r_reply = NULL;
835 }
836
837 if (!req->r_got_reply) {
838 unsigned bytes;
839
840 req->r_result = le32_to_cpu(rhead->result);
841 bytes = le32_to_cpu(msg->hdr.data_len);
842 dout("handle_reply result %d bytes %d\n", req->r_result,
843 bytes);
844 if (req->r_result == 0)
845 req->r_result = bytes;
846
847 /* in case this is a write and we need to replay, */
848 req->r_reassert_version = rhead->reassert_version;
849
850 req->r_got_reply = 1;
851 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
852 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -0800853 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700854 goto done;
855 }
856
857 dout("handle_reply tid %llu flags %d\n", tid, flags);
858
859 /* either this is a read, or we got the safe response */
860 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
861 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
862 __unregister_request(osdc, req);
863
864 mutex_unlock(&osdc->request_mutex);
865
866 if (req->r_callback)
867 req->r_callback(req, msg);
868 else
869 complete(&req->r_completion);
870
871 if (flags & CEPH_OSD_FLAG_ONDISK) {
872 if (req->r_safe_callback)
873 req->r_safe_callback(req, msg);
874 complete(&req->r_safe_completion); /* fsync waiter */
875 }
876
877done:
878 ceph_osdc_put_request(req);
879 return;
880
881bad:
882 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
883 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
884 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -0800885 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -0700886}
887
888
889/*
890 * Resubmit osd requests whose osd or osd address has changed. Request
891 * a new osd map if osds are down, or we are otherwise unable to determine
892 * how to direct a request.
893 *
894 * Close connections to down osds.
895 *
896 * If @who is specified, resubmit requests for that specific osd.
897 *
898 * Caller should hold map_sem for read and request_mutex.
899 */
900static void kick_requests(struct ceph_osd_client *osdc,
901 struct ceph_osd *kickosd)
902{
903 struct ceph_osd_request *req;
904 struct rb_node *p, *n;
905 int needmap = 0;
906 int err;
907
908 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
909 mutex_lock(&osdc->request_mutex);
Sage Weil153a0082010-02-15 12:11:51 -0800910 if (kickosd) {
911 __reset_osd(osdc, kickosd);
912 } else {
Sage Weilf24e9982009-10-06 11:31:10 -0700913 for (p = rb_first(&osdc->osds); p; p = n) {
914 struct ceph_osd *osd =
915 rb_entry(p, struct ceph_osd, o_node);
916
917 n = rb_next(p);
918 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
Sage Weil103e2d32010-01-07 16:12:36 -0800919 memcmp(&osd->o_con.peer_addr,
920 ceph_osd_addr(osdc->osdmap,
921 osd->o_osd),
922 sizeof(struct ceph_entity_addr)) != 0)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800923 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700924 }
925 }
926
927 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
928 req = rb_entry(p, struct ceph_osd_request, r_node);
929
930 if (req->r_resend) {
931 dout(" r_resend set on tid %llu\n", req->r_tid);
Sage Weil266673d2009-10-09 10:31:32 -0700932 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700933 goto kick;
934 }
Sage Weil266673d2009-10-09 10:31:32 -0700935 if (req->r_osd && kickosd == req->r_osd) {
936 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700937 goto kick;
Sage Weil266673d2009-10-09 10:31:32 -0700938 }
Sage Weilf24e9982009-10-06 11:31:10 -0700939
940 err = __map_osds(osdc, req);
941 if (err == 0)
942 continue; /* no change */
943 if (err < 0) {
944 /*
945 * FIXME: really, we should set the request
946 * error and fail if this isn't a 'nofail'
947 * request, but that's a fair bit more
948 * complicated to do. So retry!
949 */
950 dout(" setting r_resend on %llu\n", req->r_tid);
951 req->r_resend = true;
952 continue;
953 }
954 if (req->r_osd == NULL) {
955 dout("tid %llu maps to no valid osd\n", req->r_tid);
956 needmap++; /* request a newer map */
957 continue;
958 }
959
960kick:
Sage Weilc1ea8822009-10-08 16:55:47 -0700961 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
962 req->r_osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700963 req->r_flags |= CEPH_OSD_FLAG_RETRY;
964 err = __send_request(osdc, req);
965 if (err) {
966 dout(" setting r_resend on %llu\n", req->r_tid);
967 req->r_resend = true;
968 }
969 }
970 mutex_unlock(&osdc->request_mutex);
971
972 if (needmap) {
973 dout("%d requests for down osds, need new map\n", needmap);
974 ceph_monc_request_next_osdmap(&osdc->client->monc);
975 }
976}
977
978/*
979 * Process updated osd map.
980 *
981 * The message contains any number of incremental and full maps, normally
982 * indicating some sort of topology change in the cluster. Kick requests
983 * off to different OSDs as needed.
984 */
985void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
986{
987 void *p, *end, *next;
988 u32 nr_maps, maplen;
989 u32 epoch;
990 struct ceph_osdmap *newmap = NULL, *oldmap;
991 int err;
992 struct ceph_fsid fsid;
993
994 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
995 p = msg->front.iov_base;
996 end = p + msg->front.iov_len;
997
998 /* verify fsid */
999 ceph_decode_need(&p, end, sizeof(fsid), bad);
1000 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001001 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1002 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001003
1004 down_write(&osdc->map_sem);
1005
1006 /* incremental maps */
1007 ceph_decode_32_safe(&p, end, nr_maps, bad);
1008 dout(" %d inc maps\n", nr_maps);
1009 while (nr_maps > 0) {
1010 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001011 epoch = ceph_decode_32(&p);
1012 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001013 ceph_decode_need(&p, end, maplen, bad);
1014 next = p + maplen;
1015 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1016 dout("applying incremental map %u len %d\n",
1017 epoch, maplen);
1018 newmap = osdmap_apply_incremental(&p, next,
1019 osdc->osdmap,
1020 osdc->client->msgr);
1021 if (IS_ERR(newmap)) {
1022 err = PTR_ERR(newmap);
1023 goto bad;
1024 }
Sage Weil30dc6382009-12-21 14:49:37 -08001025 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001026 if (newmap != osdc->osdmap) {
1027 ceph_osdmap_destroy(osdc->osdmap);
1028 osdc->osdmap = newmap;
1029 }
1030 } else {
1031 dout("ignoring incremental map %u len %d\n",
1032 epoch, maplen);
1033 }
1034 p = next;
1035 nr_maps--;
1036 }
1037 if (newmap)
1038 goto done;
1039
1040 /* full maps */
1041 ceph_decode_32_safe(&p, end, nr_maps, bad);
1042 dout(" %d full maps\n", nr_maps);
1043 while (nr_maps) {
1044 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001045 epoch = ceph_decode_32(&p);
1046 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001047 ceph_decode_need(&p, end, maplen, bad);
1048 if (nr_maps > 1) {
1049 dout("skipping non-latest full map %u len %d\n",
1050 epoch, maplen);
1051 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1052 dout("skipping full map %u len %d, "
1053 "older than our %u\n", epoch, maplen,
1054 osdc->osdmap->epoch);
1055 } else {
1056 dout("taking full map %u len %d\n", epoch, maplen);
1057 newmap = osdmap_decode(&p, p+maplen);
1058 if (IS_ERR(newmap)) {
1059 err = PTR_ERR(newmap);
1060 goto bad;
1061 }
Sage Weil30dc6382009-12-21 14:49:37 -08001062 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001063 oldmap = osdc->osdmap;
1064 osdc->osdmap = newmap;
1065 if (oldmap)
1066 ceph_osdmap_destroy(oldmap);
1067 }
1068 p += maplen;
1069 nr_maps--;
1070 }
1071
1072done:
1073 downgrade_write(&osdc->map_sem);
1074 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1075 if (newmap)
1076 kick_requests(osdc, NULL);
1077 up_read(&osdc->map_sem);
1078 return;
1079
1080bad:
1081 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001082 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001083 up_write(&osdc->map_sem);
1084 return;
1085}
1086
1087
1088/*
1089 * A read request prepares specific pages that data is to be read into.
1090 * When a message is being read off the wire, we call prepare_pages to
1091 * find those pages.
1092 * 0 = success, -1 failure.
1093 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001094static int __prepare_pages(struct ceph_connection *con,
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001095 struct ceph_msg_header *hdr,
1096 struct ceph_osd_request *req,
1097 u64 tid,
1098 struct ceph_msg *m)
Sage Weilf24e9982009-10-06 11:31:10 -07001099{
1100 struct ceph_osd *osd = con->private;
1101 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001102 int ret = -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001103 int data_len = le32_to_cpu(hdr->data_len);
1104 unsigned data_off = le16_to_cpu(hdr->data_off);
1105
1106 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07001107
1108 if (!osd)
1109 return -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001110
Sage Weilf24e9982009-10-06 11:31:10 -07001111 osdc = osd->o_osdc;
1112
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001113 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
Sage Weilf24e9982009-10-06 11:31:10 -07001114 tid, req->r_num_pages, want);
Sage Weil350b1c32009-12-22 10:45:45 -08001115 if (unlikely(req->r_num_pages < want))
1116 goto out;
Sage Weil350b1c32009-12-22 10:45:45 -08001117 m->pages = req->r_pages;
1118 m->nr_pages = req->r_num_pages;
1119 ret = 0; /* success */
Sage Weilf24e9982009-10-06 11:31:10 -07001120out:
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001121 BUG_ON(ret < 0 || m->nr_pages < want);
1122
Sage Weilf24e9982009-10-06 11:31:10 -07001123 return ret;
1124}
1125
1126/*
1127 * Register request, send initial attempt.
1128 */
1129int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1130 struct ceph_osd_request *req,
1131 bool nofail)
1132{
Sage Weilc1ea8822009-10-08 16:55:47 -07001133 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001134
1135 req->r_request->pages = req->r_pages;
1136 req->r_request->nr_pages = req->r_num_pages;
1137
1138 register_request(osdc, req);
1139
1140 down_read(&osdc->map_sem);
1141 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001142 /*
1143 * a racing kick_requests() may have sent the message for us
1144 * while we dropped request_mutex above, so only send now if
1145 * the request still han't been touched yet.
1146 */
1147 if (req->r_sent == 0) {
1148 rc = __send_request(osdc, req);
1149 if (rc) {
1150 if (nofail) {
1151 dout("osdc_start_request failed send, "
1152 " marking %lld\n", req->r_tid);
1153 req->r_resend = true;
1154 rc = 0;
1155 } else {
1156 __unregister_request(osdc, req);
1157 }
Sage Weilf24e9982009-10-06 11:31:10 -07001158 }
1159 }
1160 mutex_unlock(&osdc->request_mutex);
1161 up_read(&osdc->map_sem);
1162 return rc;
1163}
1164
1165/*
1166 * wait for a request to complete
1167 */
1168int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1169 struct ceph_osd_request *req)
1170{
1171 int rc;
1172
1173 rc = wait_for_completion_interruptible(&req->r_completion);
1174 if (rc < 0) {
1175 mutex_lock(&osdc->request_mutex);
1176 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001177 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001178 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001179 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001180 return rc;
1181 }
1182
1183 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1184 return req->r_result;
1185}
1186
1187/*
1188 * sync - wait for all in-flight requests to flush. avoid starvation.
1189 */
1190void ceph_osdc_sync(struct ceph_osd_client *osdc)
1191{
1192 struct ceph_osd_request *req;
1193 u64 last_tid, next_tid = 0;
1194
1195 mutex_lock(&osdc->request_mutex);
1196 last_tid = osdc->last_tid;
1197 while (1) {
1198 req = __lookup_request_ge(osdc, next_tid);
1199 if (!req)
1200 break;
1201 if (req->r_tid > last_tid)
1202 break;
1203
1204 next_tid = req->r_tid + 1;
1205 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1206 continue;
1207
1208 ceph_osdc_get_request(req);
1209 mutex_unlock(&osdc->request_mutex);
1210 dout("sync waiting on tid %llu (last is %llu)\n",
1211 req->r_tid, last_tid);
1212 wait_for_completion(&req->r_safe_completion);
1213 mutex_lock(&osdc->request_mutex);
1214 ceph_osdc_put_request(req);
1215 }
1216 mutex_unlock(&osdc->request_mutex);
1217 dout("sync done (thru tid %llu)\n", last_tid);
1218}
1219
1220/*
1221 * init, shutdown
1222 */
1223int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1224{
1225 int err;
1226
1227 dout("init\n");
1228 osdc->client = client;
1229 osdc->osdmap = NULL;
1230 init_rwsem(&osdc->map_sem);
1231 init_completion(&osdc->map_waiters);
1232 osdc->last_requested_map = 0;
1233 mutex_init(&osdc->request_mutex);
1234 osdc->timeout_tid = 0;
1235 osdc->last_tid = 0;
1236 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001237 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001238 osdc->requests = RB_ROOT;
1239 osdc->num_requests = 0;
1240 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001241 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1242
1243 schedule_delayed_work(&osdc->osds_timeout_work,
1244 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001245
Sage Weil5f44f142009-11-18 14:52:18 -08001246 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001247 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1248 sizeof(struct ceph_osd_request));
1249 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001250 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001251
1252 err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
1253 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001254 goto out_mempool;
Sage Weilf24e9982009-10-06 11:31:10 -07001255 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001256
Sage Weil5f44f142009-11-18 14:52:18 -08001257out_mempool:
1258 mempool_destroy(osdc->req_mempool);
1259out:
1260 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001261}
1262
1263void ceph_osdc_stop(struct ceph_osd_client *osdc)
1264{
1265 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001266 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001267 if (osdc->osdmap) {
1268 ceph_osdmap_destroy(osdc->osdmap);
1269 osdc->osdmap = NULL;
1270 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001271 remove_old_osds(osdc, 1);
Sage Weilf24e9982009-10-06 11:31:10 -07001272 mempool_destroy(osdc->req_mempool);
1273 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilf24e9982009-10-06 11:31:10 -07001274}
1275
1276/*
1277 * Read some contiguous pages. If we cross a stripe boundary, shorten
1278 * *plen. Return number of bytes read, or error.
1279 */
1280int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1281 struct ceph_vino vino, struct ceph_file_layout *layout,
1282 u64 off, u64 *plen,
1283 u32 truncate_seq, u64 truncate_size,
1284 struct page **pages, int num_pages)
1285{
1286 struct ceph_osd_request *req;
1287 int rc = 0;
1288
1289 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1290 vino.snap, off, *plen);
1291 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1292 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1293 NULL, 0, truncate_seq, truncate_size, NULL,
1294 false, 1);
1295 if (IS_ERR(req))
1296 return PTR_ERR(req);
1297
1298 /* it may be a short read due to an object boundary */
1299 req->r_pages = pages;
1300 num_pages = calc_pages_for(off, *plen);
1301 req->r_num_pages = num_pages;
1302
1303 dout("readpages final extent is %llu~%llu (%d pages)\n",
1304 off, *plen, req->r_num_pages);
1305
1306 rc = ceph_osdc_start_request(osdc, req, false);
1307 if (!rc)
1308 rc = ceph_osdc_wait_request(osdc, req);
1309
1310 ceph_osdc_put_request(req);
1311 dout("readpages result %d\n", rc);
1312 return rc;
1313}
1314
1315/*
1316 * do a synchronous write on N pages
1317 */
1318int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1319 struct ceph_file_layout *layout,
1320 struct ceph_snap_context *snapc,
1321 u64 off, u64 len,
1322 u32 truncate_seq, u64 truncate_size,
1323 struct timespec *mtime,
1324 struct page **pages, int num_pages,
1325 int flags, int do_sync, bool nofail)
1326{
1327 struct ceph_osd_request *req;
1328 int rc = 0;
1329
1330 BUG_ON(vino.snap != CEPH_NOSNAP);
1331 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1332 CEPH_OSD_OP_WRITE,
1333 flags | CEPH_OSD_FLAG_ONDISK |
1334 CEPH_OSD_FLAG_WRITE,
1335 snapc, do_sync,
1336 truncate_seq, truncate_size, mtime,
1337 nofail, 1);
1338 if (IS_ERR(req))
1339 return PTR_ERR(req);
1340
1341 /* it may be a short write due to an object boundary */
1342 req->r_pages = pages;
1343 req->r_num_pages = calc_pages_for(off, len);
1344 dout("writepages %llu~%llu (%d pages)\n", off, len,
1345 req->r_num_pages);
1346
1347 rc = ceph_osdc_start_request(osdc, req, nofail);
1348 if (!rc)
1349 rc = ceph_osdc_wait_request(osdc, req);
1350
1351 ceph_osdc_put_request(req);
1352 if (rc == 0)
1353 rc = len;
1354 dout("writepages result %d\n", rc);
1355 return rc;
1356}
1357
1358/*
1359 * handle incoming message
1360 */
1361static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1362{
1363 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001364 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001365 int type = le16_to_cpu(msg->hdr.type);
1366
1367 if (!osd)
1368 return;
Julia Lawall32c895e2009-11-21 16:53:16 +01001369 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001370
1371 switch (type) {
1372 case CEPH_MSG_OSD_MAP:
1373 ceph_osdc_handle_map(osdc, msg);
1374 break;
1375 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001376 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001377 break;
1378
1379 default:
1380 pr_err("received unknown message type %d %s\n", type,
1381 ceph_msg_type_name(type));
1382 }
1383 ceph_msg_put(msg);
1384}
1385
Sage Weil5b3a4db2010-02-19 21:43:23 -08001386/*
1387 * lookup and return message for incoming reply
1388 */
1389static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001390 struct ceph_msg_header *hdr,
1391 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001392{
1393 struct ceph_osd *osd = con->private;
1394 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001395 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001396 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001397 int front = le32_to_cpu(hdr->front_len);
1398 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001399 u64 tid;
1400 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001401
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001402 tid = le64_to_cpu(hdr->tid);
1403 mutex_lock(&osdc->request_mutex);
1404 req = __lookup_request(osdc, tid);
1405 if (!req) {
1406 *skip = 1;
1407 m = NULL;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001408 pr_info("alloc_msg unknown tid %llu from osd%d\n", tid,
1409 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001410 goto out;
1411 }
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001412 m = __get_next_reply(con, req, front);
1413 if (!m || IS_ERR(m)) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001414 *skip = 1;
1415 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001416 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001417
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001418 if (data_len > 0) {
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001419 err = __prepare_pages(con, hdr, req, tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001420 if (err < 0) {
1421 *skip = 1;
1422 ceph_msg_put(m);
1423 m = ERR_PTR(err);
1424 }
1425 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08001426 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001427
1428out:
1429 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001430 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001431
1432}
1433
1434static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1435 struct ceph_msg_header *hdr,
1436 int *skip)
1437{
1438 struct ceph_osd *osd = con->private;
1439 int type = le16_to_cpu(hdr->type);
1440 int front = le32_to_cpu(hdr->front_len);
1441
1442 switch (type) {
1443 case CEPH_MSG_OSD_MAP:
1444 return ceph_msg_new(type, front, 0, 0, NULL);
1445 case CEPH_MSG_OSD_OPREPLY:
1446 return get_reply(con, hdr, skip);
1447 default:
1448 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1449 osd->o_osd);
1450 *skip = 1;
1451 return NULL;
1452 }
Sage Weilf24e9982009-10-06 11:31:10 -07001453}
1454
1455/*
1456 * Wrappers to refcount containing ceph_osd struct
1457 */
1458static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1459{
1460 struct ceph_osd *osd = con->private;
1461 if (get_osd(osd))
1462 return con;
1463 return NULL;
1464}
1465
1466static void put_osd_con(struct ceph_connection *con)
1467{
1468 struct ceph_osd *osd = con->private;
1469 put_osd(osd);
1470}
1471
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001472/*
1473 * authentication
1474 */
1475static int get_authorizer(struct ceph_connection *con,
Sage Weil50b885b2009-12-01 14:12:07 -08001476 void **buf, int *len, int *proto,
1477 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001478{
1479 struct ceph_osd *o = con->private;
1480 struct ceph_osd_client *osdc = o->o_osdc;
1481 struct ceph_auth_client *ac = osdc->client->monc.auth;
1482 int ret = 0;
1483
1484 if (force_new && o->o_authorizer) {
1485 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1486 o->o_authorizer = NULL;
1487 }
1488 if (o->o_authorizer == NULL) {
1489 ret = ac->ops->create_authorizer(
1490 ac, CEPH_ENTITY_TYPE_OSD,
1491 &o->o_authorizer,
1492 &o->o_authorizer_buf,
1493 &o->o_authorizer_buf_len,
1494 &o->o_authorizer_reply_buf,
1495 &o->o_authorizer_reply_buf_len);
1496 if (ret)
1497 return ret;
1498 }
1499
1500 *proto = ac->protocol;
1501 *buf = o->o_authorizer_buf;
1502 *len = o->o_authorizer_buf_len;
1503 *reply_buf = o->o_authorizer_reply_buf;
1504 *reply_len = o->o_authorizer_reply_buf_len;
1505 return 0;
1506}
1507
1508
1509static int verify_authorizer_reply(struct ceph_connection *con, int len)
1510{
1511 struct ceph_osd *o = con->private;
1512 struct ceph_osd_client *osdc = o->o_osdc;
1513 struct ceph_auth_client *ac = osdc->client->monc.auth;
1514
1515 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1516}
1517
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001518static int invalidate_authorizer(struct ceph_connection *con)
1519{
1520 struct ceph_osd *o = con->private;
1521 struct ceph_osd_client *osdc = o->o_osdc;
1522 struct ceph_auth_client *ac = osdc->client->monc.auth;
1523
1524 if (ac->ops->invalidate_authorizer)
1525 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1526
1527 return ceph_monc_validate_auth(&osdc->client->monc);
1528}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001529
Sage Weilf24e9982009-10-06 11:31:10 -07001530const static struct ceph_connection_operations osd_con_ops = {
1531 .get = get_osd_con,
1532 .put = put_osd_con,
1533 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001534 .get_authorizer = get_authorizer,
1535 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001536 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07001537 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07001538 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07001539};