blob: fa0f73703954b4f1c8a568d841155cda99ced260 [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;
618 struct ceph_osd *newosd = NULL;
619
620 dout("map_osds %p tid %lld\n", req, req->r_tid);
621 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
622 &req->r_file_layout, osdc->osdmap);
623 if (err)
624 return err;
Sage Weil51042122009-11-04 11:39:12 -0800625 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800626 req->r_pgid = pgid;
627
Sage Weilf24e9982009-10-06 11:31:10 -0700628 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
629
630 if ((req->r_osd && req->r_osd->o_osd == o &&
631 req->r_sent >= req->r_osd->o_incarnation) ||
632 (req->r_osd == NULL && o == -1))
633 return 0; /* no change */
634
Sage Weil51042122009-11-04 11:39:12 -0800635 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
636 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700637 req->r_osd ? req->r_osd->o_osd : -1);
638
639 if (req->r_osd) {
640 __cancel_request(req);
641 list_del_init(&req->r_osd_item);
642 if (list_empty(&req->r_osd->o_requests)) {
643 /* try to re-use r_osd if possible */
644 newosd = get_osd(req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800645 __remove_osd(osdc, newosd);
Sage Weilf24e9982009-10-06 11:31:10 -0700646 }
647 req->r_osd = NULL;
648 }
649
650 req->r_osd = __lookup_osd(osdc, o);
651 if (!req->r_osd && o >= 0) {
652 if (newosd) {
653 req->r_osd = newosd;
654 newosd = NULL;
655 } else {
656 err = -ENOMEM;
657 req->r_osd = create_osd(osdc);
658 if (!req->r_osd)
659 goto out;
660 }
661
662 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
663 req->r_osd->o_osd = o;
664 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
665 __insert_osd(osdc, req->r_osd);
666
667 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
668 }
669
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800670 if (req->r_osd) {
671 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700672 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800673 }
Sage Weilf24e9982009-10-06 11:31:10 -0700674 err = 1; /* osd changed */
675
676out:
677 if (newosd)
678 put_osd(newosd);
679 return err;
680}
681
682/*
683 * caller should hold map_sem (for read) and request_mutex
684 */
685static int __send_request(struct ceph_osd_client *osdc,
686 struct ceph_osd_request *req)
687{
688 struct ceph_osd_request_head *reqhead;
689 int err;
690
691 err = __map_osds(osdc, req);
692 if (err < 0)
693 return err;
694 if (req->r_osd == NULL) {
695 dout("send_request %p no up osds in pg\n", req);
696 ceph_monc_request_next_osdmap(&osdc->client->monc);
697 return 0;
698 }
699
700 dout("send_request %p tid %llu to osd%d flags %d\n",
701 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
702
703 reqhead = req->r_request->front.iov_base;
704 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
705 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
706 reqhead->reassert_version = req->r_reassert_version;
707
Sage Weil6b805182009-10-27 11:50:50 -0700708 req->r_timeout_stamp = jiffies+osdc->client->mount_args->osd_timeout*HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700709
710 ceph_msg_get(req->r_request); /* send consumes a ref */
711 ceph_con_send(&req->r_osd->o_con, req->r_request);
712 req->r_sent = req->r_osd->o_incarnation;
713 return 0;
714}
715
716/*
717 * Timeout callback, called every N seconds when 1 or more osd
718 * requests has been active for more than N seconds. When this
719 * happens, we ping all OSDs with requests who have timed out to
720 * ensure any communications channel reset is detected. Reset the
721 * request timeouts another N seconds in the future as we go.
722 * Reschedule the timeout event another N seconds in future (unless
723 * there are no open requests).
724 */
725static void handle_timeout(struct work_struct *work)
726{
727 struct ceph_osd_client *osdc =
728 container_of(work, struct ceph_osd_client, timeout_work.work);
729 struct ceph_osd_request *req;
730 struct ceph_osd *osd;
Sage Weil6b805182009-10-27 11:50:50 -0700731 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
Sage Weilf24e9982009-10-06 11:31:10 -0700732 unsigned long next_timeout = timeout + jiffies;
733 struct rb_node *p;
734
735 dout("timeout\n");
736 down_read(&osdc->map_sem);
737
738 ceph_monc_request_next_osdmap(&osdc->client->monc);
739
740 mutex_lock(&osdc->request_mutex);
741 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
742 req = rb_entry(p, struct ceph_osd_request, r_node);
743
744 if (req->r_resend) {
745 int err;
746
747 dout("osdc resending prev failed %lld\n", req->r_tid);
748 err = __send_request(osdc, req);
749 if (err)
750 dout("osdc failed again on %lld\n", req->r_tid);
751 else
752 req->r_resend = false;
753 continue;
754 }
755 }
756 for (p = rb_first(&osdc->osds); p; p = rb_next(p)) {
757 osd = rb_entry(p, struct ceph_osd, o_node);
758 if (list_empty(&osd->o_requests))
759 continue;
760 req = list_first_entry(&osd->o_requests,
761 struct ceph_osd_request, r_osd_item);
762 if (time_before(jiffies, req->r_timeout_stamp))
763 continue;
764
765 dout(" tid %llu (at least) timed out on osd%d\n",
766 req->r_tid, osd->o_osd);
767 req->r_timeout_stamp = next_timeout;
768 ceph_con_keepalive(&osd->o_con);
769 }
770
771 if (osdc->timeout_tid)
772 schedule_delayed_work(&osdc->timeout_work,
773 round_jiffies_relative(timeout));
774
775 mutex_unlock(&osdc->request_mutex);
776
777 up_read(&osdc->map_sem);
778}
779
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800780static void handle_osds_timeout(struct work_struct *work)
781{
782 struct ceph_osd_client *osdc =
783 container_of(work, struct ceph_osd_client,
784 osds_timeout_work.work);
785 unsigned long delay =
786 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
787
788 dout("osds timeout\n");
789 down_read(&osdc->map_sem);
790 remove_old_osds(osdc, 0);
791 up_read(&osdc->map_sem);
792
793 schedule_delayed_work(&osdc->osds_timeout_work,
794 round_jiffies_relative(delay));
795}
796
Sage Weilf24e9982009-10-06 11:31:10 -0700797/*
798 * handle osd op reply. either call the callback if it is specified,
799 * or do the completion to wake up the waiting thread.
800 */
Sage Weil350b1c32009-12-22 10:45:45 -0800801static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
802 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -0700803{
804 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
805 struct ceph_osd_request *req;
806 u64 tid;
807 int numops, object_len, flags;
808
Sage Weil6df058c2009-12-22 11:24:33 -0800809 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700810 if (msg->front.iov_len < sizeof(*rhead))
811 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700812 numops = le32_to_cpu(rhead->num_ops);
813 object_len = le32_to_cpu(rhead->object_len);
814 if (msg->front.iov_len != sizeof(*rhead) + object_len +
815 numops * sizeof(struct ceph_osd_op))
816 goto bad;
817 dout("handle_reply %p tid %llu\n", msg, tid);
818
819 /* lookup */
820 mutex_lock(&osdc->request_mutex);
821 req = __lookup_request(osdc, tid);
822 if (req == NULL) {
823 dout("handle_reply tid %llu dne\n", tid);
824 mutex_unlock(&osdc->request_mutex);
825 return;
826 }
827 ceph_osdc_get_request(req);
828 flags = le32_to_cpu(rhead->flags);
829
Sage Weil350b1c32009-12-22 10:45:45 -0800830 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800831 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -0800832 * avoid a (safe but slower) revoke later.
833 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800834 if (req->r_con_filling_msg == con && req->r_reply == msg) {
835 dout(" got pages, dropping con_filling_msg ref %p\n", con);
836 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800837 ceph_con_put(con);
838 }
839
Sage Weilf24e9982009-10-06 11:31:10 -0700840 if (req->r_reply) {
841 /*
842 * once we see the message has been received, we don't
843 * need a ref (which is only needed for revoking
844 * pages)
845 */
846 ceph_msg_put(req->r_reply);
847 req->r_reply = NULL;
848 }
849
850 if (!req->r_got_reply) {
851 unsigned bytes;
852
853 req->r_result = le32_to_cpu(rhead->result);
854 bytes = le32_to_cpu(msg->hdr.data_len);
855 dout("handle_reply result %d bytes %d\n", req->r_result,
856 bytes);
857 if (req->r_result == 0)
858 req->r_result = bytes;
859
860 /* in case this is a write and we need to replay, */
861 req->r_reassert_version = rhead->reassert_version;
862
863 req->r_got_reply = 1;
864 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
865 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -0800866 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700867 goto done;
868 }
869
870 dout("handle_reply tid %llu flags %d\n", tid, flags);
871
872 /* either this is a read, or we got the safe response */
873 if ((flags & CEPH_OSD_FLAG_ONDISK) ||
874 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
875 __unregister_request(osdc, req);
876
877 mutex_unlock(&osdc->request_mutex);
878
879 if (req->r_callback)
880 req->r_callback(req, msg);
881 else
882 complete(&req->r_completion);
883
884 if (flags & CEPH_OSD_FLAG_ONDISK) {
885 if (req->r_safe_callback)
886 req->r_safe_callback(req, msg);
887 complete(&req->r_safe_completion); /* fsync waiter */
888 }
889
890done:
891 ceph_osdc_put_request(req);
892 return;
893
894bad:
895 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
896 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
897 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -0800898 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -0700899}
900
901
902/*
903 * Resubmit osd requests whose osd or osd address has changed. Request
904 * a new osd map if osds are down, or we are otherwise unable to determine
905 * how to direct a request.
906 *
907 * Close connections to down osds.
908 *
909 * If @who is specified, resubmit requests for that specific osd.
910 *
911 * Caller should hold map_sem for read and request_mutex.
912 */
913static void kick_requests(struct ceph_osd_client *osdc,
914 struct ceph_osd *kickosd)
915{
916 struct ceph_osd_request *req;
917 struct rb_node *p, *n;
918 int needmap = 0;
919 int err;
920
921 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
922 mutex_lock(&osdc->request_mutex);
Sage Weil153a0082010-02-15 12:11:51 -0800923 if (kickosd) {
924 __reset_osd(osdc, kickosd);
925 } else {
Sage Weilf24e9982009-10-06 11:31:10 -0700926 for (p = rb_first(&osdc->osds); p; p = n) {
927 struct ceph_osd *osd =
928 rb_entry(p, struct ceph_osd, o_node);
929
930 n = rb_next(p);
931 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
Sage Weil103e2d32010-01-07 16:12:36 -0800932 memcmp(&osd->o_con.peer_addr,
933 ceph_osd_addr(osdc->osdmap,
934 osd->o_osd),
935 sizeof(struct ceph_entity_addr)) != 0)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800936 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700937 }
938 }
939
940 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
941 req = rb_entry(p, struct ceph_osd_request, r_node);
942
943 if (req->r_resend) {
944 dout(" r_resend set on tid %llu\n", req->r_tid);
Sage Weil266673d2009-10-09 10:31:32 -0700945 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700946 goto kick;
947 }
Sage Weil266673d2009-10-09 10:31:32 -0700948 if (req->r_osd && kickosd == req->r_osd) {
949 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700950 goto kick;
Sage Weil266673d2009-10-09 10:31:32 -0700951 }
Sage Weilf24e9982009-10-06 11:31:10 -0700952
953 err = __map_osds(osdc, req);
954 if (err == 0)
955 continue; /* no change */
956 if (err < 0) {
957 /*
958 * FIXME: really, we should set the request
959 * error and fail if this isn't a 'nofail'
960 * request, but that's a fair bit more
961 * complicated to do. So retry!
962 */
963 dout(" setting r_resend on %llu\n", req->r_tid);
964 req->r_resend = true;
965 continue;
966 }
967 if (req->r_osd == NULL) {
968 dout("tid %llu maps to no valid osd\n", req->r_tid);
969 needmap++; /* request a newer map */
970 continue;
971 }
972
973kick:
Sage Weilc1ea8822009-10-08 16:55:47 -0700974 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
975 req->r_osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700976 req->r_flags |= CEPH_OSD_FLAG_RETRY;
977 err = __send_request(osdc, req);
978 if (err) {
979 dout(" setting r_resend on %llu\n", req->r_tid);
980 req->r_resend = true;
981 }
982 }
983 mutex_unlock(&osdc->request_mutex);
984
985 if (needmap) {
986 dout("%d requests for down osds, need new map\n", needmap);
987 ceph_monc_request_next_osdmap(&osdc->client->monc);
988 }
989}
990
991/*
992 * Process updated osd map.
993 *
994 * The message contains any number of incremental and full maps, normally
995 * indicating some sort of topology change in the cluster. Kick requests
996 * off to different OSDs as needed.
997 */
998void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
999{
1000 void *p, *end, *next;
1001 u32 nr_maps, maplen;
1002 u32 epoch;
1003 struct ceph_osdmap *newmap = NULL, *oldmap;
1004 int err;
1005 struct ceph_fsid fsid;
1006
1007 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1008 p = msg->front.iov_base;
1009 end = p + msg->front.iov_len;
1010
1011 /* verify fsid */
1012 ceph_decode_need(&p, end, sizeof(fsid), bad);
1013 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001014 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1015 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001016
1017 down_write(&osdc->map_sem);
1018
1019 /* incremental maps */
1020 ceph_decode_32_safe(&p, end, nr_maps, bad);
1021 dout(" %d inc maps\n", nr_maps);
1022 while (nr_maps > 0) {
1023 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001024 epoch = ceph_decode_32(&p);
1025 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001026 ceph_decode_need(&p, end, maplen, bad);
1027 next = p + maplen;
1028 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1029 dout("applying incremental map %u len %d\n",
1030 epoch, maplen);
1031 newmap = osdmap_apply_incremental(&p, next,
1032 osdc->osdmap,
1033 osdc->client->msgr);
1034 if (IS_ERR(newmap)) {
1035 err = PTR_ERR(newmap);
1036 goto bad;
1037 }
Sage Weil30dc6382009-12-21 14:49:37 -08001038 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001039 if (newmap != osdc->osdmap) {
1040 ceph_osdmap_destroy(osdc->osdmap);
1041 osdc->osdmap = newmap;
1042 }
1043 } else {
1044 dout("ignoring incremental map %u len %d\n",
1045 epoch, maplen);
1046 }
1047 p = next;
1048 nr_maps--;
1049 }
1050 if (newmap)
1051 goto done;
1052
1053 /* full maps */
1054 ceph_decode_32_safe(&p, end, nr_maps, bad);
1055 dout(" %d full maps\n", nr_maps);
1056 while (nr_maps) {
1057 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001058 epoch = ceph_decode_32(&p);
1059 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001060 ceph_decode_need(&p, end, maplen, bad);
1061 if (nr_maps > 1) {
1062 dout("skipping non-latest full map %u len %d\n",
1063 epoch, maplen);
1064 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1065 dout("skipping full map %u len %d, "
1066 "older than our %u\n", epoch, maplen,
1067 osdc->osdmap->epoch);
1068 } else {
1069 dout("taking full map %u len %d\n", epoch, maplen);
1070 newmap = osdmap_decode(&p, p+maplen);
1071 if (IS_ERR(newmap)) {
1072 err = PTR_ERR(newmap);
1073 goto bad;
1074 }
Sage Weil30dc6382009-12-21 14:49:37 -08001075 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001076 oldmap = osdc->osdmap;
1077 osdc->osdmap = newmap;
1078 if (oldmap)
1079 ceph_osdmap_destroy(oldmap);
1080 }
1081 p += maplen;
1082 nr_maps--;
1083 }
1084
1085done:
1086 downgrade_write(&osdc->map_sem);
1087 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1088 if (newmap)
1089 kick_requests(osdc, NULL);
1090 up_read(&osdc->map_sem);
1091 return;
1092
1093bad:
1094 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001095 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001096 up_write(&osdc->map_sem);
1097 return;
1098}
1099
1100
1101/*
1102 * A read request prepares specific pages that data is to be read into.
1103 * When a message is being read off the wire, we call prepare_pages to
1104 * find those pages.
1105 * 0 = success, -1 failure.
1106 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001107static int __prepare_pages(struct ceph_connection *con,
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001108 struct ceph_msg_header *hdr,
1109 struct ceph_osd_request *req,
1110 u64 tid,
1111 struct ceph_msg *m)
Sage Weilf24e9982009-10-06 11:31:10 -07001112{
1113 struct ceph_osd *osd = con->private;
1114 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001115 int ret = -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001116 int data_len = le32_to_cpu(hdr->data_len);
1117 unsigned data_off = le16_to_cpu(hdr->data_off);
1118
1119 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07001120
1121 if (!osd)
1122 return -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001123
Sage Weilf24e9982009-10-06 11:31:10 -07001124 osdc = osd->o_osdc;
1125
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001126 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
Sage Weilf24e9982009-10-06 11:31:10 -07001127 tid, req->r_num_pages, want);
Sage Weil350b1c32009-12-22 10:45:45 -08001128 if (unlikely(req->r_num_pages < want))
1129 goto out;
Sage Weil350b1c32009-12-22 10:45:45 -08001130 m->pages = req->r_pages;
1131 m->nr_pages = req->r_num_pages;
1132 ret = 0; /* success */
Sage Weilf24e9982009-10-06 11:31:10 -07001133out:
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001134 BUG_ON(ret < 0 || m->nr_pages < want);
1135
Sage Weilf24e9982009-10-06 11:31:10 -07001136 return ret;
1137}
1138
1139/*
1140 * Register request, send initial attempt.
1141 */
1142int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1143 struct ceph_osd_request *req,
1144 bool nofail)
1145{
Sage Weilc1ea8822009-10-08 16:55:47 -07001146 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001147
1148 req->r_request->pages = req->r_pages;
1149 req->r_request->nr_pages = req->r_num_pages;
1150
1151 register_request(osdc, req);
1152
1153 down_read(&osdc->map_sem);
1154 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001155 /*
1156 * a racing kick_requests() may have sent the message for us
1157 * while we dropped request_mutex above, so only send now if
1158 * the request still han't been touched yet.
1159 */
1160 if (req->r_sent == 0) {
1161 rc = __send_request(osdc, req);
1162 if (rc) {
1163 if (nofail) {
1164 dout("osdc_start_request failed send, "
1165 " marking %lld\n", req->r_tid);
1166 req->r_resend = true;
1167 rc = 0;
1168 } else {
1169 __unregister_request(osdc, req);
1170 }
Sage Weilf24e9982009-10-06 11:31:10 -07001171 }
1172 }
1173 mutex_unlock(&osdc->request_mutex);
1174 up_read(&osdc->map_sem);
1175 return rc;
1176}
1177
1178/*
1179 * wait for a request to complete
1180 */
1181int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1182 struct ceph_osd_request *req)
1183{
1184 int rc;
1185
1186 rc = wait_for_completion_interruptible(&req->r_completion);
1187 if (rc < 0) {
1188 mutex_lock(&osdc->request_mutex);
1189 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001190 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001191 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001192 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001193 return rc;
1194 }
1195
1196 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1197 return req->r_result;
1198}
1199
1200/*
1201 * sync - wait for all in-flight requests to flush. avoid starvation.
1202 */
1203void ceph_osdc_sync(struct ceph_osd_client *osdc)
1204{
1205 struct ceph_osd_request *req;
1206 u64 last_tid, next_tid = 0;
1207
1208 mutex_lock(&osdc->request_mutex);
1209 last_tid = osdc->last_tid;
1210 while (1) {
1211 req = __lookup_request_ge(osdc, next_tid);
1212 if (!req)
1213 break;
1214 if (req->r_tid > last_tid)
1215 break;
1216
1217 next_tid = req->r_tid + 1;
1218 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1219 continue;
1220
1221 ceph_osdc_get_request(req);
1222 mutex_unlock(&osdc->request_mutex);
1223 dout("sync waiting on tid %llu (last is %llu)\n",
1224 req->r_tid, last_tid);
1225 wait_for_completion(&req->r_safe_completion);
1226 mutex_lock(&osdc->request_mutex);
1227 ceph_osdc_put_request(req);
1228 }
1229 mutex_unlock(&osdc->request_mutex);
1230 dout("sync done (thru tid %llu)\n", last_tid);
1231}
1232
1233/*
1234 * init, shutdown
1235 */
1236int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1237{
1238 int err;
1239
1240 dout("init\n");
1241 osdc->client = client;
1242 osdc->osdmap = NULL;
1243 init_rwsem(&osdc->map_sem);
1244 init_completion(&osdc->map_waiters);
1245 osdc->last_requested_map = 0;
1246 mutex_init(&osdc->request_mutex);
1247 osdc->timeout_tid = 0;
1248 osdc->last_tid = 0;
1249 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001250 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001251 osdc->requests = RB_ROOT;
1252 osdc->num_requests = 0;
1253 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001254 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1255
1256 schedule_delayed_work(&osdc->osds_timeout_work,
1257 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001258
Sage Weil5f44f142009-11-18 14:52:18 -08001259 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001260 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1261 sizeof(struct ceph_osd_request));
1262 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001263 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001264
1265 err = ceph_msgpool_init(&osdc->msgpool_op, 4096, 10, true);
1266 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001267 goto out_mempool;
Sage Weilf24e9982009-10-06 11:31:10 -07001268 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001269
Sage Weil5f44f142009-11-18 14:52:18 -08001270out_mempool:
1271 mempool_destroy(osdc->req_mempool);
1272out:
1273 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001274}
1275
1276void ceph_osdc_stop(struct ceph_osd_client *osdc)
1277{
1278 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001279 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001280 if (osdc->osdmap) {
1281 ceph_osdmap_destroy(osdc->osdmap);
1282 osdc->osdmap = NULL;
1283 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001284 remove_old_osds(osdc, 1);
Sage Weilf24e9982009-10-06 11:31:10 -07001285 mempool_destroy(osdc->req_mempool);
1286 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilf24e9982009-10-06 11:31:10 -07001287}
1288
1289/*
1290 * Read some contiguous pages. If we cross a stripe boundary, shorten
1291 * *plen. Return number of bytes read, or error.
1292 */
1293int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1294 struct ceph_vino vino, struct ceph_file_layout *layout,
1295 u64 off, u64 *plen,
1296 u32 truncate_seq, u64 truncate_size,
1297 struct page **pages, int num_pages)
1298{
1299 struct ceph_osd_request *req;
1300 int rc = 0;
1301
1302 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1303 vino.snap, off, *plen);
1304 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1305 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1306 NULL, 0, truncate_seq, truncate_size, NULL,
1307 false, 1);
1308 if (IS_ERR(req))
1309 return PTR_ERR(req);
1310
1311 /* it may be a short read due to an object boundary */
1312 req->r_pages = pages;
1313 num_pages = calc_pages_for(off, *plen);
1314 req->r_num_pages = num_pages;
1315
1316 dout("readpages final extent is %llu~%llu (%d pages)\n",
1317 off, *plen, req->r_num_pages);
1318
1319 rc = ceph_osdc_start_request(osdc, req, false);
1320 if (!rc)
1321 rc = ceph_osdc_wait_request(osdc, req);
1322
1323 ceph_osdc_put_request(req);
1324 dout("readpages result %d\n", rc);
1325 return rc;
1326}
1327
1328/*
1329 * do a synchronous write on N pages
1330 */
1331int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1332 struct ceph_file_layout *layout,
1333 struct ceph_snap_context *snapc,
1334 u64 off, u64 len,
1335 u32 truncate_seq, u64 truncate_size,
1336 struct timespec *mtime,
1337 struct page **pages, int num_pages,
1338 int flags, int do_sync, bool nofail)
1339{
1340 struct ceph_osd_request *req;
1341 int rc = 0;
1342
1343 BUG_ON(vino.snap != CEPH_NOSNAP);
1344 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1345 CEPH_OSD_OP_WRITE,
1346 flags | CEPH_OSD_FLAG_ONDISK |
1347 CEPH_OSD_FLAG_WRITE,
1348 snapc, do_sync,
1349 truncate_seq, truncate_size, mtime,
1350 nofail, 1);
1351 if (IS_ERR(req))
1352 return PTR_ERR(req);
1353
1354 /* it may be a short write due to an object boundary */
1355 req->r_pages = pages;
1356 req->r_num_pages = calc_pages_for(off, len);
1357 dout("writepages %llu~%llu (%d pages)\n", off, len,
1358 req->r_num_pages);
1359
1360 rc = ceph_osdc_start_request(osdc, req, nofail);
1361 if (!rc)
1362 rc = ceph_osdc_wait_request(osdc, req);
1363
1364 ceph_osdc_put_request(req);
1365 if (rc == 0)
1366 rc = len;
1367 dout("writepages result %d\n", rc);
1368 return rc;
1369}
1370
1371/*
1372 * handle incoming message
1373 */
1374static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1375{
1376 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001377 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001378 int type = le16_to_cpu(msg->hdr.type);
1379
1380 if (!osd)
1381 return;
Julia Lawall32c895e2009-11-21 16:53:16 +01001382 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001383
1384 switch (type) {
1385 case CEPH_MSG_OSD_MAP:
1386 ceph_osdc_handle_map(osdc, msg);
1387 break;
1388 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001389 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001390 break;
1391
1392 default:
1393 pr_err("received unknown message type %d %s\n", type,
1394 ceph_msg_type_name(type));
1395 }
1396 ceph_msg_put(msg);
1397}
1398
1399static struct ceph_msg *alloc_msg(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001400 struct ceph_msg_header *hdr,
1401 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001402{
1403 struct ceph_osd *osd = con->private;
1404 struct ceph_osd_client *osdc = osd->o_osdc;
1405 int type = le16_to_cpu(hdr->type);
Sage Weil8f3bc052009-10-14 17:36:07 -07001406 int front = le32_to_cpu(hdr->front_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001407 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001408 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001409 struct ceph_osd_request *req;
1410 u64 tid;
1411 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001412
Yehuda Sadeh24504182010-01-08 13:58:34 -08001413 *skip = 0;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001414 if (type != CEPH_MSG_OSD_OPREPLY)
Yehuda Sadeh24504182010-01-08 13:58:34 -08001415 return NULL;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001416
1417 tid = le64_to_cpu(hdr->tid);
1418 mutex_lock(&osdc->request_mutex);
1419 req = __lookup_request(osdc, tid);
1420 if (!req) {
1421 *skip = 1;
1422 m = NULL;
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001423 dout("alloc_msg unknown tid %llu\n", tid);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001424 goto out;
1425 }
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001426 m = __get_next_reply(con, req, front);
1427 if (!m || IS_ERR(m)) {
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001428 *skip = 1;
1429 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001430 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001431
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001432 if (data_len > 0) {
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001433 err = __prepare_pages(con, hdr, req, tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001434 if (err < 0) {
1435 *skip = 1;
1436 ceph_msg_put(m);
1437 m = ERR_PTR(err);
1438 }
1439 }
1440
1441out:
1442 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001443
1444 return m;
Sage Weilf24e9982009-10-06 11:31:10 -07001445}
1446
1447/*
1448 * Wrappers to refcount containing ceph_osd struct
1449 */
1450static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1451{
1452 struct ceph_osd *osd = con->private;
1453 if (get_osd(osd))
1454 return con;
1455 return NULL;
1456}
1457
1458static void put_osd_con(struct ceph_connection *con)
1459{
1460 struct ceph_osd *osd = con->private;
1461 put_osd(osd);
1462}
1463
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001464/*
1465 * authentication
1466 */
1467static int get_authorizer(struct ceph_connection *con,
Sage Weil50b885b2009-12-01 14:12:07 -08001468 void **buf, int *len, int *proto,
1469 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001470{
1471 struct ceph_osd *o = con->private;
1472 struct ceph_osd_client *osdc = o->o_osdc;
1473 struct ceph_auth_client *ac = osdc->client->monc.auth;
1474 int ret = 0;
1475
1476 if (force_new && o->o_authorizer) {
1477 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1478 o->o_authorizer = NULL;
1479 }
1480 if (o->o_authorizer == NULL) {
1481 ret = ac->ops->create_authorizer(
1482 ac, CEPH_ENTITY_TYPE_OSD,
1483 &o->o_authorizer,
1484 &o->o_authorizer_buf,
1485 &o->o_authorizer_buf_len,
1486 &o->o_authorizer_reply_buf,
1487 &o->o_authorizer_reply_buf_len);
1488 if (ret)
1489 return ret;
1490 }
1491
1492 *proto = ac->protocol;
1493 *buf = o->o_authorizer_buf;
1494 *len = o->o_authorizer_buf_len;
1495 *reply_buf = o->o_authorizer_reply_buf;
1496 *reply_len = o->o_authorizer_reply_buf_len;
1497 return 0;
1498}
1499
1500
1501static int verify_authorizer_reply(struct ceph_connection *con, int len)
1502{
1503 struct ceph_osd *o = con->private;
1504 struct ceph_osd_client *osdc = o->o_osdc;
1505 struct ceph_auth_client *ac = osdc->client->monc.auth;
1506
1507 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1508}
1509
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001510static int invalidate_authorizer(struct ceph_connection *con)
1511{
1512 struct ceph_osd *o = con->private;
1513 struct ceph_osd_client *osdc = o->o_osdc;
1514 struct ceph_auth_client *ac = osdc->client->monc.auth;
1515
1516 if (ac->ops->invalidate_authorizer)
1517 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1518
1519 return ceph_monc_validate_auth(&osdc->client->monc);
1520}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001521
Sage Weilf24e9982009-10-06 11:31:10 -07001522const static struct ceph_connection_operations osd_con_ops = {
1523 .get = get_osd_con,
1524 .put = put_osd_con,
1525 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001526 .get_authorizer = get_authorizer,
1527 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001528 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07001529 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07001530 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07001531};