blob: 16836a7df7a6364c5f6823975d559ff3b30106a2 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07004#include <linux/err.h>
5#include <linux/highmem.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/slab.h>
9#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070010#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
Sage Weilf24e9982009-10-06 11:31:10 -070013
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include <linux/ceph/libceph.h>
15#include <linux/ceph/osd_client.h>
16#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/auth.h>
19#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070020
Sage Weilc16e7862010-03-01 13:02:00 -080021#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Tobias Klauser9e327892010-05-20 10:40:19 +020024static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070025
Sage Weil6f6c7002011-01-17 20:34:08 -080026static void send_queued(struct ceph_osd_client *osdc);
27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070028static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
32static int __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070034
Yehuda Sadeh68b44762010-04-06 15:01:27 -070035static int op_needs_trail(int op)
36{
37 switch (op) {
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070042 case CEPH_OSD_OP_NOTIFY:
Yehuda Sadeh68b44762010-04-06 15:01:27 -070043 return 1;
44 default:
45 return 0;
46 }
47}
48
49static int op_has_extent(int op)
50{
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
53}
54
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070055void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
57 u64 snapid,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070058 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070061{
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Yehuda Sadeh68b44762010-04-06 15:01:27 -070063 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070064 u64 objoff, objlen; /* extent in object */
65
66 reqhead->snapid = cpu_to_le64(snapid);
67
68 /* object extent? */
Yehuda Sadeh68b44762010-04-06 15:01:27 -070069 ceph_calc_file_object_mapping(layout, off, plen, bno,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070070 &objoff, &objlen);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070071 if (*plen < orig_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070072 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070073 orig_len - *plen, off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070074
Yehuda Sadeh68b44762010-04-06 15:01:27 -070075 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
78 }
79 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080080 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070081 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070083
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
86
87}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070088EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070089
Sage Weilf24e9982009-10-06 11:31:10 -070090/*
91 * Implement client access to distributed object storage cluster.
92 *
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
97 * access to storage.
98 *
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
101 *
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
106 */
107
108/*
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
111 * object boundary.
112 *
113 * fill osd op in request message.
114 */
115static void calc_layout(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
Sage Weilf24e9982009-10-06 11:31:10 -0700118 u64 off, u64 *plen,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700121{
Sage Weilf24e9982009-10-06 11:31:10 -0700122 u64 bno;
123
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
Sage Weilf24e9982009-10-06 11:31:10 -0700126
Sage Weil2dab0362011-05-12 14:29:51 -0700127 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
Sage Weilf24e9982009-10-06 11:31:10 -0700128 req->r_oid_len = strlen(req->r_oid);
Sage Weilf24e9982009-10-06 11:31:10 -0700129}
130
Sage Weilf24e9982009-10-06 11:31:10 -0700131/*
132 * requests
133 */
Sage Weil415e49a2009-12-07 13:37:03 -0800134void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700135{
Sage Weil415e49a2009-12-07 13:37:03 -0800136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
138 r_kref);
139
140 if (req->r_request)
141 ceph_msg_put(req->r_request);
142 if (req->r_reply)
143 ceph_msg_put(req->r_reply);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800144 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -0800145 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800146 req->r_pages, req->r_con_filling_msg);
147 ceph_con_revoke_message(req->r_con_filling_msg,
148 req->r_reply);
149 ceph_con_put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800150 }
Sage Weil415e49a2009-12-07 13:37:03 -0800151 if (req->r_own_pages)
152 ceph_release_page_vector(req->r_pages,
153 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700154#ifdef CONFIG_BLOCK
155 if (req->r_bio)
156 bio_put(req->r_bio);
157#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800158 ceph_put_snap_context(req->r_snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700159 if (req->r_trail) {
160 ceph_pagelist_release(req->r_trail);
161 kfree(req->r_trail);
162 }
Sage Weil415e49a2009-12-07 13:37:03 -0800163 if (req->r_mempool)
164 mempool_free(req, req->r_osdc->req_mempool);
165 else
166 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700167}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700168EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700169
170static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
171{
172 int i = 0;
173
174 if (needs_trail)
175 *needs_trail = 0;
176 while (ops[i].op) {
177 if (needs_trail && op_needs_trail(ops[i].op))
178 *needs_trail = 1;
179 i++;
180 }
181
182 return i;
183}
184
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700185struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
186 int flags,
187 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700188 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700189 bool use_mempool,
190 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700191 struct page **pages,
192 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700193{
194 struct ceph_osd_request *req;
195 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700196 int needs_trail;
197 int num_op = get_num_ops(ops, &needs_trail);
198 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700199
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700200 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700201
202 if (use_mempool) {
203 req = mempool_alloc(osdc->req_mempool, gfp_flags);
204 memset(req, 0, sizeof(*req));
205 } else {
206 req = kzalloc(sizeof(*req), gfp_flags);
207 }
208 if (req == NULL)
209 return NULL;
210
211 req->r_osdc = osdc;
212 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700213
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700214 kref_init(&req->r_kref);
215 init_completion(&req->r_completion);
216 init_completion(&req->r_safe_completion);
217 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700218 INIT_LIST_HEAD(&req->r_linger_item);
219 INIT_LIST_HEAD(&req->r_linger_osd);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700220 req->r_flags = flags;
221
222 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
223
224 /* create reply message */
225 if (use_mempool)
226 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
227 else
228 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
229 OSD_OPREPLY_FRONT_LEN, gfp_flags);
230 if (!msg) {
231 ceph_osdc_put_request(req);
232 return NULL;
233 }
234 req->r_reply = msg;
235
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700236 /* allocate space for the trailing data */
237 if (needs_trail) {
238 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
239 if (!req->r_trail) {
240 ceph_osdc_put_request(req);
241 return NULL;
242 }
243 ceph_pagelist_init(req->r_trail);
244 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700245 /* create request message; allow space for oid */
246 msg_size += 40;
247 if (snapc)
248 msg_size += sizeof(u64) * snapc->num_snaps;
249 if (use_mempool)
250 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
251 else
252 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags);
253 if (!msg) {
254 ceph_osdc_put_request(req);
255 return NULL;
256 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700257
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700258 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
259 memset(msg->front.iov_base, 0, msg->front.iov_len);
260
261 req->r_request = msg;
262 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700263#ifdef CONFIG_BLOCK
264 if (bio) {
265 req->r_bio = bio;
266 bio_get(req->r_bio);
267 }
268#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700269
270 return req;
271}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700272EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700273
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700274static void osd_req_encode_op(struct ceph_osd_request *req,
275 struct ceph_osd_op *dst,
276 struct ceph_osd_req_op *src)
277{
278 dst->op = cpu_to_le16(src->op);
279
280 switch (dst->op) {
281 case CEPH_OSD_OP_READ:
282 case CEPH_OSD_OP_WRITE:
283 dst->extent.offset =
284 cpu_to_le64(src->extent.offset);
285 dst->extent.length =
286 cpu_to_le64(src->extent.length);
287 dst->extent.truncate_size =
288 cpu_to_le64(src->extent.truncate_size);
289 dst->extent.truncate_seq =
290 cpu_to_le32(src->extent.truncate_seq);
291 break;
292
293 case CEPH_OSD_OP_GETXATTR:
294 case CEPH_OSD_OP_SETXATTR:
295 case CEPH_OSD_OP_CMPXATTR:
296 BUG_ON(!req->r_trail);
297
298 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
299 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
300 dst->xattr.cmp_op = src->xattr.cmp_op;
301 dst->xattr.cmp_mode = src->xattr.cmp_mode;
302 ceph_pagelist_append(req->r_trail, src->xattr.name,
303 src->xattr.name_len);
304 ceph_pagelist_append(req->r_trail, src->xattr.val,
305 src->xattr.value_len);
306 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700307 case CEPH_OSD_OP_CALL:
308 BUG_ON(!req->r_trail);
309
310 dst->cls.class_len = src->cls.class_len;
311 dst->cls.method_len = src->cls.method_len;
312 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
313
314 ceph_pagelist_append(req->r_trail, src->cls.class_name,
315 src->cls.class_len);
316 ceph_pagelist_append(req->r_trail, src->cls.method_name,
317 src->cls.method_len);
318 ceph_pagelist_append(req->r_trail, src->cls.indata,
319 src->cls.indata_len);
320 break;
321 case CEPH_OSD_OP_ROLLBACK:
322 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
323 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700324 case CEPH_OSD_OP_STARTSYNC:
325 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700326 case CEPH_OSD_OP_NOTIFY:
327 {
328 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
329 __le32 timeout = cpu_to_le32(src->watch.timeout);
330
331 BUG_ON(!req->r_trail);
332
333 ceph_pagelist_append(req->r_trail,
334 &prot_ver, sizeof(prot_ver));
335 ceph_pagelist_append(req->r_trail,
336 &timeout, sizeof(timeout));
337 }
338 case CEPH_OSD_OP_NOTIFY_ACK:
339 case CEPH_OSD_OP_WATCH:
340 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
341 dst->watch.ver = cpu_to_le64(src->watch.ver);
342 dst->watch.flag = src->watch.flag;
343 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700344 default:
345 pr_err("unrecognized osd opcode %d\n", dst->op);
346 WARN_ON(1);
347 break;
348 }
349 dst->payload_len = cpu_to_le32(src->payload_len);
350}
351
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700352/*
353 * build new request AND message
354 *
355 */
356void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700357 u64 off, u64 *plen,
358 struct ceph_osd_req_op *src_ops,
359 struct ceph_snap_context *snapc,
360 struct timespec *mtime,
361 const char *oid,
362 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363{
364 struct ceph_msg *msg = req->r_request;
365 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700366 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367 struct ceph_osd_op *op;
368 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700369 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700370 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700371 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700372 u64 data_len = 0;
373 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
375 head = msg->front.iov_base;
376 op = (void *)(head + 1);
377 p = (void *)(op + num_op);
378
379 req->r_snapc = ceph_get_snap_context(snapc);
380
381 head->client_inc = cpu_to_le32(1); /* always, for now. */
382 head->flags = cpu_to_le32(flags);
383 if (flags & CEPH_OSD_FLAG_WRITE)
384 ceph_encode_timespec(&head->mtime, mtime);
385 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700386
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387
388 /* fill in oid */
389 head->object_len = cpu_to_le32(oid_len);
390 memcpy(p, oid, oid_len);
391 p += oid_len;
392
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700393 src_op = src_ops;
394 while (src_op->op) {
395 osd_req_encode_op(req, op, src_op);
396 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700398 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700399
400 if (req->r_trail)
401 data_len += req->r_trail->length;
402
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700403 if (snapc) {
404 head->snap_seq = cpu_to_le64(snapc->seq);
405 head->num_snaps = cpu_to_le32(snapc->num_snaps);
406 for (i = 0; i < snapc->num_snaps; i++) {
407 put_unaligned_le64(snapc->snaps[i], p);
408 p += sizeof(u64);
409 }
410 }
411
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700412 if (flags & CEPH_OSD_FLAG_WRITE) {
413 req->r_request->hdr.data_off = cpu_to_le16(off);
414 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
415 } else if (data_len) {
416 req->r_request->hdr.data_off = 0;
417 req->r_request->hdr.data_len = cpu_to_le32(data_len);
418 }
419
Sage Weilc5c6b192010-11-09 12:40:00 -0800420 req->r_request->page_alignment = req->r_page_alignment;
421
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700422 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
423 msg_size = p - msg->front.iov_base;
424 msg->front.iov_len = msg_size;
425 msg->hdr.front_len = cpu_to_le32(msg_size);
426 return;
427}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700428EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700429
Sage Weilf24e9982009-10-06 11:31:10 -0700430/*
431 * build new request AND message, calculate layout, and adjust file
432 * extent as needed.
433 *
434 * if the file was recently truncated, we include information about its
435 * old and new size so that the object can be updated appropriately. (we
436 * avoid synchronously deleting truncated objects because it's slow.)
437 *
438 * if @do_sync, include a 'startsync' command so that the osd will flush
439 * data quickly.
440 */
441struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
442 struct ceph_file_layout *layout,
443 struct ceph_vino vino,
444 u64 off, u64 *plen,
445 int opcode, int flags,
446 struct ceph_snap_context *snapc,
447 int do_sync,
448 u32 truncate_seq,
449 u64 truncate_size,
450 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800451 bool use_mempool, int num_reply,
452 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700453{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700454 struct ceph_osd_req_op ops[3];
455 struct ceph_osd_request *req;
456
457 ops[0].op = opcode;
458 ops[0].extent.truncate_seq = truncate_seq;
459 ops[0].extent.truncate_size = truncate_size;
460 ops[0].payload_len = 0;
461
462 if (do_sync) {
463 ops[1].op = CEPH_OSD_OP_STARTSYNC;
464 ops[1].payload_len = 0;
465 ops[2].op = 0;
466 } else
467 ops[1].op = 0;
468
469 req = ceph_osdc_alloc_request(osdc, flags,
470 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700471 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700472 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700473 if (!req)
474 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700475
476 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700477 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700478 req->r_file_layout = *layout; /* keep a copy */
479
Sage Weil9bb0ce22011-06-13 16:20:18 -0700480 /* in case it differs from natural (file) alignment that
481 calc_layout filled in for us */
482 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800483 req->r_page_alignment = page_align;
484
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700485 ceph_osdc_build_request(req, off, plen, ops,
486 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700487 mtime,
488 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700489
Sage Weilf24e9982009-10-06 11:31:10 -0700490 return req;
491}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700492EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700493
494/*
495 * We keep osd requests in an rbtree, sorted by ->r_tid.
496 */
497static void __insert_request(struct ceph_osd_client *osdc,
498 struct ceph_osd_request *new)
499{
500 struct rb_node **p = &osdc->requests.rb_node;
501 struct rb_node *parent = NULL;
502 struct ceph_osd_request *req = NULL;
503
504 while (*p) {
505 parent = *p;
506 req = rb_entry(parent, struct ceph_osd_request, r_node);
507 if (new->r_tid < req->r_tid)
508 p = &(*p)->rb_left;
509 else if (new->r_tid > req->r_tid)
510 p = &(*p)->rb_right;
511 else
512 BUG();
513 }
514
515 rb_link_node(&new->r_node, parent, p);
516 rb_insert_color(&new->r_node, &osdc->requests);
517}
518
519static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
520 u64 tid)
521{
522 struct ceph_osd_request *req;
523 struct rb_node *n = osdc->requests.rb_node;
524
525 while (n) {
526 req = rb_entry(n, struct ceph_osd_request, r_node);
527 if (tid < req->r_tid)
528 n = n->rb_left;
529 else if (tid > req->r_tid)
530 n = n->rb_right;
531 else
532 return req;
533 }
534 return NULL;
535}
536
537static struct ceph_osd_request *
538__lookup_request_ge(struct ceph_osd_client *osdc,
539 u64 tid)
540{
541 struct ceph_osd_request *req;
542 struct rb_node *n = osdc->requests.rb_node;
543
544 while (n) {
545 req = rb_entry(n, struct ceph_osd_request, r_node);
546 if (tid < req->r_tid) {
547 if (!n->rb_left)
548 return req;
549 n = n->rb_left;
550 } else if (tid > req->r_tid) {
551 n = n->rb_right;
552 } else {
553 return req;
554 }
555 }
556 return NULL;
557}
558
Sage Weil6f6c7002011-01-17 20:34:08 -0800559/*
560 * Resubmit requests pending on the given osd.
561 */
562static void __kick_osd_requests(struct ceph_osd_client *osdc,
563 struct ceph_osd *osd)
564{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700565 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800566 int err;
567
568 dout("__kick_osd_requests osd%d\n", osd->o_osd);
569 err = __reset_osd(osdc, osd);
570 if (err == -EAGAIN)
571 return;
572
573 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
574 list_move(&req->r_req_lru_item, &osdc->req_unsent);
575 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
576 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700577 if (!req->r_linger)
578 req->r_flags |= CEPH_OSD_FLAG_RETRY;
579 }
580
581 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
582 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700583 /*
584 * reregister request prior to unregistering linger so
585 * that r_osd is preserved.
586 */
587 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700588 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700589 list_add(&req->r_req_lru_item, &osdc->req_unsent);
590 list_add(&req->r_osd_item, &req->r_osd->o_requests);
591 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700592 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
593 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800594 }
595}
596
597static void kick_osd_requests(struct ceph_osd_client *osdc,
598 struct ceph_osd *kickosd)
599{
600 mutex_lock(&osdc->request_mutex);
601 __kick_osd_requests(osdc, kickosd);
602 mutex_unlock(&osdc->request_mutex);
603}
Sage Weilf24e9982009-10-06 11:31:10 -0700604
605/*
Sage Weil81b024e2009-10-09 10:29:18 -0700606 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700607 */
608static void osd_reset(struct ceph_connection *con)
609{
610 struct ceph_osd *osd = con->private;
611 struct ceph_osd_client *osdc;
612
613 if (!osd)
614 return;
615 dout("osd_reset osd%d\n", osd->o_osd);
616 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700617 down_read(&osdc->map_sem);
Sage Weil6f6c7002011-01-17 20:34:08 -0800618 kick_osd_requests(osdc, osd);
619 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700620 up_read(&osdc->map_sem);
621}
622
623/*
624 * Track open sessions with osds.
625 */
626static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
627{
628 struct ceph_osd *osd;
629
630 osd = kzalloc(sizeof(*osd), GFP_NOFS);
631 if (!osd)
632 return NULL;
633
634 atomic_set(&osd->o_ref, 1);
635 osd->o_osdc = osdc;
636 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700637 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800638 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700639 osd->o_incarnation = 1;
640
641 ceph_con_init(osdc->client->msgr, &osd->o_con);
642 osd->o_con.private = osd;
643 osd->o_con.ops = &osd_con_ops;
644 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800645
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800646 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700647 return osd;
648}
649
650static struct ceph_osd *get_osd(struct ceph_osd *osd)
651{
652 if (atomic_inc_not_zero(&osd->o_ref)) {
653 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
654 atomic_read(&osd->o_ref));
655 return osd;
656 } else {
657 dout("get_osd %p FAIL\n", osd);
658 return NULL;
659 }
660}
661
662static void put_osd(struct ceph_osd *osd)
663{
664 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
665 atomic_read(&osd->o_ref) - 1);
Sage Weil79494d12010-05-27 14:15:49 -0700666 if (atomic_dec_and_test(&osd->o_ref)) {
667 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
668
669 if (osd->o_authorizer)
670 ac->ops->destroy_authorizer(ac, osd->o_authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700671 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700672 }
Sage Weilf24e9982009-10-06 11:31:10 -0700673}
674
675/*
676 * remove an osd from our map
677 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800678static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700679{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800680 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700681 BUG_ON(!list_empty(&osd->o_requests));
682 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800683 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700684 ceph_con_close(&osd->o_con);
685 put_osd(osd);
686}
687
Sage Weilaca420b2011-08-31 14:45:53 -0700688static void remove_all_osds(struct ceph_osd_client *osdc)
689{
690 dout("__remove_old_osds %p\n", osdc);
691 mutex_lock(&osdc->request_mutex);
692 while (!RB_EMPTY_ROOT(&osdc->osds)) {
693 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
694 struct ceph_osd, o_node);
695 __remove_osd(osdc, osd);
696 }
697 mutex_unlock(&osdc->request_mutex);
698}
699
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800700static void __move_osd_to_lru(struct ceph_osd_client *osdc,
701 struct ceph_osd *osd)
702{
703 dout("__move_osd_to_lru %p\n", osd);
704 BUG_ON(!list_empty(&osd->o_osd_lru));
705 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700706 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800707}
708
709static void __remove_osd_from_lru(struct ceph_osd *osd)
710{
711 dout("__remove_osd_from_lru %p\n", osd);
712 if (!list_empty(&osd->o_osd_lru))
713 list_del_init(&osd->o_osd_lru);
714}
715
Sage Weilaca420b2011-08-31 14:45:53 -0700716static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800717{
718 struct ceph_osd *osd, *nosd;
719
720 dout("__remove_old_osds %p\n", osdc);
721 mutex_lock(&osdc->request_mutex);
722 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700723 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800724 break;
725 __remove_osd(osdc, osd);
726 }
727 mutex_unlock(&osdc->request_mutex);
728}
729
Sage Weilf24e9982009-10-06 11:31:10 -0700730/*
731 * reset osd connect
732 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800733static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700734{
Sage Weil87b315a2010-03-22 14:51:18 -0700735 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700736 int ret = 0;
737
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800738 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700739 if (list_empty(&osd->o_requests) &&
740 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800741 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700742 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
743 &osd->o_con.peer_addr,
744 sizeof(osd->o_con.peer_addr)) == 0 &&
745 !ceph_con_opened(&osd->o_con)) {
746 dout(" osd addr hasn't changed and connection never opened,"
747 " letting msgr retry");
748 /* touch each r_stamp for handle_timeout()'s benfit */
749 list_for_each_entry(req, &osd->o_requests, r_osd_item)
750 req->r_stamp = jiffies;
751 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700752 } else {
753 ceph_con_close(&osd->o_con);
754 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
755 osd->o_incarnation++;
756 }
757 return ret;
758}
759
760static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
761{
762 struct rb_node **p = &osdc->osds.rb_node;
763 struct rb_node *parent = NULL;
764 struct ceph_osd *osd = NULL;
765
Sage Weilaca420b2011-08-31 14:45:53 -0700766 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700767 while (*p) {
768 parent = *p;
769 osd = rb_entry(parent, struct ceph_osd, o_node);
770 if (new->o_osd < osd->o_osd)
771 p = &(*p)->rb_left;
772 else if (new->o_osd > osd->o_osd)
773 p = &(*p)->rb_right;
774 else
775 BUG();
776 }
777
778 rb_link_node(&new->o_node, parent, p);
779 rb_insert_color(&new->o_node, &osdc->osds);
780}
781
782static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
783{
784 struct ceph_osd *osd;
785 struct rb_node *n = osdc->osds.rb_node;
786
787 while (n) {
788 osd = rb_entry(n, struct ceph_osd, o_node);
789 if (o < osd->o_osd)
790 n = n->rb_left;
791 else if (o > osd->o_osd)
792 n = n->rb_right;
793 else
794 return osd;
795 }
796 return NULL;
797}
798
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800799static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
800{
801 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700802 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800803}
804
805static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
806{
807 cancel_delayed_work(&osdc->timeout_work);
808}
Sage Weilf24e9982009-10-06 11:31:10 -0700809
810/*
811 * Register request, assign tid. If this is the first request, set up
812 * the timeout event.
813 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700814static void __register_request(struct ceph_osd_client *osdc,
815 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700816{
Sage Weilf24e9982009-10-06 11:31:10 -0700817 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800818 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800819 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700820
Sage Weil77f38e02011-04-06 09:09:16 -0700821 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700822 __insert_request(osdc, req);
823 ceph_osdc_get_request(req);
824 osdc->num_requests++;
825
Sage Weilf24e9982009-10-06 11:31:10 -0700826 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800827 dout(" first request, scheduling timeout\n");
828 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700829 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700830}
831
832static void register_request(struct ceph_osd_client *osdc,
833 struct ceph_osd_request *req)
834{
835 mutex_lock(&osdc->request_mutex);
836 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700837 mutex_unlock(&osdc->request_mutex);
838}
839
840/*
841 * called under osdc->request_mutex
842 */
843static void __unregister_request(struct ceph_osd_client *osdc,
844 struct ceph_osd_request *req)
845{
846 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
847 rb_erase(&req->r_node, &osdc->requests);
848 osdc->num_requests--;
849
Sage Weil0ba64782009-10-08 16:57:16 -0700850 if (req->r_osd) {
851 /* make sure the original request isn't in flight. */
852 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
853
854 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700855 if (list_empty(&req->r_osd->o_requests) &&
856 list_empty(&req->r_osd->o_linger_requests)) {
857 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800858 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700859 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700860 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700861 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700862 }
Sage Weilf24e9982009-10-06 11:31:10 -0700863
864 ceph_osdc_put_request(req);
865
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800866 list_del_init(&req->r_req_lru_item);
867 if (osdc->num_requests == 0) {
868 dout(" no requests, canceling timeout\n");
869 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700870 }
871}
872
873/*
874 * Cancel a previously queued request message
875 */
876static void __cancel_request(struct ceph_osd_request *req)
877{
Sage Weil6bc18872010-09-27 10:18:52 -0700878 if (req->r_sent && req->r_osd) {
Sage Weilf24e9982009-10-06 11:31:10 -0700879 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
880 req->r_sent = 0;
881 }
882}
883
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700884static void __register_linger_request(struct ceph_osd_client *osdc,
885 struct ceph_osd_request *req)
886{
887 dout("__register_linger_request %p\n", req);
888 list_add_tail(&req->r_linger_item, &osdc->req_linger);
889 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
890}
891
892static void __unregister_linger_request(struct ceph_osd_client *osdc,
893 struct ceph_osd_request *req)
894{
895 dout("__unregister_linger_request %p\n", req);
896 if (req->r_osd) {
897 list_del_init(&req->r_linger_item);
898 list_del_init(&req->r_linger_osd);
899
900 if (list_empty(&req->r_osd->o_requests) &&
901 list_empty(&req->r_osd->o_linger_requests)) {
902 dout("moving osd to %p lru\n", req->r_osd);
903 __move_osd_to_lru(osdc, req->r_osd);
904 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700905 if (list_empty(&req->r_osd_item))
906 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700907 }
908}
909
910void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
911 struct ceph_osd_request *req)
912{
913 mutex_lock(&osdc->request_mutex);
914 if (req->r_linger) {
915 __unregister_linger_request(osdc, req);
916 ceph_osdc_put_request(req);
917 }
918 mutex_unlock(&osdc->request_mutex);
919}
920EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
921
922void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
923 struct ceph_osd_request *req)
924{
925 if (!req->r_linger) {
926 dout("set_request_linger %p\n", req);
927 req->r_linger = 1;
928 /*
929 * caller is now responsible for calling
930 * unregister_linger_request
931 */
932 ceph_osdc_get_request(req);
933 }
934}
935EXPORT_SYMBOL(ceph_osdc_set_request_linger);
936
Sage Weilf24e9982009-10-06 11:31:10 -0700937/*
938 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
939 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300940 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800941 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700942 *
943 * Return 0 if unchanged, 1 if changed, or negative on error.
944 *
945 * Caller should hold map_sem for read and request_mutex.
946 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800947static int __map_request(struct ceph_osd_client *osdc,
948 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700949{
950 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800951 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700952 int acting[CEPH_PG_MAX_SIZE];
953 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700954 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700955
Sage Weil6f6c7002011-01-17 20:34:08 -0800956 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700957 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
958 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800959 if (err) {
960 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700961 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800962 }
Sage Weil51042122009-11-04 11:39:12 -0800963 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800964 req->r_pgid = pgid;
965
Sage Weild85b7052010-05-10 10:24:48 -0700966 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
967 if (err > 0) {
968 o = acting[0];
969 num = err;
970 }
Sage Weilf24e9982009-10-06 11:31:10 -0700971
972 if ((req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700973 req->r_sent >= req->r_osd->o_incarnation &&
974 req->r_num_pg_osds == num &&
975 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700976 (req->r_osd == NULL && o == -1))
977 return 0; /* no change */
978
Sage Weil6f6c7002011-01-17 20:34:08 -0800979 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800980 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700981 req->r_osd ? req->r_osd->o_osd : -1);
982
Sage Weild85b7052010-05-10 10:24:48 -0700983 /* record full pg acting set */
984 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
985 req->r_num_pg_osds = num;
986
Sage Weilf24e9982009-10-06 11:31:10 -0700987 if (req->r_osd) {
988 __cancel_request(req);
989 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700990 req->r_osd = NULL;
991 }
992
993 req->r_osd = __lookup_osd(osdc, o);
994 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800995 err = -ENOMEM;
996 req->r_osd = create_osd(osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -0800997 if (!req->r_osd) {
998 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800999 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001000 }
Sage Weilf24e9982009-10-06 11:31:10 -07001001
Sage Weil6f6c7002011-01-17 20:34:08 -08001002 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001003 req->r_osd->o_osd = o;
1004 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
1005 __insert_osd(osdc, req->r_osd);
1006
1007 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1008 }
1009
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001010 if (req->r_osd) {
1011 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001012 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001013 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1014 } else {
1015 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001016 }
Sage Weild85b7052010-05-10 10:24:48 -07001017 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001018
1019out:
Sage Weilf24e9982009-10-06 11:31:10 -07001020 return err;
1021}
1022
1023/*
1024 * caller should hold map_sem (for read) and request_mutex
1025 */
1026static int __send_request(struct ceph_osd_client *osdc,
1027 struct ceph_osd_request *req)
1028{
1029 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001030
1031 dout("send_request %p tid %llu to osd%d flags %d\n",
1032 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1033
1034 reqhead = req->r_request->front.iov_base;
1035 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1036 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1037 reqhead->reassert_version = req->r_reassert_version;
1038
Sage Weil3dd72fc2010-03-22 14:42:30 -07001039 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001040 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001041
1042 ceph_msg_get(req->r_request); /* send consumes a ref */
1043 ceph_con_send(&req->r_osd->o_con, req->r_request);
1044 req->r_sent = req->r_osd->o_incarnation;
1045 return 0;
1046}
1047
1048/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001049 * Send any requests in the queue (req_unsent).
1050 */
1051static void send_queued(struct ceph_osd_client *osdc)
1052{
1053 struct ceph_osd_request *req, *tmp;
1054
1055 dout("send_queued\n");
1056 mutex_lock(&osdc->request_mutex);
1057 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1058 __send_request(osdc, req);
1059 }
1060 mutex_unlock(&osdc->request_mutex);
1061}
1062
1063/*
Sage Weilf24e9982009-10-06 11:31:10 -07001064 * Timeout callback, called every N seconds when 1 or more osd
1065 * requests has been active for more than N seconds. When this
1066 * happens, we ping all OSDs with requests who have timed out to
1067 * ensure any communications channel reset is detected. Reset the
1068 * request timeouts another N seconds in the future as we go.
1069 * Reschedule the timeout event another N seconds in future (unless
1070 * there are no open requests).
1071 */
1072static void handle_timeout(struct work_struct *work)
1073{
1074 struct ceph_osd_client *osdc =
1075 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001076 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001077 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001078 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001079 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001080 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001081 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001082 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001083 dout("timeout\n");
1084 down_read(&osdc->map_sem);
1085
1086 ceph_monc_request_next_osdmap(&osdc->client->monc);
1087
1088 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001089
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001090 /*
1091 * reset osds that appear to be _really_ unresponsive. this
1092 * is a failsafe measure.. we really shouldn't be getting to
1093 * this point if the system is working properly. the monitors
1094 * should mark the osd as failed and we should find out about
1095 * it from an updated osd map.
1096 */
Sage Weilf26e6812010-04-21 11:09:38 -07001097 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001098 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1099 r_req_lru_item);
1100
Sage Weil4cf9d542011-07-26 11:27:24 -07001101 /* hasn't been long enough since we sent it? */
Sage Weil3dd72fc2010-03-22 14:42:30 -07001102 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001103 break;
1104
Sage Weil4cf9d542011-07-26 11:27:24 -07001105 /* hasn't been long enough since it was acked? */
1106 if (req->r_request->ack_stamp == 0 ||
1107 time_before(jiffies, req->r_request->ack_stamp + timeout))
1108 break;
1109
Sage Weil3dd72fc2010-03-22 14:42:30 -07001110 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001111 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001112 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001113
1114 osd = req->r_osd;
1115 BUG_ON(!osd);
1116 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1117 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001118 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001119 }
1120
1121 /*
1122 * ping osds that are a bit slow. this ensures that if there
1123 * is a break in the TCP connection we will notice, and reopen
1124 * a connection with that osd (from the fault callback).
1125 */
1126 INIT_LIST_HEAD(&slow_osds);
1127 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001128 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001129 break;
1130
1131 osd = req->r_osd;
1132 BUG_ON(!osd);
1133 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001134 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001135 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1136 }
1137 while (!list_empty(&slow_osds)) {
1138 osd = list_entry(slow_osds.next, struct ceph_osd,
1139 o_keepalive_item);
1140 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001141 ceph_con_keepalive(&osd->o_con);
1142 }
1143
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001144 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001145 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001146 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001147 up_read(&osdc->map_sem);
1148}
1149
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001150static void handle_osds_timeout(struct work_struct *work)
1151{
1152 struct ceph_osd_client *osdc =
1153 container_of(work, struct ceph_osd_client,
1154 osds_timeout_work.work);
1155 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001156 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001157
1158 dout("osds timeout\n");
1159 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001160 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001161 up_read(&osdc->map_sem);
1162
1163 schedule_delayed_work(&osdc->osds_timeout_work,
1164 round_jiffies_relative(delay));
1165}
1166
Sage Weil25845472011-06-03 09:37:09 -07001167static void complete_request(struct ceph_osd_request *req)
1168{
1169 if (req->r_safe_callback)
1170 req->r_safe_callback(req, NULL);
1171 complete_all(&req->r_safe_completion); /* fsync waiter */
1172}
1173
Sage Weilf24e9982009-10-06 11:31:10 -07001174/*
1175 * handle osd op reply. either call the callback if it is specified,
1176 * or do the completion to wake up the waiting thread.
1177 */
Sage Weil350b1c32009-12-22 10:45:45 -08001178static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1179 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001180{
1181 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1182 struct ceph_osd_request *req;
1183 u64 tid;
1184 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001185 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001186
Sage Weil6df058c2009-12-22 11:24:33 -08001187 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001188 if (msg->front.iov_len < sizeof(*rhead))
1189 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001190 numops = le32_to_cpu(rhead->num_ops);
1191 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001192 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001193 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1194 numops * sizeof(struct ceph_osd_op))
1195 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001196 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001197 /* lookup */
1198 mutex_lock(&osdc->request_mutex);
1199 req = __lookup_request(osdc, tid);
1200 if (req == NULL) {
1201 dout("handle_reply tid %llu dne\n", tid);
1202 mutex_unlock(&osdc->request_mutex);
1203 return;
1204 }
1205 ceph_osdc_get_request(req);
1206 flags = le32_to_cpu(rhead->flags);
1207
Sage Weil350b1c32009-12-22 10:45:45 -08001208 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001209 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001210 * avoid a (safe but slower) revoke later.
1211 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001212 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001213 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001214 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -08001215 ceph_con_put(con);
1216 }
1217
Sage Weilf24e9982009-10-06 11:31:10 -07001218 if (!req->r_got_reply) {
1219 unsigned bytes;
1220
1221 req->r_result = le32_to_cpu(rhead->result);
1222 bytes = le32_to_cpu(msg->hdr.data_len);
1223 dout("handle_reply result %d bytes %d\n", req->r_result,
1224 bytes);
1225 if (req->r_result == 0)
1226 req->r_result = bytes;
1227
1228 /* in case this is a write and we need to replay, */
1229 req->r_reassert_version = rhead->reassert_version;
1230
1231 req->r_got_reply = 1;
1232 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1233 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001234 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001235 goto done;
1236 }
1237
1238 dout("handle_reply tid %llu flags %d\n", tid, flags);
1239
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001240 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1241 __register_linger_request(osdc, req);
1242
Sage Weilf24e9982009-10-06 11:31:10 -07001243 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001244 if (result < 0 ||
1245 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001246 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1247 __unregister_request(osdc, req);
1248
1249 mutex_unlock(&osdc->request_mutex);
1250
1251 if (req->r_callback)
1252 req->r_callback(req, msg);
1253 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001254 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001255
Sage Weil25845472011-06-03 09:37:09 -07001256 if (flags & CEPH_OSD_FLAG_ONDISK)
1257 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001258
1259done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001260 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001261 ceph_osdc_put_request(req);
1262 return;
1263
1264bad:
1265 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1266 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1267 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001268 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001269}
1270
Sage Weil6f6c7002011-01-17 20:34:08 -08001271static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001272{
Sage Weilf24e9982009-10-06 11:31:10 -07001273 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001274
Sage Weil6f6c7002011-01-17 20:34:08 -08001275 for (p = rb_first(&osdc->osds); p; p = n) {
1276 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001277
Sage Weil6f6c7002011-01-17 20:34:08 -08001278 n = rb_next(p);
1279 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1280 memcmp(&osd->o_con.peer_addr,
1281 ceph_osd_addr(osdc->osdmap,
1282 osd->o_osd),
1283 sizeof(struct ceph_entity_addr)) != 0)
1284 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001285 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001286}
1287
1288/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001289 * Requeue requests whose mapping to an OSD has changed. If requests map to
1290 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001291 *
1292 * Caller should hold map_sem for read and request_mutex.
1293 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001294static void kick_requests(struct ceph_osd_client *osdc)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001295{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001296 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001297 struct rb_node *p;
1298 int needmap = 0;
1299 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001300
Sage Weil6f6c7002011-01-17 20:34:08 -08001301 dout("kick_requests\n");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001302 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001303 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1304 req = rb_entry(p, struct ceph_osd_request, r_node);
1305 err = __map_request(osdc, req);
1306 if (err < 0)
1307 continue; /* error */
1308 if (req->r_osd == NULL) {
1309 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1310 needmap++; /* request a newer map */
1311 } else if (err > 0) {
1312 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1313 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001314 if (!req->r_linger)
1315 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001316 }
1317 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001318
1319 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1320 r_linger_item) {
1321 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1322
1323 err = __map_request(osdc, req);
1324 if (err == 0)
1325 continue; /* no change and no osd was specified */
1326 if (err < 0)
1327 continue; /* hrm! */
1328 if (req->r_osd == NULL) {
1329 dout("tid %llu maps to no valid osd\n", req->r_tid);
1330 needmap++; /* request a newer map */
1331 continue;
1332 }
1333
1334 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1335 req->r_osd ? req->r_osd->o_osd : -1);
1336 __unregister_linger_request(osdc, req);
1337 __register_request(osdc, req);
1338 }
Sage Weilf24e9982009-10-06 11:31:10 -07001339 mutex_unlock(&osdc->request_mutex);
1340
1341 if (needmap) {
1342 dout("%d requests for down osds, need new map\n", needmap);
1343 ceph_monc_request_next_osdmap(&osdc->client->monc);
1344 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001345}
Sage Weil6f6c7002011-01-17 20:34:08 -08001346
1347
Sage Weilf24e9982009-10-06 11:31:10 -07001348/*
1349 * Process updated osd map.
1350 *
1351 * The message contains any number of incremental and full maps, normally
1352 * indicating some sort of topology change in the cluster. Kick requests
1353 * off to different OSDs as needed.
1354 */
1355void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1356{
1357 void *p, *end, *next;
1358 u32 nr_maps, maplen;
1359 u32 epoch;
1360 struct ceph_osdmap *newmap = NULL, *oldmap;
1361 int err;
1362 struct ceph_fsid fsid;
1363
1364 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1365 p = msg->front.iov_base;
1366 end = p + msg->front.iov_len;
1367
1368 /* verify fsid */
1369 ceph_decode_need(&p, end, sizeof(fsid), bad);
1370 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001371 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1372 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001373
1374 down_write(&osdc->map_sem);
1375
1376 /* incremental maps */
1377 ceph_decode_32_safe(&p, end, nr_maps, bad);
1378 dout(" %d inc maps\n", nr_maps);
1379 while (nr_maps > 0) {
1380 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001381 epoch = ceph_decode_32(&p);
1382 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001383 ceph_decode_need(&p, end, maplen, bad);
1384 next = p + maplen;
1385 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1386 dout("applying incremental map %u len %d\n",
1387 epoch, maplen);
1388 newmap = osdmap_apply_incremental(&p, next,
1389 osdc->osdmap,
1390 osdc->client->msgr);
1391 if (IS_ERR(newmap)) {
1392 err = PTR_ERR(newmap);
1393 goto bad;
1394 }
Sage Weil30dc6382009-12-21 14:49:37 -08001395 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001396 if (newmap != osdc->osdmap) {
1397 ceph_osdmap_destroy(osdc->osdmap);
1398 osdc->osdmap = newmap;
1399 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001400 kick_requests(osdc);
1401 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001402 } else {
1403 dout("ignoring incremental map %u len %d\n",
1404 epoch, maplen);
1405 }
1406 p = next;
1407 nr_maps--;
1408 }
1409 if (newmap)
1410 goto done;
1411
1412 /* full maps */
1413 ceph_decode_32_safe(&p, end, nr_maps, bad);
1414 dout(" %d full maps\n", nr_maps);
1415 while (nr_maps) {
1416 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001417 epoch = ceph_decode_32(&p);
1418 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001419 ceph_decode_need(&p, end, maplen, bad);
1420 if (nr_maps > 1) {
1421 dout("skipping non-latest full map %u len %d\n",
1422 epoch, maplen);
1423 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1424 dout("skipping full map %u len %d, "
1425 "older than our %u\n", epoch, maplen,
1426 osdc->osdmap->epoch);
1427 } else {
1428 dout("taking full map %u len %d\n", epoch, maplen);
1429 newmap = osdmap_decode(&p, p+maplen);
1430 if (IS_ERR(newmap)) {
1431 err = PTR_ERR(newmap);
1432 goto bad;
1433 }
Sage Weil30dc6382009-12-21 14:49:37 -08001434 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001435 oldmap = osdc->osdmap;
1436 osdc->osdmap = newmap;
1437 if (oldmap)
1438 ceph_osdmap_destroy(oldmap);
Sage Weil6f6c7002011-01-17 20:34:08 -08001439 kick_requests(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001440 }
1441 p += maplen;
1442 nr_maps--;
1443 }
1444
1445done:
1446 downgrade_write(&osdc->map_sem);
1447 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001448
1449 /*
1450 * subscribe to subsequent osdmap updates if full to ensure
1451 * we find out when we are no longer full and stop returning
1452 * ENOSPC.
1453 */
1454 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1455 ceph_monc_request_next_osdmap(&osdc->client->monc);
1456
Sage Weil6f6c7002011-01-17 20:34:08 -08001457 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001458 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001459 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001460 return;
1461
1462bad:
1463 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001464 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001465 up_write(&osdc->map_sem);
1466 return;
1467}
1468
Sage Weilf24e9982009-10-06 11:31:10 -07001469/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001470 * watch/notify callback event infrastructure
1471 *
1472 * These callbacks are used both for watch and notify operations.
1473 */
1474static void __release_event(struct kref *kref)
1475{
1476 struct ceph_osd_event *event =
1477 container_of(kref, struct ceph_osd_event, kref);
1478
1479 dout("__release_event %p\n", event);
1480 kfree(event);
1481}
1482
1483static void get_event(struct ceph_osd_event *event)
1484{
1485 kref_get(&event->kref);
1486}
1487
1488void ceph_osdc_put_event(struct ceph_osd_event *event)
1489{
1490 kref_put(&event->kref, __release_event);
1491}
1492EXPORT_SYMBOL(ceph_osdc_put_event);
1493
1494static void __insert_event(struct ceph_osd_client *osdc,
1495 struct ceph_osd_event *new)
1496{
1497 struct rb_node **p = &osdc->event_tree.rb_node;
1498 struct rb_node *parent = NULL;
1499 struct ceph_osd_event *event = NULL;
1500
1501 while (*p) {
1502 parent = *p;
1503 event = rb_entry(parent, struct ceph_osd_event, node);
1504 if (new->cookie < event->cookie)
1505 p = &(*p)->rb_left;
1506 else if (new->cookie > event->cookie)
1507 p = &(*p)->rb_right;
1508 else
1509 BUG();
1510 }
1511
1512 rb_link_node(&new->node, parent, p);
1513 rb_insert_color(&new->node, &osdc->event_tree);
1514}
1515
1516static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1517 u64 cookie)
1518{
1519 struct rb_node **p = &osdc->event_tree.rb_node;
1520 struct rb_node *parent = NULL;
1521 struct ceph_osd_event *event = NULL;
1522
1523 while (*p) {
1524 parent = *p;
1525 event = rb_entry(parent, struct ceph_osd_event, node);
1526 if (cookie < event->cookie)
1527 p = &(*p)->rb_left;
1528 else if (cookie > event->cookie)
1529 p = &(*p)->rb_right;
1530 else
1531 return event;
1532 }
1533 return NULL;
1534}
1535
1536static void __remove_event(struct ceph_osd_event *event)
1537{
1538 struct ceph_osd_client *osdc = event->osdc;
1539
1540 if (!RB_EMPTY_NODE(&event->node)) {
1541 dout("__remove_event removed %p\n", event);
1542 rb_erase(&event->node, &osdc->event_tree);
1543 ceph_osdc_put_event(event);
1544 } else {
1545 dout("__remove_event didn't remove %p\n", event);
1546 }
1547}
1548
1549int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1550 void (*event_cb)(u64, u64, u8, void *),
1551 int one_shot, void *data,
1552 struct ceph_osd_event **pevent)
1553{
1554 struct ceph_osd_event *event;
1555
1556 event = kmalloc(sizeof(*event), GFP_NOIO);
1557 if (!event)
1558 return -ENOMEM;
1559
1560 dout("create_event %p\n", event);
1561 event->cb = event_cb;
1562 event->one_shot = one_shot;
1563 event->data = data;
1564 event->osdc = osdc;
1565 INIT_LIST_HEAD(&event->osd_node);
1566 kref_init(&event->kref); /* one ref for us */
1567 kref_get(&event->kref); /* one ref for the caller */
1568 init_completion(&event->completion);
1569
1570 spin_lock(&osdc->event_lock);
1571 event->cookie = ++osdc->event_count;
1572 __insert_event(osdc, event);
1573 spin_unlock(&osdc->event_lock);
1574
1575 *pevent = event;
1576 return 0;
1577}
1578EXPORT_SYMBOL(ceph_osdc_create_event);
1579
1580void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1581{
1582 struct ceph_osd_client *osdc = event->osdc;
1583
1584 dout("cancel_event %p\n", event);
1585 spin_lock(&osdc->event_lock);
1586 __remove_event(event);
1587 spin_unlock(&osdc->event_lock);
1588 ceph_osdc_put_event(event); /* caller's */
1589}
1590EXPORT_SYMBOL(ceph_osdc_cancel_event);
1591
1592
1593static void do_event_work(struct work_struct *work)
1594{
1595 struct ceph_osd_event_work *event_work =
1596 container_of(work, struct ceph_osd_event_work, work);
1597 struct ceph_osd_event *event = event_work->event;
1598 u64 ver = event_work->ver;
1599 u64 notify_id = event_work->notify_id;
1600 u8 opcode = event_work->opcode;
1601
1602 dout("do_event_work completing %p\n", event);
1603 event->cb(ver, notify_id, opcode, event->data);
1604 complete(&event->completion);
1605 dout("do_event_work completed %p\n", event);
1606 ceph_osdc_put_event(event);
1607 kfree(event_work);
1608}
1609
1610
1611/*
1612 * Process osd watch notifications
1613 */
1614void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1615{
1616 void *p, *end;
1617 u8 proto_ver;
1618 u64 cookie, ver, notify_id;
1619 u8 opcode;
1620 struct ceph_osd_event *event;
1621 struct ceph_osd_event_work *event_work;
1622
1623 p = msg->front.iov_base;
1624 end = p + msg->front.iov_len;
1625
1626 ceph_decode_8_safe(&p, end, proto_ver, bad);
1627 ceph_decode_8_safe(&p, end, opcode, bad);
1628 ceph_decode_64_safe(&p, end, cookie, bad);
1629 ceph_decode_64_safe(&p, end, ver, bad);
1630 ceph_decode_64_safe(&p, end, notify_id, bad);
1631
1632 spin_lock(&osdc->event_lock);
1633 event = __find_event(osdc, cookie);
1634 if (event) {
1635 get_event(event);
1636 if (event->one_shot)
1637 __remove_event(event);
1638 }
1639 spin_unlock(&osdc->event_lock);
1640 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1641 cookie, ver, event);
1642 if (event) {
1643 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001644 if (!event_work) {
1645 dout("ERROR: could not allocate event_work\n");
1646 goto done_err;
1647 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001648 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001649 event_work->event = event;
1650 event_work->ver = ver;
1651 event_work->notify_id = notify_id;
1652 event_work->opcode = opcode;
1653 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1654 dout("WARNING: failed to queue notify event work\n");
1655 goto done_err;
1656 }
1657 }
1658
1659 return;
1660
1661done_err:
1662 complete(&event->completion);
1663 ceph_osdc_put_event(event);
1664 return;
1665
1666bad:
1667 pr_err("osdc handle_watch_notify corrupt msg\n");
1668 return;
1669}
1670
1671int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1672{
1673 int err;
1674
1675 dout("wait_event %p\n", event);
1676 err = wait_for_completion_interruptible_timeout(&event->completion,
1677 timeout * HZ);
1678 ceph_osdc_put_event(event);
1679 if (err > 0)
1680 err = 0;
1681 dout("wait_event %p returns %d\n", event, err);
1682 return err;
1683}
1684EXPORT_SYMBOL(ceph_osdc_wait_event);
1685
1686/*
Sage Weilf24e9982009-10-06 11:31:10 -07001687 * Register request, send initial attempt.
1688 */
1689int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1690 struct ceph_osd_request *req,
1691 bool nofail)
1692{
Sage Weilc1ea8822009-10-08 16:55:47 -07001693 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001694
1695 req->r_request->pages = req->r_pages;
1696 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001697#ifdef CONFIG_BLOCK
1698 req->r_request->bio = req->r_bio;
1699#endif
1700 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001701
1702 register_request(osdc, req);
1703
1704 down_read(&osdc->map_sem);
1705 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001706 /*
1707 * a racing kick_requests() may have sent the message for us
1708 * while we dropped request_mutex above, so only send now if
1709 * the request still han't been touched yet.
1710 */
1711 if (req->r_sent == 0) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001712 rc = __map_request(osdc, req);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001713 if (rc < 0) {
1714 if (nofail) {
1715 dout("osdc_start_request failed map, "
1716 " will retry %lld\n", req->r_tid);
1717 rc = 0;
1718 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001719 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001720 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001721 if (req->r_osd == NULL) {
1722 dout("send_request %p no up osds in pg\n", req);
1723 ceph_monc_request_next_osdmap(&osdc->client->monc);
1724 } else {
1725 rc = __send_request(osdc, req);
1726 if (rc) {
1727 if (nofail) {
1728 dout("osdc_start_request failed send, "
1729 " will retry %lld\n", req->r_tid);
1730 rc = 0;
1731 } else {
1732 __unregister_request(osdc, req);
1733 }
Sage Weilc1ea8822009-10-08 16:55:47 -07001734 }
Sage Weilf24e9982009-10-06 11:31:10 -07001735 }
1736 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001737
1738out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001739 mutex_unlock(&osdc->request_mutex);
1740 up_read(&osdc->map_sem);
1741 return rc;
1742}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001743EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001744
1745/*
1746 * wait for a request to complete
1747 */
1748int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1749 struct ceph_osd_request *req)
1750{
1751 int rc;
1752
1753 rc = wait_for_completion_interruptible(&req->r_completion);
1754 if (rc < 0) {
1755 mutex_lock(&osdc->request_mutex);
1756 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001757 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001758 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001759 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001760 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001761 return rc;
1762 }
1763
1764 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1765 return req->r_result;
1766}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001767EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001768
1769/*
1770 * sync - wait for all in-flight requests to flush. avoid starvation.
1771 */
1772void ceph_osdc_sync(struct ceph_osd_client *osdc)
1773{
1774 struct ceph_osd_request *req;
1775 u64 last_tid, next_tid = 0;
1776
1777 mutex_lock(&osdc->request_mutex);
1778 last_tid = osdc->last_tid;
1779 while (1) {
1780 req = __lookup_request_ge(osdc, next_tid);
1781 if (!req)
1782 break;
1783 if (req->r_tid > last_tid)
1784 break;
1785
1786 next_tid = req->r_tid + 1;
1787 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1788 continue;
1789
1790 ceph_osdc_get_request(req);
1791 mutex_unlock(&osdc->request_mutex);
1792 dout("sync waiting on tid %llu (last is %llu)\n",
1793 req->r_tid, last_tid);
1794 wait_for_completion(&req->r_safe_completion);
1795 mutex_lock(&osdc->request_mutex);
1796 ceph_osdc_put_request(req);
1797 }
1798 mutex_unlock(&osdc->request_mutex);
1799 dout("sync done (thru tid %llu)\n", last_tid);
1800}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001801EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001802
1803/*
1804 * init, shutdown
1805 */
1806int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1807{
1808 int err;
1809
1810 dout("init\n");
1811 osdc->client = client;
1812 osdc->osdmap = NULL;
1813 init_rwsem(&osdc->map_sem);
1814 init_completion(&osdc->map_waiters);
1815 osdc->last_requested_map = 0;
1816 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001817 osdc->last_tid = 0;
1818 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001819 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001820 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001821 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001822 INIT_LIST_HEAD(&osdc->req_unsent);
1823 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001824 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001825 osdc->num_requests = 0;
1826 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001827 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001828 spin_lock_init(&osdc->event_lock);
1829 osdc->event_tree = RB_ROOT;
1830 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001831
1832 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001833 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001834
Sage Weil5f44f142009-11-18 14:52:18 -08001835 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001836 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1837 sizeof(struct ceph_osd_request));
1838 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001839 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001840
Sage Weil4f482802010-04-24 09:56:35 -07001841 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1842 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001843 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001844 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001845 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001846 OSD_OPREPLY_FRONT_LEN, 10, true,
1847 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001848 if (err < 0)
1849 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001850
1851 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1852 if (IS_ERR(osdc->notify_wq)) {
1853 err = PTR_ERR(osdc->notify_wq);
1854 osdc->notify_wq = NULL;
1855 goto out_msgpool;
1856 }
Sage Weilf24e9982009-10-06 11:31:10 -07001857 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001858
Sage Weilc16e7862010-03-01 13:02:00 -08001859out_msgpool:
1860 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001861out_mempool:
1862 mempool_destroy(osdc->req_mempool);
1863out:
1864 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001865}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001866EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001867
1868void ceph_osdc_stop(struct ceph_osd_client *osdc)
1869{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001870 flush_workqueue(osdc->notify_wq);
1871 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001872 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001873 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001874 if (osdc->osdmap) {
1875 ceph_osdmap_destroy(osdc->osdmap);
1876 osdc->osdmap = NULL;
1877 }
Sage Weilaca420b2011-08-31 14:45:53 -07001878 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001879 mempool_destroy(osdc->req_mempool);
1880 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001881 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001882}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001883EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001884
1885/*
1886 * Read some contiguous pages. If we cross a stripe boundary, shorten
1887 * *plen. Return number of bytes read, or error.
1888 */
1889int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1890 struct ceph_vino vino, struct ceph_file_layout *layout,
1891 u64 off, u64 *plen,
1892 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001893 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001894{
1895 struct ceph_osd_request *req;
1896 int rc = 0;
1897
1898 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1899 vino.snap, off, *plen);
1900 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1901 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1902 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001903 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001904 if (!req)
1905 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001906
1907 /* it may be a short read due to an object boundary */
1908 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001909
Sage Weilb7495fc2010-11-09 12:43:12 -08001910 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1911 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001912
1913 rc = ceph_osdc_start_request(osdc, req, false);
1914 if (!rc)
1915 rc = ceph_osdc_wait_request(osdc, req);
1916
1917 ceph_osdc_put_request(req);
1918 dout("readpages result %d\n", rc);
1919 return rc;
1920}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001921EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001922
1923/*
1924 * do a synchronous write on N pages
1925 */
1926int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1927 struct ceph_file_layout *layout,
1928 struct ceph_snap_context *snapc,
1929 u64 off, u64 len,
1930 u32 truncate_seq, u64 truncate_size,
1931 struct timespec *mtime,
1932 struct page **pages, int num_pages,
1933 int flags, int do_sync, bool nofail)
1934{
1935 struct ceph_osd_request *req;
1936 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001937 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001938
1939 BUG_ON(vino.snap != CEPH_NOSNAP);
1940 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1941 CEPH_OSD_OP_WRITE,
1942 flags | CEPH_OSD_FLAG_ONDISK |
1943 CEPH_OSD_FLAG_WRITE,
1944 snapc, do_sync,
1945 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001946 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001947 if (!req)
1948 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001949
1950 /* it may be a short write due to an object boundary */
1951 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001952 dout("writepages %llu~%llu (%d pages)\n", off, len,
1953 req->r_num_pages);
1954
1955 rc = ceph_osdc_start_request(osdc, req, nofail);
1956 if (!rc)
1957 rc = ceph_osdc_wait_request(osdc, req);
1958
1959 ceph_osdc_put_request(req);
1960 if (rc == 0)
1961 rc = len;
1962 dout("writepages result %d\n", rc);
1963 return rc;
1964}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001965EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001966
1967/*
1968 * handle incoming message
1969 */
1970static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1971{
1972 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001973 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001974 int type = le16_to_cpu(msg->hdr.type);
1975
1976 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001977 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001978 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001979
1980 switch (type) {
1981 case CEPH_MSG_OSD_MAP:
1982 ceph_osdc_handle_map(osdc, msg);
1983 break;
1984 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001985 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001986 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001987 case CEPH_MSG_WATCH_NOTIFY:
1988 handle_watch_notify(osdc, msg);
1989 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001990
1991 default:
1992 pr_err("received unknown message type %d %s\n", type,
1993 ceph_msg_type_name(type));
1994 }
Sage Weil4a32f932010-06-13 10:27:53 -07001995out:
Sage Weilf24e9982009-10-06 11:31:10 -07001996 ceph_msg_put(msg);
1997}
1998
Sage Weil5b3a4db2010-02-19 21:43:23 -08001999/*
Sage Weil21b667f2010-03-04 10:22:59 -08002000 * lookup and return message for incoming reply. set up reply message
2001 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002002 */
2003static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002004 struct ceph_msg_header *hdr,
2005 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002006{
2007 struct ceph_osd *osd = con->private;
2008 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002009 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002010 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002011 int front = le32_to_cpu(hdr->front_len);
2012 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002013 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002014
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002015 tid = le64_to_cpu(hdr->tid);
2016 mutex_lock(&osdc->request_mutex);
2017 req = __lookup_request(osdc, tid);
2018 if (!req) {
2019 *skip = 1;
2020 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08002021 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08002022 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002023 goto out;
2024 }
Sage Weilc16e7862010-03-01 13:02:00 -08002025
2026 if (req->r_con_filling_msg) {
2027 dout("get_reply revoking msg %p from old con %p\n",
2028 req->r_reply, req->r_con_filling_msg);
2029 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2030 ceph_con_put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002031 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002032 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002033
Sage Weilc16e7862010-03-01 13:02:00 -08002034 if (front > req->r_reply->front.iov_len) {
2035 pr_warning("get_reply front %d > preallocated %d\n",
2036 front, (int)req->r_reply->front.iov_len);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002037 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002038 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002039 goto out;
2040 ceph_msg_put(req->r_reply);
2041 req->r_reply = m;
2042 }
2043 m = ceph_msg_get(req->r_reply);
2044
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002045 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002046 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002047
2048 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002049 pr_warning("tid %lld reply has %d bytes %d pages, we"
2050 " had only %d pages ready\n", tid, data_len,
2051 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002052 *skip = 1;
2053 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002054 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002055 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002056 }
Sage Weil21b667f2010-03-04 10:22:59 -08002057 m->pages = req->r_pages;
2058 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002059 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002060#ifdef CONFIG_BLOCK
2061 m->bio = req->r_bio;
2062#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002063 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002064 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002065 req->r_con_filling_msg = ceph_con_get(con);
2066 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002067
2068out:
2069 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002070 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002071
2072}
2073
2074static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2075 struct ceph_msg_header *hdr,
2076 int *skip)
2077{
2078 struct ceph_osd *osd = con->private;
2079 int type = le16_to_cpu(hdr->type);
2080 int front = le32_to_cpu(hdr->front_len);
2081
2082 switch (type) {
2083 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002084 case CEPH_MSG_WATCH_NOTIFY:
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002085 return ceph_msg_new(type, front, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002086 case CEPH_MSG_OSD_OPREPLY:
2087 return get_reply(con, hdr, skip);
2088 default:
2089 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2090 osd->o_osd);
2091 *skip = 1;
2092 return NULL;
2093 }
Sage Weilf24e9982009-10-06 11:31:10 -07002094}
2095
2096/*
2097 * Wrappers to refcount containing ceph_osd struct
2098 */
2099static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2100{
2101 struct ceph_osd *osd = con->private;
2102 if (get_osd(osd))
2103 return con;
2104 return NULL;
2105}
2106
2107static void put_osd_con(struct ceph_connection *con)
2108{
2109 struct ceph_osd *osd = con->private;
2110 put_osd(osd);
2111}
2112
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002113/*
2114 * authentication
2115 */
2116static int get_authorizer(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002117 void **buf, int *len, int *proto,
2118 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002119{
2120 struct ceph_osd *o = con->private;
2121 struct ceph_osd_client *osdc = o->o_osdc;
2122 struct ceph_auth_client *ac = osdc->client->monc.auth;
2123 int ret = 0;
2124
2125 if (force_new && o->o_authorizer) {
2126 ac->ops->destroy_authorizer(ac, o->o_authorizer);
2127 o->o_authorizer = NULL;
2128 }
2129 if (o->o_authorizer == NULL) {
2130 ret = ac->ops->create_authorizer(
2131 ac, CEPH_ENTITY_TYPE_OSD,
2132 &o->o_authorizer,
2133 &o->o_authorizer_buf,
2134 &o->o_authorizer_buf_len,
2135 &o->o_authorizer_reply_buf,
2136 &o->o_authorizer_reply_buf_len);
2137 if (ret)
Sage Weil213c99e2010-08-03 10:25:11 -07002138 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002139 }
2140
2141 *proto = ac->protocol;
2142 *buf = o->o_authorizer_buf;
2143 *len = o->o_authorizer_buf_len;
2144 *reply_buf = o->o_authorizer_reply_buf;
2145 *reply_len = o->o_authorizer_reply_buf_len;
2146 return 0;
2147}
2148
2149
2150static int verify_authorizer_reply(struct ceph_connection *con, int len)
2151{
2152 struct ceph_osd *o = con->private;
2153 struct ceph_osd_client *osdc = o->o_osdc;
2154 struct ceph_auth_client *ac = osdc->client->monc.auth;
2155
2156 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
2157}
2158
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002159static int invalidate_authorizer(struct ceph_connection *con)
2160{
2161 struct ceph_osd *o = con->private;
2162 struct ceph_osd_client *osdc = o->o_osdc;
2163 struct ceph_auth_client *ac = osdc->client->monc.auth;
2164
2165 if (ac->ops->invalidate_authorizer)
2166 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2167
2168 return ceph_monc_validate_auth(&osdc->client->monc);
2169}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002170
Tobias Klauser9e327892010-05-20 10:40:19 +02002171static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002172 .get = get_osd_con,
2173 .put = put_osd_con,
2174 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002175 .get_authorizer = get_authorizer,
2176 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002177 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002178 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002179 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002180};