blob: 8128082a028eb2fe40601e058b5b47ef443b7076 [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
Sage Weilc16e7862010-03-01 13:02:00 -080016#define OSD_OP_FRONT_LEN 4096
17#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080018
Sage Weilf24e9982009-10-06 11:31:10 -070019const static struct ceph_connection_operations osd_con_ops;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -080020static int __kick_requests(struct ceph_osd_client *osdc,
21 struct ceph_osd *kickosd);
Sage Weilf24e9982009-10-06 11:31:10 -070022
23static void kick_requests(struct ceph_osd_client *osdc, struct ceph_osd *osd);
24
25/*
26 * Implement client access to distributed object storage cluster.
27 *
28 * All data objects are stored within a cluster/cloud of OSDs, or
29 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
30 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
31 * remote daemons serving up and coordinating consistent and safe
32 * access to storage.
33 *
34 * Cluster membership and the mapping of data objects onto storage devices
35 * are described by the osd map.
36 *
37 * We keep track of pending OSD requests (read, write), resubmit
38 * requests to different OSDs when the cluster topology/data layout
39 * change, or retry the affected requests when the communications
40 * channel with an OSD is reset.
41 */
42
43/*
44 * calculate the mapping of a file extent onto an object, and fill out the
45 * request accordingly. shorten extent as necessary if it crosses an
46 * object boundary.
47 *
48 * fill osd op in request message.
49 */
50static void calc_layout(struct ceph_osd_client *osdc,
51 struct ceph_vino vino, struct ceph_file_layout *layout,
52 u64 off, u64 *plen,
53 struct ceph_osd_request *req)
54{
55 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
56 struct ceph_osd_op *op = (void *)(reqhead + 1);
57 u64 orig_len = *plen;
58 u64 objoff, objlen; /* extent in object */
59 u64 bno;
60
61 reqhead->snapid = cpu_to_le64(vino.snap);
62
63 /* object extent? */
64 ceph_calc_file_object_mapping(layout, off, plen, &bno,
65 &objoff, &objlen);
66 if (*plen < orig_len)
67 dout(" skipping last %llu, final file extent %llu~%llu\n",
68 orig_len - *plen, off, *plen);
69
70 sprintf(req->r_oid, "%llx.%08llx", vino.ino, bno);
71 req->r_oid_len = strlen(req->r_oid);
72
73 op->extent.offset = cpu_to_le64(objoff);
74 op->extent.length = cpu_to_le64(objlen);
75 req->r_num_pages = calc_pages_for(off, *plen);
76
77 dout("calc_layout %s (%d) %llu~%llu (%d pages)\n",
78 req->r_oid, req->r_oid_len, objoff, objlen, req->r_num_pages);
79}
80
Sage Weilf24e9982009-10-06 11:31:10 -070081/*
82 * requests
83 */
Sage Weil415e49a2009-12-07 13:37:03 -080084void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -070085{
Sage Weil415e49a2009-12-07 13:37:03 -080086 struct ceph_osd_request *req = container_of(kref,
87 struct ceph_osd_request,
88 r_kref);
89
90 if (req->r_request)
91 ceph_msg_put(req->r_request);
92 if (req->r_reply)
93 ceph_msg_put(req->r_reply);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080094 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -080095 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080096 req->r_pages, req->r_con_filling_msg);
97 ceph_con_revoke_message(req->r_con_filling_msg,
98 req->r_reply);
99 ceph_con_put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800100 }
Sage Weil415e49a2009-12-07 13:37:03 -0800101 if (req->r_own_pages)
102 ceph_release_page_vector(req->r_pages,
103 req->r_num_pages);
104 ceph_put_snap_context(req->r_snapc);
105 if (req->r_mempool)
106 mempool_free(req, req->r_osdc->req_mempool);
107 else
108 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700109}
110
111/*
112 * build new request AND message, calculate layout, and adjust file
113 * extent as needed.
114 *
115 * if the file was recently truncated, we include information about its
116 * old and new size so that the object can be updated appropriately. (we
117 * avoid synchronously deleting truncated objects because it's slow.)
118 *
119 * if @do_sync, include a 'startsync' command so that the osd will flush
120 * data quickly.
121 */
122struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
123 struct ceph_file_layout *layout,
124 struct ceph_vino vino,
125 u64 off, u64 *plen,
126 int opcode, int flags,
127 struct ceph_snap_context *snapc,
128 int do_sync,
129 u32 truncate_seq,
130 u64 truncate_size,
131 struct timespec *mtime,
132 bool use_mempool, int num_reply)
133{
134 struct ceph_osd_request *req;
135 struct ceph_msg *msg;
136 struct ceph_osd_request_head *head;
137 struct ceph_osd_op *op;
138 void *p;
Yehuda Sadeh0c948992010-02-01 16:10:45 -0800139 int num_op = 1 + do_sync;
Sage Weilf24e9982009-10-06 11:31:10 -0700140 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Sage Weilc16e7862010-03-01 13:02:00 -0800141 int i;
Sage Weilf24e9982009-10-06 11:31:10 -0700142
143 if (use_mempool) {
144 req = mempool_alloc(osdc->req_mempool, GFP_NOFS);
145 memset(req, 0, sizeof(*req));
146 } else {
147 req = kzalloc(sizeof(*req), GFP_NOFS);
148 }
149 if (req == NULL)
150 return ERR_PTR(-ENOMEM);
151
Sage Weilf24e9982009-10-06 11:31:10 -0700152 req->r_osdc = osdc;
153 req->r_mempool = use_mempool;
Sage Weil415e49a2009-12-07 13:37:03 -0800154 kref_init(&req->r_kref);
Sage Weilf24e9982009-10-06 11:31:10 -0700155 init_completion(&req->r_completion);
156 init_completion(&req->r_safe_completion);
157 INIT_LIST_HEAD(&req->r_unsafe_item);
158 req->r_flags = flags;
159
160 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
161
Sage Weilc16e7862010-03-01 13:02:00 -0800162 /* create reply message */
163 if (use_mempool)
164 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
165 else
166 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
167 OSD_OPREPLY_FRONT_LEN, 0, 0, NULL);
168 if (IS_ERR(msg)) {
169 ceph_osdc_put_request(req);
170 return ERR_PTR(PTR_ERR(msg));
171 }
172 req->r_reply = msg;
173
174 /* create request message; allow space for oid */
Sage Weilf24e9982009-10-06 11:31:10 -0700175 msg_size += 40;
176 if (snapc)
177 msg_size += sizeof(u64) * snapc->num_snaps;
178 if (use_mempool)
Sage Weil8f3bc052009-10-14 17:36:07 -0700179 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
Sage Weilf24e9982009-10-06 11:31:10 -0700180 else
181 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, 0, 0, NULL);
182 if (IS_ERR(msg)) {
Sage Weilf24e9982009-10-06 11:31:10 -0700183 ceph_osdc_put_request(req);
184 return ERR_PTR(PTR_ERR(msg));
185 }
186 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
187 memset(msg->front.iov_base, 0, msg->front.iov_len);
188 head = msg->front.iov_base;
189 op = (void *)(head + 1);
190 p = (void *)(op + num_op);
191
192 req->r_request = msg;
193 req->r_snapc = ceph_get_snap_context(snapc);
194
195 head->client_inc = cpu_to_le32(1); /* always, for now. */
196 head->flags = cpu_to_le32(flags);
197 if (flags & CEPH_OSD_FLAG_WRITE)
198 ceph_encode_timespec(&head->mtime, mtime);
199 head->num_ops = cpu_to_le16(num_op);
200 op->op = cpu_to_le16(opcode);
201
202 /* calculate max write size */
203 calc_layout(osdc, vino, layout, off, plen, req);
204 req->r_file_layout = *layout; /* keep a copy */
205
206 if (flags & CEPH_OSD_FLAG_WRITE) {
207 req->r_request->hdr.data_off = cpu_to_le16(off);
208 req->r_request->hdr.data_len = cpu_to_le32(*plen);
209 op->payload_len = cpu_to_le32(*plen);
210 }
Yehuda Sadeh0c948992010-02-01 16:10:45 -0800211 op->extent.truncate_size = cpu_to_le64(truncate_size);
212 op->extent.truncate_seq = cpu_to_le32(truncate_seq);
Sage Weilf24e9982009-10-06 11:31:10 -0700213
214 /* fill in oid */
215 head->object_len = cpu_to_le32(req->r_oid_len);
216 memcpy(p, req->r_oid, req->r_oid_len);
217 p += req->r_oid_len;
218
Sage Weilf24e9982009-10-06 11:31:10 -0700219 if (do_sync) {
220 op++;
221 op->op = cpu_to_le16(CEPH_OSD_OP_STARTSYNC);
222 }
223 if (snapc) {
224 head->snap_seq = cpu_to_le64(snapc->seq);
225 head->num_snaps = cpu_to_le32(snapc->num_snaps);
226 for (i = 0; i < snapc->num_snaps; i++) {
227 put_unaligned_le64(snapc->snaps[i], p);
228 p += sizeof(u64);
229 }
230 }
231
232 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
Sage Weil6f863e72010-03-01 15:26:41 -0800233 msg_size = p - msg->front.iov_base;
234 msg->front.iov_len = msg_size;
235 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weilf24e9982009-10-06 11:31:10 -0700236 return req;
237}
238
239/*
240 * We keep osd requests in an rbtree, sorted by ->r_tid.
241 */
242static void __insert_request(struct ceph_osd_client *osdc,
243 struct ceph_osd_request *new)
244{
245 struct rb_node **p = &osdc->requests.rb_node;
246 struct rb_node *parent = NULL;
247 struct ceph_osd_request *req = NULL;
248
249 while (*p) {
250 parent = *p;
251 req = rb_entry(parent, struct ceph_osd_request, r_node);
252 if (new->r_tid < req->r_tid)
253 p = &(*p)->rb_left;
254 else if (new->r_tid > req->r_tid)
255 p = &(*p)->rb_right;
256 else
257 BUG();
258 }
259
260 rb_link_node(&new->r_node, parent, p);
261 rb_insert_color(&new->r_node, &osdc->requests);
262}
263
264static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
265 u64 tid)
266{
267 struct ceph_osd_request *req;
268 struct rb_node *n = osdc->requests.rb_node;
269
270 while (n) {
271 req = rb_entry(n, struct ceph_osd_request, r_node);
272 if (tid < req->r_tid)
273 n = n->rb_left;
274 else if (tid > req->r_tid)
275 n = n->rb_right;
276 else
277 return req;
278 }
279 return NULL;
280}
281
282static struct ceph_osd_request *
283__lookup_request_ge(struct ceph_osd_client *osdc,
284 u64 tid)
285{
286 struct ceph_osd_request *req;
287 struct rb_node *n = osdc->requests.rb_node;
288
289 while (n) {
290 req = rb_entry(n, struct ceph_osd_request, r_node);
291 if (tid < req->r_tid) {
292 if (!n->rb_left)
293 return req;
294 n = n->rb_left;
295 } else if (tid > req->r_tid) {
296 n = n->rb_right;
297 } else {
298 return req;
299 }
300 }
301 return NULL;
302}
303
304
305/*
Sage Weil81b024e2009-10-09 10:29:18 -0700306 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700307 */
308static void osd_reset(struct ceph_connection *con)
309{
310 struct ceph_osd *osd = con->private;
311 struct ceph_osd_client *osdc;
312
313 if (!osd)
314 return;
315 dout("osd_reset osd%d\n", osd->o_osd);
316 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700317 down_read(&osdc->map_sem);
318 kick_requests(osdc, osd);
319 up_read(&osdc->map_sem);
320}
321
322/*
323 * Track open sessions with osds.
324 */
325static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
326{
327 struct ceph_osd *osd;
328
329 osd = kzalloc(sizeof(*osd), GFP_NOFS);
330 if (!osd)
331 return NULL;
332
333 atomic_set(&osd->o_ref, 1);
334 osd->o_osdc = osdc;
335 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800336 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700337 osd->o_incarnation = 1;
338
339 ceph_con_init(osdc->client->msgr, &osd->o_con);
340 osd->o_con.private = osd;
341 osd->o_con.ops = &osd_con_ops;
342 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800343
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800344 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700345 return osd;
346}
347
348static struct ceph_osd *get_osd(struct ceph_osd *osd)
349{
350 if (atomic_inc_not_zero(&osd->o_ref)) {
351 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
352 atomic_read(&osd->o_ref));
353 return osd;
354 } else {
355 dout("get_osd %p FAIL\n", osd);
356 return NULL;
357 }
358}
359
360static void put_osd(struct ceph_osd *osd)
361{
362 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
363 atomic_read(&osd->o_ref) - 1);
Sage Weil42ce56e2009-11-18 11:22:36 -0800364 if (atomic_dec_and_test(&osd->o_ref))
Sage Weilf24e9982009-10-06 11:31:10 -0700365 kfree(osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700366}
367
368/*
369 * remove an osd from our map
370 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800371static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700372{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800373 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700374 BUG_ON(!list_empty(&osd->o_requests));
375 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800376 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700377 ceph_con_close(&osd->o_con);
378 put_osd(osd);
379}
380
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800381static void __move_osd_to_lru(struct ceph_osd_client *osdc,
382 struct ceph_osd *osd)
383{
384 dout("__move_osd_to_lru %p\n", osd);
385 BUG_ON(!list_empty(&osd->o_osd_lru));
386 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
387 osd->lru_ttl = jiffies + osdc->client->mount_args->osd_idle_ttl * HZ;
388}
389
390static void __remove_osd_from_lru(struct ceph_osd *osd)
391{
392 dout("__remove_osd_from_lru %p\n", osd);
393 if (!list_empty(&osd->o_osd_lru))
394 list_del_init(&osd->o_osd_lru);
395}
396
397static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
398{
399 struct ceph_osd *osd, *nosd;
400
401 dout("__remove_old_osds %p\n", osdc);
402 mutex_lock(&osdc->request_mutex);
403 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
404 if (!remove_all && time_before(jiffies, osd->lru_ttl))
405 break;
406 __remove_osd(osdc, osd);
407 }
408 mutex_unlock(&osdc->request_mutex);
409}
410
Sage Weilf24e9982009-10-06 11:31:10 -0700411/*
412 * reset osd connect
413 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800414static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700415{
Sage Weil87b315a2010-03-22 14:51:18 -0700416 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700417 int ret = 0;
418
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800419 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700420 if (list_empty(&osd->o_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800421 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700422 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
423 &osd->o_con.peer_addr,
424 sizeof(osd->o_con.peer_addr)) == 0 &&
425 !ceph_con_opened(&osd->o_con)) {
426 dout(" osd addr hasn't changed and connection never opened,"
427 " letting msgr retry");
428 /* touch each r_stamp for handle_timeout()'s benfit */
429 list_for_each_entry(req, &osd->o_requests, r_osd_item)
430 req->r_stamp = jiffies;
431 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700432 } else {
433 ceph_con_close(&osd->o_con);
434 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
435 osd->o_incarnation++;
436 }
437 return ret;
438}
439
440static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
441{
442 struct rb_node **p = &osdc->osds.rb_node;
443 struct rb_node *parent = NULL;
444 struct ceph_osd *osd = NULL;
445
446 while (*p) {
447 parent = *p;
448 osd = rb_entry(parent, struct ceph_osd, o_node);
449 if (new->o_osd < osd->o_osd)
450 p = &(*p)->rb_left;
451 else if (new->o_osd > osd->o_osd)
452 p = &(*p)->rb_right;
453 else
454 BUG();
455 }
456
457 rb_link_node(&new->o_node, parent, p);
458 rb_insert_color(&new->o_node, &osdc->osds);
459}
460
461static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
462{
463 struct ceph_osd *osd;
464 struct rb_node *n = osdc->osds.rb_node;
465
466 while (n) {
467 osd = rb_entry(n, struct ceph_osd, o_node);
468 if (o < osd->o_osd)
469 n = n->rb_left;
470 else if (o > osd->o_osd)
471 n = n->rb_right;
472 else
473 return osd;
474 }
475 return NULL;
476}
477
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800478static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
479{
480 schedule_delayed_work(&osdc->timeout_work,
481 osdc->client->mount_args->osd_keepalive_timeout * HZ);
482}
483
484static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
485{
486 cancel_delayed_work(&osdc->timeout_work);
487}
Sage Weilf24e9982009-10-06 11:31:10 -0700488
489/*
490 * Register request, assign tid. If this is the first request, set up
491 * the timeout event.
492 */
493static void register_request(struct ceph_osd_client *osdc,
494 struct ceph_osd_request *req)
495{
Sage Weilf24e9982009-10-06 11:31:10 -0700496 mutex_lock(&osdc->request_mutex);
497 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800498 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800499 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700500
501 dout("register_request %p tid %lld\n", req, req->r_tid);
502 __insert_request(osdc, req);
503 ceph_osdc_get_request(req);
504 osdc->num_requests++;
505
Sage Weilf24e9982009-10-06 11:31:10 -0700506 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800507 dout(" first request, scheduling timeout\n");
508 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700509 }
510 mutex_unlock(&osdc->request_mutex);
511}
512
513/*
514 * called under osdc->request_mutex
515 */
516static void __unregister_request(struct ceph_osd_client *osdc,
517 struct ceph_osd_request *req)
518{
519 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
520 rb_erase(&req->r_node, &osdc->requests);
521 osdc->num_requests--;
522
Sage Weil0ba64782009-10-08 16:57:16 -0700523 if (req->r_osd) {
524 /* make sure the original request isn't in flight. */
525 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
526
527 list_del_init(&req->r_osd_item);
528 if (list_empty(&req->r_osd->o_requests))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800529 __move_osd_to_lru(osdc, req->r_osd);
Sage Weil0ba64782009-10-08 16:57:16 -0700530 req->r_osd = NULL;
531 }
Sage Weilf24e9982009-10-06 11:31:10 -0700532
533 ceph_osdc_put_request(req);
534
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800535 list_del_init(&req->r_req_lru_item);
536 if (osdc->num_requests == 0) {
537 dout(" no requests, canceling timeout\n");
538 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700539 }
540}
541
542/*
543 * Cancel a previously queued request message
544 */
545static void __cancel_request(struct ceph_osd_request *req)
546{
547 if (req->r_sent) {
548 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
549 req->r_sent = 0;
550 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800551 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700552}
553
554/*
555 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
556 * (as needed), and set the request r_osd appropriately. If there is
557 * no up osd, set r_osd to NULL.
558 *
559 * Return 0 if unchanged, 1 if changed, or negative on error.
560 *
561 * Caller should hold map_sem for read and request_mutex.
562 */
563static int __map_osds(struct ceph_osd_client *osdc,
564 struct ceph_osd_request *req)
565{
566 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800567 struct ceph_pg pgid;
Sage Weilf24e9982009-10-06 11:31:10 -0700568 int o = -1;
569 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700570
571 dout("map_osds %p tid %lld\n", req, req->r_tid);
572 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
573 &req->r_file_layout, osdc->osdmap);
574 if (err)
575 return err;
Sage Weil51042122009-11-04 11:39:12 -0800576 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800577 req->r_pgid = pgid;
578
Sage Weilf24e9982009-10-06 11:31:10 -0700579 o = ceph_calc_pg_primary(osdc->osdmap, pgid);
580
581 if ((req->r_osd && req->r_osd->o_osd == o &&
582 req->r_sent >= req->r_osd->o_incarnation) ||
583 (req->r_osd == NULL && o == -1))
584 return 0; /* no change */
585
Sage Weil51042122009-11-04 11:39:12 -0800586 dout("map_osds tid %llu pgid %d.%x osd%d (was osd%d)\n",
587 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700588 req->r_osd ? req->r_osd->o_osd : -1);
589
590 if (req->r_osd) {
591 __cancel_request(req);
592 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700593 req->r_osd = NULL;
594 }
595
596 req->r_osd = __lookup_osd(osdc, o);
597 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800598 err = -ENOMEM;
599 req->r_osd = create_osd(osdc);
600 if (!req->r_osd)
601 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -0700602
603 dout("map_osds osd %p is osd%d\n", req->r_osd, o);
604 req->r_osd->o_osd = o;
605 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
606 __insert_osd(osdc, req->r_osd);
607
608 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
609 }
610
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800611 if (req->r_osd) {
612 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700613 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800614 }
Sage Weilf24e9982009-10-06 11:31:10 -0700615 err = 1; /* osd changed */
616
617out:
Sage Weilf24e9982009-10-06 11:31:10 -0700618 return err;
619}
620
621/*
622 * caller should hold map_sem (for read) and request_mutex
623 */
624static int __send_request(struct ceph_osd_client *osdc,
625 struct ceph_osd_request *req)
626{
627 struct ceph_osd_request_head *reqhead;
628 int err;
629
630 err = __map_osds(osdc, req);
631 if (err < 0)
632 return err;
633 if (req->r_osd == NULL) {
634 dout("send_request %p no up osds in pg\n", req);
635 ceph_monc_request_next_osdmap(&osdc->client->monc);
636 return 0;
637 }
638
639 dout("send_request %p tid %llu to osd%d flags %d\n",
640 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
641
642 reqhead = req->r_request->front.iov_base;
643 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
644 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
645 reqhead->reassert_version = req->r_reassert_version;
646
Sage Weil3dd72fc2010-03-22 14:42:30 -0700647 req->r_stamp = jiffies;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800648 list_move_tail(&osdc->req_lru, &req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700649
650 ceph_msg_get(req->r_request); /* send consumes a ref */
651 ceph_con_send(&req->r_osd->o_con, req->r_request);
652 req->r_sent = req->r_osd->o_incarnation;
653 return 0;
654}
655
656/*
657 * Timeout callback, called every N seconds when 1 or more osd
658 * requests has been active for more than N seconds. When this
659 * happens, we ping all OSDs with requests who have timed out to
660 * ensure any communications channel reset is detected. Reset the
661 * request timeouts another N seconds in the future as we go.
662 * Reschedule the timeout event another N seconds in future (unless
663 * there are no open requests).
664 */
665static void handle_timeout(struct work_struct *work)
666{
667 struct ceph_osd_client *osdc =
668 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800669 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700670 struct ceph_osd *osd;
Sage Weil6b805182009-10-27 11:50:50 -0700671 unsigned long timeout = osdc->client->mount_args->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800672 unsigned long keepalive =
673 osdc->client->mount_args->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -0700674 unsigned long last_stamp = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700675 struct rb_node *p;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800676 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -0700677
678 dout("timeout\n");
679 down_read(&osdc->map_sem);
680
681 ceph_monc_request_next_osdmap(&osdc->client->monc);
682
683 mutex_lock(&osdc->request_mutex);
684 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
685 req = rb_entry(p, struct ceph_osd_request, r_node);
686
687 if (req->r_resend) {
688 int err;
689
690 dout("osdc resending prev failed %lld\n", req->r_tid);
691 err = __send_request(osdc, req);
692 if (err)
693 dout("osdc failed again on %lld\n", req->r_tid);
694 else
695 req->r_resend = false;
696 continue;
697 }
698 }
Sage Weilf24e9982009-10-06 11:31:10 -0700699
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800700 /*
701 * reset osds that appear to be _really_ unresponsive. this
702 * is a failsafe measure.. we really shouldn't be getting to
703 * this point if the system is working properly. the monitors
704 * should mark the osd as failed and we should find out about
705 * it from an updated osd map.
706 */
707 while (!list_empty(&osdc->req_lru)) {
708 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
709 r_req_lru_item);
710
Sage Weil3dd72fc2010-03-22 14:42:30 -0700711 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800712 break;
713
Sage Weil3dd72fc2010-03-22 14:42:30 -0700714 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800715 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -0700716 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800717
718 osd = req->r_osd;
719 BUG_ON(!osd);
720 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
721 req->r_tid, osd->o_osd);
722 __kick_requests(osdc, osd);
723 }
724
725 /*
726 * ping osds that are a bit slow. this ensures that if there
727 * is a break in the TCP connection we will notice, and reopen
728 * a connection with that osd (from the fault callback).
729 */
730 INIT_LIST_HEAD(&slow_osds);
731 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -0700732 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800733 break;
734
735 osd = req->r_osd;
736 BUG_ON(!osd);
737 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -0700738 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800739 list_move_tail(&osd->o_keepalive_item, &slow_osds);
740 }
741 while (!list_empty(&slow_osds)) {
742 osd = list_entry(slow_osds.next, struct ceph_osd,
743 o_keepalive_item);
744 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700745 ceph_con_keepalive(&osd->o_con);
746 }
747
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800748 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700749 mutex_unlock(&osdc->request_mutex);
750
751 up_read(&osdc->map_sem);
752}
753
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800754static void handle_osds_timeout(struct work_struct *work)
755{
756 struct ceph_osd_client *osdc =
757 container_of(work, struct ceph_osd_client,
758 osds_timeout_work.work);
759 unsigned long delay =
760 osdc->client->mount_args->osd_idle_ttl * HZ >> 2;
761
762 dout("osds timeout\n");
763 down_read(&osdc->map_sem);
764 remove_old_osds(osdc, 0);
765 up_read(&osdc->map_sem);
766
767 schedule_delayed_work(&osdc->osds_timeout_work,
768 round_jiffies_relative(delay));
769}
770
Sage Weilf24e9982009-10-06 11:31:10 -0700771/*
772 * handle osd op reply. either call the callback if it is specified,
773 * or do the completion to wake up the waiting thread.
774 */
Sage Weil350b1c32009-12-22 10:45:45 -0800775static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
776 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -0700777{
778 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
779 struct ceph_osd_request *req;
780 u64 tid;
781 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -0700782 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -0700783
Sage Weil6df058c2009-12-22 11:24:33 -0800784 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700785 if (msg->front.iov_len < sizeof(*rhead))
786 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -0700787 numops = le32_to_cpu(rhead->num_ops);
788 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -0700789 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -0700790 if (msg->front.iov_len != sizeof(*rhead) + object_len +
791 numops * sizeof(struct ceph_osd_op))
792 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -0700793 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -0700794
795 /* lookup */
796 mutex_lock(&osdc->request_mutex);
797 req = __lookup_request(osdc, tid);
798 if (req == NULL) {
799 dout("handle_reply tid %llu dne\n", tid);
800 mutex_unlock(&osdc->request_mutex);
801 return;
802 }
803 ceph_osdc_get_request(req);
804 flags = le32_to_cpu(rhead->flags);
805
Sage Weil350b1c32009-12-22 10:45:45 -0800806 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800807 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -0800808 * avoid a (safe but slower) revoke later.
809 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800810 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -0800811 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800812 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800813 ceph_con_put(con);
814 }
815
Sage Weilf24e9982009-10-06 11:31:10 -0700816 if (!req->r_got_reply) {
817 unsigned bytes;
818
819 req->r_result = le32_to_cpu(rhead->result);
820 bytes = le32_to_cpu(msg->hdr.data_len);
821 dout("handle_reply result %d bytes %d\n", req->r_result,
822 bytes);
823 if (req->r_result == 0)
824 req->r_result = bytes;
825
826 /* in case this is a write and we need to replay, */
827 req->r_reassert_version = rhead->reassert_version;
828
829 req->r_got_reply = 1;
830 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
831 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -0800832 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700833 goto done;
834 }
835
836 dout("handle_reply tid %llu flags %d\n", tid, flags);
837
838 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -0700839 if (result < 0 ||
840 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700841 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
842 __unregister_request(osdc, req);
843
844 mutex_unlock(&osdc->request_mutex);
845
846 if (req->r_callback)
847 req->r_callback(req, msg);
848 else
849 complete(&req->r_completion);
850
851 if (flags & CEPH_OSD_FLAG_ONDISK) {
852 if (req->r_safe_callback)
853 req->r_safe_callback(req, msg);
854 complete(&req->r_safe_completion); /* fsync waiter */
855 }
856
857done:
858 ceph_osdc_put_request(req);
859 return;
860
861bad:
862 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
863 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
864 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -0800865 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -0700866}
867
868
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800869static int __kick_requests(struct ceph_osd_client *osdc,
Sage Weilf24e9982009-10-06 11:31:10 -0700870 struct ceph_osd *kickosd)
871{
872 struct ceph_osd_request *req;
873 struct rb_node *p, *n;
874 int needmap = 0;
875 int err;
876
877 dout("kick_requests osd%d\n", kickosd ? kickosd->o_osd : -1);
Sage Weil153a0082010-02-15 12:11:51 -0800878 if (kickosd) {
Sage Weil87b315a2010-03-22 14:51:18 -0700879 err = __reset_osd(osdc, kickosd);
880 if (err == -EAGAIN)
881 return 1;
Sage Weil153a0082010-02-15 12:11:51 -0800882 } else {
Sage Weilf24e9982009-10-06 11:31:10 -0700883 for (p = rb_first(&osdc->osds); p; p = n) {
884 struct ceph_osd *osd =
885 rb_entry(p, struct ceph_osd, o_node);
886
887 n = rb_next(p);
888 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
Sage Weil103e2d32010-01-07 16:12:36 -0800889 memcmp(&osd->o_con.peer_addr,
890 ceph_osd_addr(osdc->osdmap,
891 osd->o_osd),
892 sizeof(struct ceph_entity_addr)) != 0)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800893 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700894 }
895 }
896
897 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
898 req = rb_entry(p, struct ceph_osd_request, r_node);
899
900 if (req->r_resend) {
901 dout(" r_resend set on tid %llu\n", req->r_tid);
Sage Weil266673d2009-10-09 10:31:32 -0700902 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700903 goto kick;
904 }
Sage Weil266673d2009-10-09 10:31:32 -0700905 if (req->r_osd && kickosd == req->r_osd) {
906 __cancel_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700907 goto kick;
Sage Weil266673d2009-10-09 10:31:32 -0700908 }
Sage Weilf24e9982009-10-06 11:31:10 -0700909
910 err = __map_osds(osdc, req);
911 if (err == 0)
912 continue; /* no change */
913 if (err < 0) {
914 /*
915 * FIXME: really, we should set the request
916 * error and fail if this isn't a 'nofail'
917 * request, but that's a fair bit more
918 * complicated to do. So retry!
919 */
920 dout(" setting r_resend on %llu\n", req->r_tid);
921 req->r_resend = true;
922 continue;
923 }
924 if (req->r_osd == NULL) {
925 dout("tid %llu maps to no valid osd\n", req->r_tid);
926 needmap++; /* request a newer map */
927 continue;
928 }
929
930kick:
Sage Weilc1ea8822009-10-08 16:55:47 -0700931 dout("kicking %p tid %llu osd%d\n", req, req->r_tid,
Sage Weil12eadc12010-03-15 22:20:39 -0700932 req->r_osd ? req->r_osd->o_osd : -1);
Sage Weilf24e9982009-10-06 11:31:10 -0700933 req->r_flags |= CEPH_OSD_FLAG_RETRY;
934 err = __send_request(osdc, req);
935 if (err) {
936 dout(" setting r_resend on %llu\n", req->r_tid);
937 req->r_resend = true;
938 }
939 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800940
941 return needmap;
942}
943
944/*
945 * Resubmit osd requests whose osd or osd address has changed. Request
946 * a new osd map if osds are down, or we are otherwise unable to determine
947 * how to direct a request.
948 *
949 * Close connections to down osds.
950 *
951 * If @who is specified, resubmit requests for that specific osd.
952 *
953 * Caller should hold map_sem for read and request_mutex.
954 */
955static void kick_requests(struct ceph_osd_client *osdc,
956 struct ceph_osd *kickosd)
957{
958 int needmap;
959
960 mutex_lock(&osdc->request_mutex);
961 needmap = __kick_requests(osdc, kickosd);
Sage Weilf24e9982009-10-06 11:31:10 -0700962 mutex_unlock(&osdc->request_mutex);
963
964 if (needmap) {
965 dout("%d requests for down osds, need new map\n", needmap);
966 ceph_monc_request_next_osdmap(&osdc->client->monc);
967 }
Sage Weilf24e9982009-10-06 11:31:10 -0700968
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800969}
Sage Weilf24e9982009-10-06 11:31:10 -0700970/*
971 * Process updated osd map.
972 *
973 * The message contains any number of incremental and full maps, normally
974 * indicating some sort of topology change in the cluster. Kick requests
975 * off to different OSDs as needed.
976 */
977void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
978{
979 void *p, *end, *next;
980 u32 nr_maps, maplen;
981 u32 epoch;
982 struct ceph_osdmap *newmap = NULL, *oldmap;
983 int err;
984 struct ceph_fsid fsid;
985
986 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
987 p = msg->front.iov_base;
988 end = p + msg->front.iov_len;
989
990 /* verify fsid */
991 ceph_decode_need(&p, end, sizeof(fsid), bad);
992 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -0800993 if (ceph_check_fsid(osdc->client, &fsid) < 0)
994 return;
Sage Weilf24e9982009-10-06 11:31:10 -0700995
996 down_write(&osdc->map_sem);
997
998 /* incremental maps */
999 ceph_decode_32_safe(&p, end, nr_maps, bad);
1000 dout(" %d inc maps\n", nr_maps);
1001 while (nr_maps > 0) {
1002 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001003 epoch = ceph_decode_32(&p);
1004 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001005 ceph_decode_need(&p, end, maplen, bad);
1006 next = p + maplen;
1007 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1008 dout("applying incremental map %u len %d\n",
1009 epoch, maplen);
1010 newmap = osdmap_apply_incremental(&p, next,
1011 osdc->osdmap,
1012 osdc->client->msgr);
1013 if (IS_ERR(newmap)) {
1014 err = PTR_ERR(newmap);
1015 goto bad;
1016 }
Sage Weil30dc6382009-12-21 14:49:37 -08001017 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001018 if (newmap != osdc->osdmap) {
1019 ceph_osdmap_destroy(osdc->osdmap);
1020 osdc->osdmap = newmap;
1021 }
1022 } else {
1023 dout("ignoring incremental map %u len %d\n",
1024 epoch, maplen);
1025 }
1026 p = next;
1027 nr_maps--;
1028 }
1029 if (newmap)
1030 goto done;
1031
1032 /* full maps */
1033 ceph_decode_32_safe(&p, end, nr_maps, bad);
1034 dout(" %d full maps\n", nr_maps);
1035 while (nr_maps) {
1036 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001037 epoch = ceph_decode_32(&p);
1038 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001039 ceph_decode_need(&p, end, maplen, bad);
1040 if (nr_maps > 1) {
1041 dout("skipping non-latest full map %u len %d\n",
1042 epoch, maplen);
1043 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1044 dout("skipping full map %u len %d, "
1045 "older than our %u\n", epoch, maplen,
1046 osdc->osdmap->epoch);
1047 } else {
1048 dout("taking full map %u len %d\n", epoch, maplen);
1049 newmap = osdmap_decode(&p, p+maplen);
1050 if (IS_ERR(newmap)) {
1051 err = PTR_ERR(newmap);
1052 goto bad;
1053 }
Sage Weil30dc6382009-12-21 14:49:37 -08001054 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001055 oldmap = osdc->osdmap;
1056 osdc->osdmap = newmap;
1057 if (oldmap)
1058 ceph_osdmap_destroy(oldmap);
1059 }
1060 p += maplen;
1061 nr_maps--;
1062 }
1063
1064done:
1065 downgrade_write(&osdc->map_sem);
1066 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
1067 if (newmap)
1068 kick_requests(osdc, NULL);
1069 up_read(&osdc->map_sem);
1070 return;
1071
1072bad:
1073 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001074 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001075 up_write(&osdc->map_sem);
1076 return;
1077}
1078
1079
1080/*
1081 * A read request prepares specific pages that data is to be read into.
1082 * When a message is being read off the wire, we call prepare_pages to
1083 * find those pages.
1084 * 0 = success, -1 failure.
1085 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001086static int __prepare_pages(struct ceph_connection *con,
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001087 struct ceph_msg_header *hdr,
1088 struct ceph_osd_request *req,
1089 u64 tid,
1090 struct ceph_msg *m)
Sage Weilf24e9982009-10-06 11:31:10 -07001091{
1092 struct ceph_osd *osd = con->private;
1093 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001094 int ret = -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001095 int data_len = le32_to_cpu(hdr->data_len);
1096 unsigned data_off = le16_to_cpu(hdr->data_off);
1097
1098 int want = calc_pages_for(data_off & ~PAGE_MASK, data_len);
Sage Weilf24e9982009-10-06 11:31:10 -07001099
1100 if (!osd)
1101 return -1;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001102
Sage Weilf24e9982009-10-06 11:31:10 -07001103 osdc = osd->o_osdc;
1104
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001105 dout("__prepare_pages on msg %p tid %llu, has %d pages, want %d\n", m,
Sage Weilf24e9982009-10-06 11:31:10 -07001106 tid, req->r_num_pages, want);
Sage Weil350b1c32009-12-22 10:45:45 -08001107 if (unlikely(req->r_num_pages < want))
1108 goto out;
Sage Weil350b1c32009-12-22 10:45:45 -08001109 m->pages = req->r_pages;
1110 m->nr_pages = req->r_num_pages;
1111 ret = 0; /* success */
Sage Weilf24e9982009-10-06 11:31:10 -07001112out:
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001113 BUG_ON(ret < 0 || m->nr_pages < want);
1114
Sage Weilf24e9982009-10-06 11:31:10 -07001115 return ret;
1116}
1117
1118/*
1119 * Register request, send initial attempt.
1120 */
1121int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1122 struct ceph_osd_request *req,
1123 bool nofail)
1124{
Sage Weilc1ea8822009-10-08 16:55:47 -07001125 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001126
1127 req->r_request->pages = req->r_pages;
1128 req->r_request->nr_pages = req->r_num_pages;
1129
1130 register_request(osdc, req);
1131
1132 down_read(&osdc->map_sem);
1133 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001134 /*
1135 * a racing kick_requests() may have sent the message for us
1136 * while we dropped request_mutex above, so only send now if
1137 * the request still han't been touched yet.
1138 */
1139 if (req->r_sent == 0) {
1140 rc = __send_request(osdc, req);
1141 if (rc) {
1142 if (nofail) {
1143 dout("osdc_start_request failed send, "
1144 " marking %lld\n", req->r_tid);
1145 req->r_resend = true;
1146 rc = 0;
1147 } else {
1148 __unregister_request(osdc, req);
1149 }
Sage Weilf24e9982009-10-06 11:31:10 -07001150 }
1151 }
1152 mutex_unlock(&osdc->request_mutex);
1153 up_read(&osdc->map_sem);
1154 return rc;
1155}
1156
1157/*
1158 * wait for a request to complete
1159 */
1160int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1161 struct ceph_osd_request *req)
1162{
1163 int rc;
1164
1165 rc = wait_for_completion_interruptible(&req->r_completion);
1166 if (rc < 0) {
1167 mutex_lock(&osdc->request_mutex);
1168 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001169 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001170 mutex_unlock(&osdc->request_mutex);
Sage Weil529cfcc2009-12-22 10:29:39 -08001171 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001172 return rc;
1173 }
1174
1175 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1176 return req->r_result;
1177}
1178
1179/*
1180 * sync - wait for all in-flight requests to flush. avoid starvation.
1181 */
1182void ceph_osdc_sync(struct ceph_osd_client *osdc)
1183{
1184 struct ceph_osd_request *req;
1185 u64 last_tid, next_tid = 0;
1186
1187 mutex_lock(&osdc->request_mutex);
1188 last_tid = osdc->last_tid;
1189 while (1) {
1190 req = __lookup_request_ge(osdc, next_tid);
1191 if (!req)
1192 break;
1193 if (req->r_tid > last_tid)
1194 break;
1195
1196 next_tid = req->r_tid + 1;
1197 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1198 continue;
1199
1200 ceph_osdc_get_request(req);
1201 mutex_unlock(&osdc->request_mutex);
1202 dout("sync waiting on tid %llu (last is %llu)\n",
1203 req->r_tid, last_tid);
1204 wait_for_completion(&req->r_safe_completion);
1205 mutex_lock(&osdc->request_mutex);
1206 ceph_osdc_put_request(req);
1207 }
1208 mutex_unlock(&osdc->request_mutex);
1209 dout("sync done (thru tid %llu)\n", last_tid);
1210}
1211
1212/*
1213 * init, shutdown
1214 */
1215int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1216{
1217 int err;
1218
1219 dout("init\n");
1220 osdc->client = client;
1221 osdc->osdmap = NULL;
1222 init_rwsem(&osdc->map_sem);
1223 init_completion(&osdc->map_waiters);
1224 osdc->last_requested_map = 0;
1225 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001226 osdc->last_tid = 0;
1227 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001228 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001229 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001230 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001231 osdc->num_requests = 0;
1232 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001233 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
1234
1235 schedule_delayed_work(&osdc->osds_timeout_work,
1236 round_jiffies_relative(osdc->client->mount_args->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001237
Sage Weil5f44f142009-11-18 14:52:18 -08001238 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001239 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1240 sizeof(struct ceph_osd_request));
1241 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001242 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001243
Sage Weilc16e7862010-03-01 13:02:00 -08001244 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001245 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001246 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001247 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
1248 OSD_OPREPLY_FRONT_LEN, 10, true);
1249 if (err < 0)
1250 goto out_msgpool;
Sage Weilf24e9982009-10-06 11:31:10 -07001251 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001252
Sage Weilc16e7862010-03-01 13:02:00 -08001253out_msgpool:
1254 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001255out_mempool:
1256 mempool_destroy(osdc->req_mempool);
1257out:
1258 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001259}
1260
1261void ceph_osdc_stop(struct ceph_osd_client *osdc)
1262{
1263 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001264 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001265 if (osdc->osdmap) {
1266 ceph_osdmap_destroy(osdc->osdmap);
1267 osdc->osdmap = NULL;
1268 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001269 remove_old_osds(osdc, 1);
Sage Weilf24e9982009-10-06 11:31:10 -07001270 mempool_destroy(osdc->req_mempool);
1271 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001272 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001273}
1274
1275/*
1276 * Read some contiguous pages. If we cross a stripe boundary, shorten
1277 * *plen. Return number of bytes read, or error.
1278 */
1279int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1280 struct ceph_vino vino, struct ceph_file_layout *layout,
1281 u64 off, u64 *plen,
1282 u32 truncate_seq, u64 truncate_size,
1283 struct page **pages, int num_pages)
1284{
1285 struct ceph_osd_request *req;
1286 int rc = 0;
1287
1288 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1289 vino.snap, off, *plen);
1290 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1291 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1292 NULL, 0, truncate_seq, truncate_size, NULL,
1293 false, 1);
1294 if (IS_ERR(req))
1295 return PTR_ERR(req);
1296
1297 /* it may be a short read due to an object boundary */
1298 req->r_pages = pages;
1299 num_pages = calc_pages_for(off, *plen);
1300 req->r_num_pages = num_pages;
1301
1302 dout("readpages final extent is %llu~%llu (%d pages)\n",
1303 off, *plen, req->r_num_pages);
1304
1305 rc = ceph_osdc_start_request(osdc, req, false);
1306 if (!rc)
1307 rc = ceph_osdc_wait_request(osdc, req);
1308
1309 ceph_osdc_put_request(req);
1310 dout("readpages result %d\n", rc);
1311 return rc;
1312}
1313
1314/*
1315 * do a synchronous write on N pages
1316 */
1317int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1318 struct ceph_file_layout *layout,
1319 struct ceph_snap_context *snapc,
1320 u64 off, u64 len,
1321 u32 truncate_seq, u64 truncate_size,
1322 struct timespec *mtime,
1323 struct page **pages, int num_pages,
1324 int flags, int do_sync, bool nofail)
1325{
1326 struct ceph_osd_request *req;
1327 int rc = 0;
1328
1329 BUG_ON(vino.snap != CEPH_NOSNAP);
1330 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1331 CEPH_OSD_OP_WRITE,
1332 flags | CEPH_OSD_FLAG_ONDISK |
1333 CEPH_OSD_FLAG_WRITE,
1334 snapc, do_sync,
1335 truncate_seq, truncate_size, mtime,
1336 nofail, 1);
1337 if (IS_ERR(req))
1338 return PTR_ERR(req);
1339
1340 /* it may be a short write due to an object boundary */
1341 req->r_pages = pages;
1342 req->r_num_pages = calc_pages_for(off, len);
1343 dout("writepages %llu~%llu (%d pages)\n", off, len,
1344 req->r_num_pages);
1345
1346 rc = ceph_osdc_start_request(osdc, req, nofail);
1347 if (!rc)
1348 rc = ceph_osdc_wait_request(osdc, req);
1349
1350 ceph_osdc_put_request(req);
1351 if (rc == 0)
1352 rc = len;
1353 dout("writepages result %d\n", rc);
1354 return rc;
1355}
1356
1357/*
1358 * handle incoming message
1359 */
1360static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1361{
1362 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001363 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001364 int type = le16_to_cpu(msg->hdr.type);
1365
1366 if (!osd)
1367 return;
Julia Lawall32c895e2009-11-21 16:53:16 +01001368 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001369
1370 switch (type) {
1371 case CEPH_MSG_OSD_MAP:
1372 ceph_osdc_handle_map(osdc, msg);
1373 break;
1374 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001375 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001376 break;
1377
1378 default:
1379 pr_err("received unknown message type %d %s\n", type,
1380 ceph_msg_type_name(type));
1381 }
1382 ceph_msg_put(msg);
1383}
1384
Sage Weil5b3a4db2010-02-19 21:43:23 -08001385/*
1386 * lookup and return message for incoming reply
1387 */
1388static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001389 struct ceph_msg_header *hdr,
1390 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001391{
1392 struct ceph_osd *osd = con->private;
1393 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001394 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001395 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001396 int front = le32_to_cpu(hdr->front_len);
1397 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001398 u64 tid;
1399 int err;
Sage Weilf24e9982009-10-06 11:31:10 -07001400
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001401 tid = le64_to_cpu(hdr->tid);
1402 mutex_lock(&osdc->request_mutex);
1403 req = __lookup_request(osdc, tid);
1404 if (!req) {
1405 *skip = 1;
1406 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08001407 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08001408 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001409 goto out;
1410 }
Sage Weilc16e7862010-03-01 13:02:00 -08001411
1412 if (req->r_con_filling_msg) {
1413 dout("get_reply revoking msg %p from old con %p\n",
1414 req->r_reply, req->r_con_filling_msg);
1415 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
1416 ceph_con_put(req->r_con_filling_msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001417 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001418
Sage Weilc16e7862010-03-01 13:02:00 -08001419 if (front > req->r_reply->front.iov_len) {
1420 pr_warning("get_reply front %d > preallocated %d\n",
1421 front, (int)req->r_reply->front.iov_len);
1422 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, 0, 0, NULL);
1423 if (IS_ERR(m))
1424 goto out;
1425 ceph_msg_put(req->r_reply);
1426 req->r_reply = m;
1427 }
1428 m = ceph_msg_get(req->r_reply);
1429
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001430 if (data_len > 0) {
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001431 err = __prepare_pages(con, hdr, req, tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001432 if (err < 0) {
1433 *skip = 1;
1434 ceph_msg_put(m);
1435 m = ERR_PTR(err);
1436 }
1437 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08001438 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08001439 req->r_con_filling_msg = ceph_con_get(con);
1440 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001441
1442out:
1443 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08001444 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001445
1446}
1447
1448static struct ceph_msg *alloc_msg(struct ceph_connection *con,
1449 struct ceph_msg_header *hdr,
1450 int *skip)
1451{
1452 struct ceph_osd *osd = con->private;
1453 int type = le16_to_cpu(hdr->type);
1454 int front = le32_to_cpu(hdr->front_len);
1455
1456 switch (type) {
1457 case CEPH_MSG_OSD_MAP:
1458 return ceph_msg_new(type, front, 0, 0, NULL);
1459 case CEPH_MSG_OSD_OPREPLY:
1460 return get_reply(con, hdr, skip);
1461 default:
1462 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
1463 osd->o_osd);
1464 *skip = 1;
1465 return NULL;
1466 }
Sage Weilf24e9982009-10-06 11:31:10 -07001467}
1468
1469/*
1470 * Wrappers to refcount containing ceph_osd struct
1471 */
1472static struct ceph_connection *get_osd_con(struct ceph_connection *con)
1473{
1474 struct ceph_osd *osd = con->private;
1475 if (get_osd(osd))
1476 return con;
1477 return NULL;
1478}
1479
1480static void put_osd_con(struct ceph_connection *con)
1481{
1482 struct ceph_osd *osd = con->private;
1483 put_osd(osd);
1484}
1485
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001486/*
1487 * authentication
1488 */
1489static int get_authorizer(struct ceph_connection *con,
Sage Weil50b885b2009-12-01 14:12:07 -08001490 void **buf, int *len, int *proto,
1491 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001492{
1493 struct ceph_osd *o = con->private;
1494 struct ceph_osd_client *osdc = o->o_osdc;
1495 struct ceph_auth_client *ac = osdc->client->monc.auth;
1496 int ret = 0;
1497
1498 if (force_new && o->o_authorizer) {
1499 ac->ops->destroy_authorizer(ac, o->o_authorizer);
1500 o->o_authorizer = NULL;
1501 }
1502 if (o->o_authorizer == NULL) {
1503 ret = ac->ops->create_authorizer(
1504 ac, CEPH_ENTITY_TYPE_OSD,
1505 &o->o_authorizer,
1506 &o->o_authorizer_buf,
1507 &o->o_authorizer_buf_len,
1508 &o->o_authorizer_reply_buf,
1509 &o->o_authorizer_reply_buf_len);
1510 if (ret)
1511 return ret;
1512 }
1513
1514 *proto = ac->protocol;
1515 *buf = o->o_authorizer_buf;
1516 *len = o->o_authorizer_buf_len;
1517 *reply_buf = o->o_authorizer_reply_buf;
1518 *reply_len = o->o_authorizer_reply_buf_len;
1519 return 0;
1520}
1521
1522
1523static int verify_authorizer_reply(struct ceph_connection *con, int len)
1524{
1525 struct ceph_osd *o = con->private;
1526 struct ceph_osd_client *osdc = o->o_osdc;
1527 struct ceph_auth_client *ac = osdc->client->monc.auth;
1528
1529 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
1530}
1531
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001532static int invalidate_authorizer(struct ceph_connection *con)
1533{
1534 struct ceph_osd *o = con->private;
1535 struct ceph_osd_client *osdc = o->o_osdc;
1536 struct ceph_auth_client *ac = osdc->client->monc.auth;
1537
1538 if (ac->ops->invalidate_authorizer)
1539 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
1540
1541 return ceph_monc_validate_auth(&osdc->client->monc);
1542}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001543
Sage Weilf24e9982009-10-06 11:31:10 -07001544const static struct ceph_connection_operations osd_con_ops = {
1545 .get = get_osd_con,
1546 .put = put_osd_con,
1547 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08001548 .get_authorizer = get_authorizer,
1549 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08001550 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07001551 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07001552 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07001553};