blob: 7330c2757c0c23c6cf0f11b1331d59de82983690 [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
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800688static void __move_osd_to_lru(struct ceph_osd_client *osdc,
689 struct ceph_osd *osd)
690{
691 dout("__move_osd_to_lru %p\n", osd);
692 BUG_ON(!list_empty(&osd->o_osd_lru));
693 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700694 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800695}
696
697static void __remove_osd_from_lru(struct ceph_osd *osd)
698{
699 dout("__remove_osd_from_lru %p\n", osd);
700 if (!list_empty(&osd->o_osd_lru))
701 list_del_init(&osd->o_osd_lru);
702}
703
704static void remove_old_osds(struct ceph_osd_client *osdc, int remove_all)
705{
706 struct ceph_osd *osd, *nosd;
707
708 dout("__remove_old_osds %p\n", osdc);
709 mutex_lock(&osdc->request_mutex);
710 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
711 if (!remove_all && time_before(jiffies, osd->lru_ttl))
712 break;
713 __remove_osd(osdc, osd);
714 }
715 mutex_unlock(&osdc->request_mutex);
716}
717
Sage Weilf24e9982009-10-06 11:31:10 -0700718/*
719 * reset osd connect
720 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800721static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700722{
Sage Weil87b315a2010-03-22 14:51:18 -0700723 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700724 int ret = 0;
725
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800726 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700727 if (list_empty(&osd->o_requests) &&
728 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800729 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700730 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
731 &osd->o_con.peer_addr,
732 sizeof(osd->o_con.peer_addr)) == 0 &&
733 !ceph_con_opened(&osd->o_con)) {
734 dout(" osd addr hasn't changed and connection never opened,"
735 " letting msgr retry");
736 /* touch each r_stamp for handle_timeout()'s benfit */
737 list_for_each_entry(req, &osd->o_requests, r_osd_item)
738 req->r_stamp = jiffies;
739 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700740 } else {
741 ceph_con_close(&osd->o_con);
742 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
743 osd->o_incarnation++;
744 }
745 return ret;
746}
747
748static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
749{
750 struct rb_node **p = &osdc->osds.rb_node;
751 struct rb_node *parent = NULL;
752 struct ceph_osd *osd = NULL;
753
754 while (*p) {
755 parent = *p;
756 osd = rb_entry(parent, struct ceph_osd, o_node);
757 if (new->o_osd < osd->o_osd)
758 p = &(*p)->rb_left;
759 else if (new->o_osd > osd->o_osd)
760 p = &(*p)->rb_right;
761 else
762 BUG();
763 }
764
765 rb_link_node(&new->o_node, parent, p);
766 rb_insert_color(&new->o_node, &osdc->osds);
767}
768
769static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
770{
771 struct ceph_osd *osd;
772 struct rb_node *n = osdc->osds.rb_node;
773
774 while (n) {
775 osd = rb_entry(n, struct ceph_osd, o_node);
776 if (o < osd->o_osd)
777 n = n->rb_left;
778 else if (o > osd->o_osd)
779 n = n->rb_right;
780 else
781 return osd;
782 }
783 return NULL;
784}
785
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800786static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
787{
788 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700789 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800790}
791
792static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
793{
794 cancel_delayed_work(&osdc->timeout_work);
795}
Sage Weilf24e9982009-10-06 11:31:10 -0700796
797/*
798 * Register request, assign tid. If this is the first request, set up
799 * the timeout event.
800 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700801static void __register_request(struct ceph_osd_client *osdc,
802 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700803{
Sage Weilf24e9982009-10-06 11:31:10 -0700804 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800805 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800806 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700807
Sage Weil77f38e02011-04-06 09:09:16 -0700808 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700809 __insert_request(osdc, req);
810 ceph_osdc_get_request(req);
811 osdc->num_requests++;
812
Sage Weilf24e9982009-10-06 11:31:10 -0700813 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800814 dout(" first request, scheduling timeout\n");
815 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700816 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700817}
818
819static void register_request(struct ceph_osd_client *osdc,
820 struct ceph_osd_request *req)
821{
822 mutex_lock(&osdc->request_mutex);
823 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700824 mutex_unlock(&osdc->request_mutex);
825}
826
827/*
828 * called under osdc->request_mutex
829 */
830static void __unregister_request(struct ceph_osd_client *osdc,
831 struct ceph_osd_request *req)
832{
833 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
834 rb_erase(&req->r_node, &osdc->requests);
835 osdc->num_requests--;
836
Sage Weil0ba64782009-10-08 16:57:16 -0700837 if (req->r_osd) {
838 /* make sure the original request isn't in flight. */
839 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
840
841 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700842 if (list_empty(&req->r_osd->o_requests) &&
843 list_empty(&req->r_osd->o_linger_requests)) {
844 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800845 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700846 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700847 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700848 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700849 }
Sage Weilf24e9982009-10-06 11:31:10 -0700850
851 ceph_osdc_put_request(req);
852
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800853 list_del_init(&req->r_req_lru_item);
854 if (osdc->num_requests == 0) {
855 dout(" no requests, canceling timeout\n");
856 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700857 }
858}
859
860/*
861 * Cancel a previously queued request message
862 */
863static void __cancel_request(struct ceph_osd_request *req)
864{
Sage Weil6bc18872010-09-27 10:18:52 -0700865 if (req->r_sent && req->r_osd) {
Sage Weilf24e9982009-10-06 11:31:10 -0700866 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
867 req->r_sent = 0;
868 }
869}
870
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700871static void __register_linger_request(struct ceph_osd_client *osdc,
872 struct ceph_osd_request *req)
873{
874 dout("__register_linger_request %p\n", req);
875 list_add_tail(&req->r_linger_item, &osdc->req_linger);
876 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
877}
878
879static void __unregister_linger_request(struct ceph_osd_client *osdc,
880 struct ceph_osd_request *req)
881{
882 dout("__unregister_linger_request %p\n", req);
883 if (req->r_osd) {
884 list_del_init(&req->r_linger_item);
885 list_del_init(&req->r_linger_osd);
886
887 if (list_empty(&req->r_osd->o_requests) &&
888 list_empty(&req->r_osd->o_linger_requests)) {
889 dout("moving osd to %p lru\n", req->r_osd);
890 __move_osd_to_lru(osdc, req->r_osd);
891 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700892 if (list_empty(&req->r_osd_item))
893 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700894 }
895}
896
897void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
898 struct ceph_osd_request *req)
899{
900 mutex_lock(&osdc->request_mutex);
901 if (req->r_linger) {
902 __unregister_linger_request(osdc, req);
903 ceph_osdc_put_request(req);
904 }
905 mutex_unlock(&osdc->request_mutex);
906}
907EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
908
909void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
910 struct ceph_osd_request *req)
911{
912 if (!req->r_linger) {
913 dout("set_request_linger %p\n", req);
914 req->r_linger = 1;
915 /*
916 * caller is now responsible for calling
917 * unregister_linger_request
918 */
919 ceph_osdc_get_request(req);
920 }
921}
922EXPORT_SYMBOL(ceph_osdc_set_request_linger);
923
Sage Weilf24e9982009-10-06 11:31:10 -0700924/*
925 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
926 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300927 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800928 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700929 *
930 * Return 0 if unchanged, 1 if changed, or negative on error.
931 *
932 * Caller should hold map_sem for read and request_mutex.
933 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800934static int __map_request(struct ceph_osd_client *osdc,
935 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700936{
937 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800938 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700939 int acting[CEPH_PG_MAX_SIZE];
940 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700941 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700942
Sage Weil6f6c7002011-01-17 20:34:08 -0800943 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700944 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
945 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800946 if (err) {
947 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700948 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800949 }
Sage Weil51042122009-11-04 11:39:12 -0800950 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800951 req->r_pgid = pgid;
952
Sage Weild85b7052010-05-10 10:24:48 -0700953 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
954 if (err > 0) {
955 o = acting[0];
956 num = err;
957 }
Sage Weilf24e9982009-10-06 11:31:10 -0700958
959 if ((req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700960 req->r_sent >= req->r_osd->o_incarnation &&
961 req->r_num_pg_osds == num &&
962 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700963 (req->r_osd == NULL && o == -1))
964 return 0; /* no change */
965
Sage Weil6f6c7002011-01-17 20:34:08 -0800966 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800967 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700968 req->r_osd ? req->r_osd->o_osd : -1);
969
Sage Weild85b7052010-05-10 10:24:48 -0700970 /* record full pg acting set */
971 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
972 req->r_num_pg_osds = num;
973
Sage Weilf24e9982009-10-06 11:31:10 -0700974 if (req->r_osd) {
975 __cancel_request(req);
976 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700977 req->r_osd = NULL;
978 }
979
980 req->r_osd = __lookup_osd(osdc, o);
981 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800982 err = -ENOMEM;
983 req->r_osd = create_osd(osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -0800984 if (!req->r_osd) {
985 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800986 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -0800987 }
Sage Weilf24e9982009-10-06 11:31:10 -0700988
Sage Weil6f6c7002011-01-17 20:34:08 -0800989 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -0700990 req->r_osd->o_osd = o;
991 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
992 __insert_osd(osdc, req->r_osd);
993
994 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
995 }
996
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800997 if (req->r_osd) {
998 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700999 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001000 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1001 } else {
1002 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001003 }
Sage Weild85b7052010-05-10 10:24:48 -07001004 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001005
1006out:
Sage Weilf24e9982009-10-06 11:31:10 -07001007 return err;
1008}
1009
1010/*
1011 * caller should hold map_sem (for read) and request_mutex
1012 */
1013static int __send_request(struct ceph_osd_client *osdc,
1014 struct ceph_osd_request *req)
1015{
1016 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001017
1018 dout("send_request %p tid %llu to osd%d flags %d\n",
1019 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1020
1021 reqhead = req->r_request->front.iov_base;
1022 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1023 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1024 reqhead->reassert_version = req->r_reassert_version;
1025
Sage Weil3dd72fc2010-03-22 14:42:30 -07001026 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001027 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001028
1029 ceph_msg_get(req->r_request); /* send consumes a ref */
1030 ceph_con_send(&req->r_osd->o_con, req->r_request);
1031 req->r_sent = req->r_osd->o_incarnation;
1032 return 0;
1033}
1034
1035/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001036 * Send any requests in the queue (req_unsent).
1037 */
1038static void send_queued(struct ceph_osd_client *osdc)
1039{
1040 struct ceph_osd_request *req, *tmp;
1041
1042 dout("send_queued\n");
1043 mutex_lock(&osdc->request_mutex);
1044 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1045 __send_request(osdc, req);
1046 }
1047 mutex_unlock(&osdc->request_mutex);
1048}
1049
1050/*
Sage Weilf24e9982009-10-06 11:31:10 -07001051 * Timeout callback, called every N seconds when 1 or more osd
1052 * requests has been active for more than N seconds. When this
1053 * happens, we ping all OSDs with requests who have timed out to
1054 * ensure any communications channel reset is detected. Reset the
1055 * request timeouts another N seconds in the future as we go.
1056 * Reschedule the timeout event another N seconds in future (unless
1057 * there are no open requests).
1058 */
1059static void handle_timeout(struct work_struct *work)
1060{
1061 struct ceph_osd_client *osdc =
1062 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001063 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001064 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001065 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001066 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001067 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001068 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001069 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001070 dout("timeout\n");
1071 down_read(&osdc->map_sem);
1072
1073 ceph_monc_request_next_osdmap(&osdc->client->monc);
1074
1075 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001076
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001077 /*
1078 * reset osds that appear to be _really_ unresponsive. this
1079 * is a failsafe measure.. we really shouldn't be getting to
1080 * this point if the system is working properly. the monitors
1081 * should mark the osd as failed and we should find out about
1082 * it from an updated osd map.
1083 */
Sage Weilf26e6812010-04-21 11:09:38 -07001084 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001085 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1086 r_req_lru_item);
1087
Sage Weil3dd72fc2010-03-22 14:42:30 -07001088 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001089 break;
1090
Sage Weil3dd72fc2010-03-22 14:42:30 -07001091 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001092 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001093 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001094
1095 osd = req->r_osd;
1096 BUG_ON(!osd);
1097 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1098 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001099 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001100 }
1101
1102 /*
1103 * ping osds that are a bit slow. this ensures that if there
1104 * is a break in the TCP connection we will notice, and reopen
1105 * a connection with that osd (from the fault callback).
1106 */
1107 INIT_LIST_HEAD(&slow_osds);
1108 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001109 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001110 break;
1111
1112 osd = req->r_osd;
1113 BUG_ON(!osd);
1114 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001115 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001116 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1117 }
1118 while (!list_empty(&slow_osds)) {
1119 osd = list_entry(slow_osds.next, struct ceph_osd,
1120 o_keepalive_item);
1121 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001122 ceph_con_keepalive(&osd->o_con);
1123 }
1124
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001125 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001126 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001127 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001128 up_read(&osdc->map_sem);
1129}
1130
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001131static void handle_osds_timeout(struct work_struct *work)
1132{
1133 struct ceph_osd_client *osdc =
1134 container_of(work, struct ceph_osd_client,
1135 osds_timeout_work.work);
1136 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001137 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001138
1139 dout("osds timeout\n");
1140 down_read(&osdc->map_sem);
1141 remove_old_osds(osdc, 0);
1142 up_read(&osdc->map_sem);
1143
1144 schedule_delayed_work(&osdc->osds_timeout_work,
1145 round_jiffies_relative(delay));
1146}
1147
Sage Weil25845472011-06-03 09:37:09 -07001148static void complete_request(struct ceph_osd_request *req)
1149{
1150 if (req->r_safe_callback)
1151 req->r_safe_callback(req, NULL);
1152 complete_all(&req->r_safe_completion); /* fsync waiter */
1153}
1154
Sage Weilf24e9982009-10-06 11:31:10 -07001155/*
1156 * handle osd op reply. either call the callback if it is specified,
1157 * or do the completion to wake up the waiting thread.
1158 */
Sage Weil350b1c32009-12-22 10:45:45 -08001159static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1160 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001161{
1162 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1163 struct ceph_osd_request *req;
1164 u64 tid;
1165 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001166 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001167
Sage Weil6df058c2009-12-22 11:24:33 -08001168 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001169 if (msg->front.iov_len < sizeof(*rhead))
1170 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001171 numops = le32_to_cpu(rhead->num_ops);
1172 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001173 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001174 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1175 numops * sizeof(struct ceph_osd_op))
1176 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001177 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001178 /* lookup */
1179 mutex_lock(&osdc->request_mutex);
1180 req = __lookup_request(osdc, tid);
1181 if (req == NULL) {
1182 dout("handle_reply tid %llu dne\n", tid);
1183 mutex_unlock(&osdc->request_mutex);
1184 return;
1185 }
1186 ceph_osdc_get_request(req);
1187 flags = le32_to_cpu(rhead->flags);
1188
Sage Weil350b1c32009-12-22 10:45:45 -08001189 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001190 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001191 * avoid a (safe but slower) revoke later.
1192 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001193 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001194 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001195 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -08001196 ceph_con_put(con);
1197 }
1198
Sage Weilf24e9982009-10-06 11:31:10 -07001199 if (!req->r_got_reply) {
1200 unsigned bytes;
1201
1202 req->r_result = le32_to_cpu(rhead->result);
1203 bytes = le32_to_cpu(msg->hdr.data_len);
1204 dout("handle_reply result %d bytes %d\n", req->r_result,
1205 bytes);
1206 if (req->r_result == 0)
1207 req->r_result = bytes;
1208
1209 /* in case this is a write and we need to replay, */
1210 req->r_reassert_version = rhead->reassert_version;
1211
1212 req->r_got_reply = 1;
1213 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1214 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001215 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001216 goto done;
1217 }
1218
1219 dout("handle_reply tid %llu flags %d\n", tid, flags);
1220
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001221 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1222 __register_linger_request(osdc, req);
1223
Sage Weilf24e9982009-10-06 11:31:10 -07001224 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001225 if (result < 0 ||
1226 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001227 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1228 __unregister_request(osdc, req);
1229
1230 mutex_unlock(&osdc->request_mutex);
1231
1232 if (req->r_callback)
1233 req->r_callback(req, msg);
1234 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001235 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001236
Sage Weil25845472011-06-03 09:37:09 -07001237 if (flags & CEPH_OSD_FLAG_ONDISK)
1238 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001239
1240done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001241 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001242 ceph_osdc_put_request(req);
1243 return;
1244
1245bad:
1246 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1247 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1248 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001249 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001250}
1251
Sage Weil6f6c7002011-01-17 20:34:08 -08001252static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001253{
Sage Weilf24e9982009-10-06 11:31:10 -07001254 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001255
Sage Weil6f6c7002011-01-17 20:34:08 -08001256 for (p = rb_first(&osdc->osds); p; p = n) {
1257 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001258
Sage Weil6f6c7002011-01-17 20:34:08 -08001259 n = rb_next(p);
1260 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1261 memcmp(&osd->o_con.peer_addr,
1262 ceph_osd_addr(osdc->osdmap,
1263 osd->o_osd),
1264 sizeof(struct ceph_entity_addr)) != 0)
1265 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001266 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001267}
1268
1269/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001270 * Requeue requests whose mapping to an OSD has changed. If requests map to
1271 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001272 *
1273 * Caller should hold map_sem for read and request_mutex.
1274 */
Sage Weil6f6c7002011-01-17 20:34:08 -08001275static void kick_requests(struct ceph_osd_client *osdc)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001276{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001277 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001278 struct rb_node *p;
1279 int needmap = 0;
1280 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001281
Sage Weil6f6c7002011-01-17 20:34:08 -08001282 dout("kick_requests\n");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001283 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001284 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1285 req = rb_entry(p, struct ceph_osd_request, r_node);
1286 err = __map_request(osdc, req);
1287 if (err < 0)
1288 continue; /* error */
1289 if (req->r_osd == NULL) {
1290 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1291 needmap++; /* request a newer map */
1292 } else if (err > 0) {
1293 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1294 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001295 if (!req->r_linger)
1296 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001297 }
1298 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001299
1300 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1301 r_linger_item) {
1302 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1303
1304 err = __map_request(osdc, req);
1305 if (err == 0)
1306 continue; /* no change and no osd was specified */
1307 if (err < 0)
1308 continue; /* hrm! */
1309 if (req->r_osd == NULL) {
1310 dout("tid %llu maps to no valid osd\n", req->r_tid);
1311 needmap++; /* request a newer map */
1312 continue;
1313 }
1314
1315 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1316 req->r_osd ? req->r_osd->o_osd : -1);
1317 __unregister_linger_request(osdc, req);
1318 __register_request(osdc, req);
1319 }
Sage Weilf24e9982009-10-06 11:31:10 -07001320 mutex_unlock(&osdc->request_mutex);
1321
1322 if (needmap) {
1323 dout("%d requests for down osds, need new map\n", needmap);
1324 ceph_monc_request_next_osdmap(&osdc->client->monc);
1325 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001326}
Sage Weil6f6c7002011-01-17 20:34:08 -08001327
1328
Sage Weilf24e9982009-10-06 11:31:10 -07001329/*
1330 * Process updated osd map.
1331 *
1332 * The message contains any number of incremental and full maps, normally
1333 * indicating some sort of topology change in the cluster. Kick requests
1334 * off to different OSDs as needed.
1335 */
1336void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1337{
1338 void *p, *end, *next;
1339 u32 nr_maps, maplen;
1340 u32 epoch;
1341 struct ceph_osdmap *newmap = NULL, *oldmap;
1342 int err;
1343 struct ceph_fsid fsid;
1344
1345 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1346 p = msg->front.iov_base;
1347 end = p + msg->front.iov_len;
1348
1349 /* verify fsid */
1350 ceph_decode_need(&p, end, sizeof(fsid), bad);
1351 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001352 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1353 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001354
1355 down_write(&osdc->map_sem);
1356
1357 /* incremental maps */
1358 ceph_decode_32_safe(&p, end, nr_maps, bad);
1359 dout(" %d inc maps\n", nr_maps);
1360 while (nr_maps > 0) {
1361 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001362 epoch = ceph_decode_32(&p);
1363 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001364 ceph_decode_need(&p, end, maplen, bad);
1365 next = p + maplen;
1366 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1367 dout("applying incremental map %u len %d\n",
1368 epoch, maplen);
1369 newmap = osdmap_apply_incremental(&p, next,
1370 osdc->osdmap,
1371 osdc->client->msgr);
1372 if (IS_ERR(newmap)) {
1373 err = PTR_ERR(newmap);
1374 goto bad;
1375 }
Sage Weil30dc6382009-12-21 14:49:37 -08001376 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001377 if (newmap != osdc->osdmap) {
1378 ceph_osdmap_destroy(osdc->osdmap);
1379 osdc->osdmap = newmap;
1380 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001381 kick_requests(osdc);
1382 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001383 } else {
1384 dout("ignoring incremental map %u len %d\n",
1385 epoch, maplen);
1386 }
1387 p = next;
1388 nr_maps--;
1389 }
1390 if (newmap)
1391 goto done;
1392
1393 /* full maps */
1394 ceph_decode_32_safe(&p, end, nr_maps, bad);
1395 dout(" %d full maps\n", nr_maps);
1396 while (nr_maps) {
1397 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001398 epoch = ceph_decode_32(&p);
1399 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001400 ceph_decode_need(&p, end, maplen, bad);
1401 if (nr_maps > 1) {
1402 dout("skipping non-latest full map %u len %d\n",
1403 epoch, maplen);
1404 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1405 dout("skipping full map %u len %d, "
1406 "older than our %u\n", epoch, maplen,
1407 osdc->osdmap->epoch);
1408 } else {
1409 dout("taking full map %u len %d\n", epoch, maplen);
1410 newmap = osdmap_decode(&p, p+maplen);
1411 if (IS_ERR(newmap)) {
1412 err = PTR_ERR(newmap);
1413 goto bad;
1414 }
Sage Weil30dc6382009-12-21 14:49:37 -08001415 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001416 oldmap = osdc->osdmap;
1417 osdc->osdmap = newmap;
1418 if (oldmap)
1419 ceph_osdmap_destroy(oldmap);
Sage Weil6f6c7002011-01-17 20:34:08 -08001420 kick_requests(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001421 }
1422 p += maplen;
1423 nr_maps--;
1424 }
1425
1426done:
1427 downgrade_write(&osdc->map_sem);
1428 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001429
1430 /*
1431 * subscribe to subsequent osdmap updates if full to ensure
1432 * we find out when we are no longer full and stop returning
1433 * ENOSPC.
1434 */
1435 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1436 ceph_monc_request_next_osdmap(&osdc->client->monc);
1437
Sage Weil6f6c7002011-01-17 20:34:08 -08001438 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001439 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001440 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001441 return;
1442
1443bad:
1444 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001445 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001446 up_write(&osdc->map_sem);
1447 return;
1448}
1449
Sage Weilf24e9982009-10-06 11:31:10 -07001450/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001451 * watch/notify callback event infrastructure
1452 *
1453 * These callbacks are used both for watch and notify operations.
1454 */
1455static void __release_event(struct kref *kref)
1456{
1457 struct ceph_osd_event *event =
1458 container_of(kref, struct ceph_osd_event, kref);
1459
1460 dout("__release_event %p\n", event);
1461 kfree(event);
1462}
1463
1464static void get_event(struct ceph_osd_event *event)
1465{
1466 kref_get(&event->kref);
1467}
1468
1469void ceph_osdc_put_event(struct ceph_osd_event *event)
1470{
1471 kref_put(&event->kref, __release_event);
1472}
1473EXPORT_SYMBOL(ceph_osdc_put_event);
1474
1475static void __insert_event(struct ceph_osd_client *osdc,
1476 struct ceph_osd_event *new)
1477{
1478 struct rb_node **p = &osdc->event_tree.rb_node;
1479 struct rb_node *parent = NULL;
1480 struct ceph_osd_event *event = NULL;
1481
1482 while (*p) {
1483 parent = *p;
1484 event = rb_entry(parent, struct ceph_osd_event, node);
1485 if (new->cookie < event->cookie)
1486 p = &(*p)->rb_left;
1487 else if (new->cookie > event->cookie)
1488 p = &(*p)->rb_right;
1489 else
1490 BUG();
1491 }
1492
1493 rb_link_node(&new->node, parent, p);
1494 rb_insert_color(&new->node, &osdc->event_tree);
1495}
1496
1497static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1498 u64 cookie)
1499{
1500 struct rb_node **p = &osdc->event_tree.rb_node;
1501 struct rb_node *parent = NULL;
1502 struct ceph_osd_event *event = NULL;
1503
1504 while (*p) {
1505 parent = *p;
1506 event = rb_entry(parent, struct ceph_osd_event, node);
1507 if (cookie < event->cookie)
1508 p = &(*p)->rb_left;
1509 else if (cookie > event->cookie)
1510 p = &(*p)->rb_right;
1511 else
1512 return event;
1513 }
1514 return NULL;
1515}
1516
1517static void __remove_event(struct ceph_osd_event *event)
1518{
1519 struct ceph_osd_client *osdc = event->osdc;
1520
1521 if (!RB_EMPTY_NODE(&event->node)) {
1522 dout("__remove_event removed %p\n", event);
1523 rb_erase(&event->node, &osdc->event_tree);
1524 ceph_osdc_put_event(event);
1525 } else {
1526 dout("__remove_event didn't remove %p\n", event);
1527 }
1528}
1529
1530int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1531 void (*event_cb)(u64, u64, u8, void *),
1532 int one_shot, void *data,
1533 struct ceph_osd_event **pevent)
1534{
1535 struct ceph_osd_event *event;
1536
1537 event = kmalloc(sizeof(*event), GFP_NOIO);
1538 if (!event)
1539 return -ENOMEM;
1540
1541 dout("create_event %p\n", event);
1542 event->cb = event_cb;
1543 event->one_shot = one_shot;
1544 event->data = data;
1545 event->osdc = osdc;
1546 INIT_LIST_HEAD(&event->osd_node);
1547 kref_init(&event->kref); /* one ref for us */
1548 kref_get(&event->kref); /* one ref for the caller */
1549 init_completion(&event->completion);
1550
1551 spin_lock(&osdc->event_lock);
1552 event->cookie = ++osdc->event_count;
1553 __insert_event(osdc, event);
1554 spin_unlock(&osdc->event_lock);
1555
1556 *pevent = event;
1557 return 0;
1558}
1559EXPORT_SYMBOL(ceph_osdc_create_event);
1560
1561void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1562{
1563 struct ceph_osd_client *osdc = event->osdc;
1564
1565 dout("cancel_event %p\n", event);
1566 spin_lock(&osdc->event_lock);
1567 __remove_event(event);
1568 spin_unlock(&osdc->event_lock);
1569 ceph_osdc_put_event(event); /* caller's */
1570}
1571EXPORT_SYMBOL(ceph_osdc_cancel_event);
1572
1573
1574static void do_event_work(struct work_struct *work)
1575{
1576 struct ceph_osd_event_work *event_work =
1577 container_of(work, struct ceph_osd_event_work, work);
1578 struct ceph_osd_event *event = event_work->event;
1579 u64 ver = event_work->ver;
1580 u64 notify_id = event_work->notify_id;
1581 u8 opcode = event_work->opcode;
1582
1583 dout("do_event_work completing %p\n", event);
1584 event->cb(ver, notify_id, opcode, event->data);
1585 complete(&event->completion);
1586 dout("do_event_work completed %p\n", event);
1587 ceph_osdc_put_event(event);
1588 kfree(event_work);
1589}
1590
1591
1592/*
1593 * Process osd watch notifications
1594 */
1595void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1596{
1597 void *p, *end;
1598 u8 proto_ver;
1599 u64 cookie, ver, notify_id;
1600 u8 opcode;
1601 struct ceph_osd_event *event;
1602 struct ceph_osd_event_work *event_work;
1603
1604 p = msg->front.iov_base;
1605 end = p + msg->front.iov_len;
1606
1607 ceph_decode_8_safe(&p, end, proto_ver, bad);
1608 ceph_decode_8_safe(&p, end, opcode, bad);
1609 ceph_decode_64_safe(&p, end, cookie, bad);
1610 ceph_decode_64_safe(&p, end, ver, bad);
1611 ceph_decode_64_safe(&p, end, notify_id, bad);
1612
1613 spin_lock(&osdc->event_lock);
1614 event = __find_event(osdc, cookie);
1615 if (event) {
1616 get_event(event);
1617 if (event->one_shot)
1618 __remove_event(event);
1619 }
1620 spin_unlock(&osdc->event_lock);
1621 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1622 cookie, ver, event);
1623 if (event) {
1624 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001625 if (!event_work) {
1626 dout("ERROR: could not allocate event_work\n");
1627 goto done_err;
1628 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001629 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001630 event_work->event = event;
1631 event_work->ver = ver;
1632 event_work->notify_id = notify_id;
1633 event_work->opcode = opcode;
1634 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1635 dout("WARNING: failed to queue notify event work\n");
1636 goto done_err;
1637 }
1638 }
1639
1640 return;
1641
1642done_err:
1643 complete(&event->completion);
1644 ceph_osdc_put_event(event);
1645 return;
1646
1647bad:
1648 pr_err("osdc handle_watch_notify corrupt msg\n");
1649 return;
1650}
1651
1652int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1653{
1654 int err;
1655
1656 dout("wait_event %p\n", event);
1657 err = wait_for_completion_interruptible_timeout(&event->completion,
1658 timeout * HZ);
1659 ceph_osdc_put_event(event);
1660 if (err > 0)
1661 err = 0;
1662 dout("wait_event %p returns %d\n", event, err);
1663 return err;
1664}
1665EXPORT_SYMBOL(ceph_osdc_wait_event);
1666
1667/*
Sage Weilf24e9982009-10-06 11:31:10 -07001668 * Register request, send initial attempt.
1669 */
1670int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1671 struct ceph_osd_request *req,
1672 bool nofail)
1673{
Sage Weilc1ea8822009-10-08 16:55:47 -07001674 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001675
1676 req->r_request->pages = req->r_pages;
1677 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001678#ifdef CONFIG_BLOCK
1679 req->r_request->bio = req->r_bio;
1680#endif
1681 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001682
1683 register_request(osdc, req);
1684
1685 down_read(&osdc->map_sem);
1686 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001687 /*
1688 * a racing kick_requests() may have sent the message for us
1689 * while we dropped request_mutex above, so only send now if
1690 * the request still han't been touched yet.
1691 */
1692 if (req->r_sent == 0) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001693 rc = __map_request(osdc, req);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001694 if (rc < 0) {
1695 if (nofail) {
1696 dout("osdc_start_request failed map, "
1697 " will retry %lld\n", req->r_tid);
1698 rc = 0;
1699 }
Dan Carpenter234af262011-03-29 06:25:59 +03001700 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001701 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001702 if (req->r_osd == NULL) {
1703 dout("send_request %p no up osds in pg\n", req);
1704 ceph_monc_request_next_osdmap(&osdc->client->monc);
1705 } else {
1706 rc = __send_request(osdc, req);
1707 if (rc) {
1708 if (nofail) {
1709 dout("osdc_start_request failed send, "
1710 " will retry %lld\n", req->r_tid);
1711 rc = 0;
1712 } else {
1713 __unregister_request(osdc, req);
1714 }
Sage Weilc1ea8822009-10-08 16:55:47 -07001715 }
Sage Weilf24e9982009-10-06 11:31:10 -07001716 }
1717 }
Dan Carpenter234af262011-03-29 06:25:59 +03001718
1719out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001720 mutex_unlock(&osdc->request_mutex);
1721 up_read(&osdc->map_sem);
1722 return rc;
1723}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001724EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001725
1726/*
1727 * wait for a request to complete
1728 */
1729int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1730 struct ceph_osd_request *req)
1731{
1732 int rc;
1733
1734 rc = wait_for_completion_interruptible(&req->r_completion);
1735 if (rc < 0) {
1736 mutex_lock(&osdc->request_mutex);
1737 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001738 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001739 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001740 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001741 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001742 return rc;
1743 }
1744
1745 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1746 return req->r_result;
1747}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001748EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001749
1750/*
1751 * sync - wait for all in-flight requests to flush. avoid starvation.
1752 */
1753void ceph_osdc_sync(struct ceph_osd_client *osdc)
1754{
1755 struct ceph_osd_request *req;
1756 u64 last_tid, next_tid = 0;
1757
1758 mutex_lock(&osdc->request_mutex);
1759 last_tid = osdc->last_tid;
1760 while (1) {
1761 req = __lookup_request_ge(osdc, next_tid);
1762 if (!req)
1763 break;
1764 if (req->r_tid > last_tid)
1765 break;
1766
1767 next_tid = req->r_tid + 1;
1768 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1769 continue;
1770
1771 ceph_osdc_get_request(req);
1772 mutex_unlock(&osdc->request_mutex);
1773 dout("sync waiting on tid %llu (last is %llu)\n",
1774 req->r_tid, last_tid);
1775 wait_for_completion(&req->r_safe_completion);
1776 mutex_lock(&osdc->request_mutex);
1777 ceph_osdc_put_request(req);
1778 }
1779 mutex_unlock(&osdc->request_mutex);
1780 dout("sync done (thru tid %llu)\n", last_tid);
1781}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001782EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001783
1784/*
1785 * init, shutdown
1786 */
1787int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1788{
1789 int err;
1790
1791 dout("init\n");
1792 osdc->client = client;
1793 osdc->osdmap = NULL;
1794 init_rwsem(&osdc->map_sem);
1795 init_completion(&osdc->map_waiters);
1796 osdc->last_requested_map = 0;
1797 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001798 osdc->last_tid = 0;
1799 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001800 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001801 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001802 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001803 INIT_LIST_HEAD(&osdc->req_unsent);
1804 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001805 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001806 osdc->num_requests = 0;
1807 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001808 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001809 spin_lock_init(&osdc->event_lock);
1810 osdc->event_tree = RB_ROOT;
1811 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001812
1813 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001814 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001815
Sage Weil5f44f142009-11-18 14:52:18 -08001816 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001817 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1818 sizeof(struct ceph_osd_request));
1819 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001820 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001821
Sage Weil4f482802010-04-24 09:56:35 -07001822 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1823 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001824 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001825 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001826 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001827 OSD_OPREPLY_FRONT_LEN, 10, true,
1828 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001829 if (err < 0)
1830 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001831
1832 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1833 if (IS_ERR(osdc->notify_wq)) {
1834 err = PTR_ERR(osdc->notify_wq);
1835 osdc->notify_wq = NULL;
1836 goto out_msgpool;
1837 }
Sage Weilf24e9982009-10-06 11:31:10 -07001838 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001839
Sage Weilc16e7862010-03-01 13:02:00 -08001840out_msgpool:
1841 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001842out_mempool:
1843 mempool_destroy(osdc->req_mempool);
1844out:
1845 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001846}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001847EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001848
1849void ceph_osdc_stop(struct ceph_osd_client *osdc)
1850{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001851 flush_workqueue(osdc->notify_wq);
1852 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001853 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001854 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001855 if (osdc->osdmap) {
1856 ceph_osdmap_destroy(osdc->osdmap);
1857 osdc->osdmap = NULL;
1858 }
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001859 remove_old_osds(osdc, 1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001860 WARN_ON(!RB_EMPTY_ROOT(&osdc->osds));
Sage Weilf24e9982009-10-06 11:31:10 -07001861 mempool_destroy(osdc->req_mempool);
1862 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001863 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001864}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001865EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001866
1867/*
1868 * Read some contiguous pages. If we cross a stripe boundary, shorten
1869 * *plen. Return number of bytes read, or error.
1870 */
1871int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1872 struct ceph_vino vino, struct ceph_file_layout *layout,
1873 u64 off, u64 *plen,
1874 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001875 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001876{
1877 struct ceph_osd_request *req;
1878 int rc = 0;
1879
1880 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1881 vino.snap, off, *plen);
1882 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1883 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1884 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001885 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001886 if (!req)
1887 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001888
1889 /* it may be a short read due to an object boundary */
1890 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001891
Sage Weilb7495fc2010-11-09 12:43:12 -08001892 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1893 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001894
1895 rc = ceph_osdc_start_request(osdc, req, false);
1896 if (!rc)
1897 rc = ceph_osdc_wait_request(osdc, req);
1898
1899 ceph_osdc_put_request(req);
1900 dout("readpages result %d\n", rc);
1901 return rc;
1902}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001903EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001904
1905/*
1906 * do a synchronous write on N pages
1907 */
1908int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1909 struct ceph_file_layout *layout,
1910 struct ceph_snap_context *snapc,
1911 u64 off, u64 len,
1912 u32 truncate_seq, u64 truncate_size,
1913 struct timespec *mtime,
1914 struct page **pages, int num_pages,
1915 int flags, int do_sync, bool nofail)
1916{
1917 struct ceph_osd_request *req;
1918 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001919 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001920
1921 BUG_ON(vino.snap != CEPH_NOSNAP);
1922 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1923 CEPH_OSD_OP_WRITE,
1924 flags | CEPH_OSD_FLAG_ONDISK |
1925 CEPH_OSD_FLAG_WRITE,
1926 snapc, do_sync,
1927 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001928 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001929 if (!req)
1930 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001931
1932 /* it may be a short write due to an object boundary */
1933 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001934 dout("writepages %llu~%llu (%d pages)\n", off, len,
1935 req->r_num_pages);
1936
1937 rc = ceph_osdc_start_request(osdc, req, nofail);
1938 if (!rc)
1939 rc = ceph_osdc_wait_request(osdc, req);
1940
1941 ceph_osdc_put_request(req);
1942 if (rc == 0)
1943 rc = len;
1944 dout("writepages result %d\n", rc);
1945 return rc;
1946}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001947EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001948
1949/*
1950 * handle incoming message
1951 */
1952static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1953{
1954 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001955 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001956 int type = le16_to_cpu(msg->hdr.type);
1957
1958 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001959 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001960 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001961
1962 switch (type) {
1963 case CEPH_MSG_OSD_MAP:
1964 ceph_osdc_handle_map(osdc, msg);
1965 break;
1966 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001967 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001968 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001969 case CEPH_MSG_WATCH_NOTIFY:
1970 handle_watch_notify(osdc, msg);
1971 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001972
1973 default:
1974 pr_err("received unknown message type %d %s\n", type,
1975 ceph_msg_type_name(type));
1976 }
Sage Weil4a32f932010-06-13 10:27:53 -07001977out:
Sage Weilf24e9982009-10-06 11:31:10 -07001978 ceph_msg_put(msg);
1979}
1980
Sage Weil5b3a4db2010-02-19 21:43:23 -08001981/*
Sage Weil21b667f2010-03-04 10:22:59 -08001982 * lookup and return message for incoming reply. set up reply message
1983 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001984 */
1985static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001986 struct ceph_msg_header *hdr,
1987 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001988{
1989 struct ceph_osd *osd = con->private;
1990 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001991 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001992 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001993 int front = le32_to_cpu(hdr->front_len);
1994 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001995 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07001996
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001997 tid = le64_to_cpu(hdr->tid);
1998 mutex_lock(&osdc->request_mutex);
1999 req = __lookup_request(osdc, tid);
2000 if (!req) {
2001 *skip = 1;
2002 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08002003 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08002004 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002005 goto out;
2006 }
Sage Weilc16e7862010-03-01 13:02:00 -08002007
2008 if (req->r_con_filling_msg) {
2009 dout("get_reply revoking msg %p from old con %p\n",
2010 req->r_reply, req->r_con_filling_msg);
2011 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2012 ceph_con_put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002013 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002014 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002015
Sage Weilc16e7862010-03-01 13:02:00 -08002016 if (front > req->r_reply->front.iov_len) {
2017 pr_warning("get_reply front %d > preallocated %d\n",
2018 front, (int)req->r_reply->front.iov_len);
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002019 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS);
Sage Weila79832f2010-04-01 16:06:19 -07002020 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002021 goto out;
2022 ceph_msg_put(req->r_reply);
2023 req->r_reply = m;
2024 }
2025 m = ceph_msg_get(req->r_reply);
2026
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002027 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002028 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002029
2030 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002031 pr_warning("tid %lld reply has %d bytes %d pages, we"
2032 " had only %d pages ready\n", tid, data_len,
2033 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002034 *skip = 1;
2035 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002036 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002037 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002038 }
Sage Weil21b667f2010-03-04 10:22:59 -08002039 m->pages = req->r_pages;
2040 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002041 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002042#ifdef CONFIG_BLOCK
2043 m->bio = req->r_bio;
2044#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002045 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002046 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002047 req->r_con_filling_msg = ceph_con_get(con);
2048 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002049
2050out:
2051 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002052 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002053
2054}
2055
2056static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2057 struct ceph_msg_header *hdr,
2058 int *skip)
2059{
2060 struct ceph_osd *osd = con->private;
2061 int type = le16_to_cpu(hdr->type);
2062 int front = le32_to_cpu(hdr->front_len);
2063
2064 switch (type) {
2065 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002066 case CEPH_MSG_WATCH_NOTIFY:
Yehuda Sadeh34d23762010-04-06 14:33:58 -07002067 return ceph_msg_new(type, front, GFP_NOFS);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002068 case CEPH_MSG_OSD_OPREPLY:
2069 return get_reply(con, hdr, skip);
2070 default:
2071 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2072 osd->o_osd);
2073 *skip = 1;
2074 return NULL;
2075 }
Sage Weilf24e9982009-10-06 11:31:10 -07002076}
2077
2078/*
2079 * Wrappers to refcount containing ceph_osd struct
2080 */
2081static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2082{
2083 struct ceph_osd *osd = con->private;
2084 if (get_osd(osd))
2085 return con;
2086 return NULL;
2087}
2088
2089static void put_osd_con(struct ceph_connection *con)
2090{
2091 struct ceph_osd *osd = con->private;
2092 put_osd(osd);
2093}
2094
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002095/*
2096 * authentication
2097 */
2098static int get_authorizer(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002099 void **buf, int *len, int *proto,
2100 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002101{
2102 struct ceph_osd *o = con->private;
2103 struct ceph_osd_client *osdc = o->o_osdc;
2104 struct ceph_auth_client *ac = osdc->client->monc.auth;
2105 int ret = 0;
2106
2107 if (force_new && o->o_authorizer) {
2108 ac->ops->destroy_authorizer(ac, o->o_authorizer);
2109 o->o_authorizer = NULL;
2110 }
2111 if (o->o_authorizer == NULL) {
2112 ret = ac->ops->create_authorizer(
2113 ac, CEPH_ENTITY_TYPE_OSD,
2114 &o->o_authorizer,
2115 &o->o_authorizer_buf,
2116 &o->o_authorizer_buf_len,
2117 &o->o_authorizer_reply_buf,
2118 &o->o_authorizer_reply_buf_len);
2119 if (ret)
Sage Weil213c99e2010-08-03 10:25:11 -07002120 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002121 }
2122
2123 *proto = ac->protocol;
2124 *buf = o->o_authorizer_buf;
2125 *len = o->o_authorizer_buf_len;
2126 *reply_buf = o->o_authorizer_reply_buf;
2127 *reply_len = o->o_authorizer_reply_buf_len;
2128 return 0;
2129}
2130
2131
2132static int verify_authorizer_reply(struct ceph_connection *con, int len)
2133{
2134 struct ceph_osd *o = con->private;
2135 struct ceph_osd_client *osdc = o->o_osdc;
2136 struct ceph_auth_client *ac = osdc->client->monc.auth;
2137
2138 return ac->ops->verify_authorizer_reply(ac, o->o_authorizer, len);
2139}
2140
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002141static int invalidate_authorizer(struct ceph_connection *con)
2142{
2143 struct ceph_osd *o = con->private;
2144 struct ceph_osd_client *osdc = o->o_osdc;
2145 struct ceph_auth_client *ac = osdc->client->monc.auth;
2146
2147 if (ac->ops->invalidate_authorizer)
2148 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2149
2150 return ceph_monc_validate_auth(&osdc->client->monc);
2151}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002152
Tobias Klauser9e327892010-05-20 10:40:19 +02002153static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002154 .get = get_osd_con,
2155 .put = put_osd_con,
2156 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002157 .get_authorizer = get_authorizer,
2158 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002159 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002160 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002161 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002162};