blob: f640bdf027e79f868054f5ce534810aba682a2cb [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);
Sage Weil56e925b2012-01-03 12:34:34 -080032static void __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);
Sage Weil935b6392011-09-16 11:13:17 -0700220 INIT_LIST_HEAD(&req->r_req_lru_item);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700221 req->r_flags = flags;
222
223 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
224
225 /* create reply message */
226 if (use_mempool)
227 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
228 else
229 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700230 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700231 if (!msg) {
232 ceph_osdc_put_request(req);
233 return NULL;
234 }
235 req->r_reply = msg;
236
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700237 /* allocate space for the trailing data */
238 if (needs_trail) {
239 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
240 if (!req->r_trail) {
241 ceph_osdc_put_request(req);
242 return NULL;
243 }
244 ceph_pagelist_init(req->r_trail);
245 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700246 /* create request message; allow space for oid */
Stratos Psomadakis224736d2011-11-10 15:45:37 +0200247 msg_size += MAX_OBJ_NAME_SIZE;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700248 if (snapc)
249 msg_size += sizeof(u64) * snapc->num_snaps;
250 if (use_mempool)
251 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
252 else
Sage Weilb61c2762011-08-09 15:03:46 -0700253 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700254 if (!msg) {
255 ceph_osdc_put_request(req);
256 return NULL;
257 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700258
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700259 msg->hdr.type = cpu_to_le16(CEPH_MSG_OSD_OP);
260 memset(msg->front.iov_base, 0, msg->front.iov_len);
261
262 req->r_request = msg;
263 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700264#ifdef CONFIG_BLOCK
265 if (bio) {
266 req->r_bio = bio;
267 bio_get(req->r_bio);
268 }
269#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700270
271 return req;
272}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700273EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700274
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700275static void osd_req_encode_op(struct ceph_osd_request *req,
276 struct ceph_osd_op *dst,
277 struct ceph_osd_req_op *src)
278{
279 dst->op = cpu_to_le16(src->op);
280
Alex Elder065a68f2012-04-20 15:49:43 -0500281 switch (src->op) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700282 case CEPH_OSD_OP_READ:
283 case CEPH_OSD_OP_WRITE:
284 dst->extent.offset =
285 cpu_to_le64(src->extent.offset);
286 dst->extent.length =
287 cpu_to_le64(src->extent.length);
288 dst->extent.truncate_size =
289 cpu_to_le64(src->extent.truncate_size);
290 dst->extent.truncate_seq =
291 cpu_to_le32(src->extent.truncate_seq);
292 break;
293
294 case CEPH_OSD_OP_GETXATTR:
295 case CEPH_OSD_OP_SETXATTR:
296 case CEPH_OSD_OP_CMPXATTR:
297 BUG_ON(!req->r_trail);
298
299 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
300 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
301 dst->xattr.cmp_op = src->xattr.cmp_op;
302 dst->xattr.cmp_mode = src->xattr.cmp_mode;
303 ceph_pagelist_append(req->r_trail, src->xattr.name,
304 src->xattr.name_len);
305 ceph_pagelist_append(req->r_trail, src->xattr.val,
306 src->xattr.value_len);
307 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700308 case CEPH_OSD_OP_CALL:
309 BUG_ON(!req->r_trail);
310
311 dst->cls.class_len = src->cls.class_len;
312 dst->cls.method_len = src->cls.method_len;
313 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
314
315 ceph_pagelist_append(req->r_trail, src->cls.class_name,
316 src->cls.class_len);
317 ceph_pagelist_append(req->r_trail, src->cls.method_name,
318 src->cls.method_len);
319 ceph_pagelist_append(req->r_trail, src->cls.indata,
320 src->cls.indata_len);
321 break;
322 case CEPH_OSD_OP_ROLLBACK:
323 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
324 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700325 case CEPH_OSD_OP_STARTSYNC:
326 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700327 case CEPH_OSD_OP_NOTIFY:
328 {
329 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
330 __le32 timeout = cpu_to_le32(src->watch.timeout);
331
332 BUG_ON(!req->r_trail);
333
334 ceph_pagelist_append(req->r_trail,
335 &prot_ver, sizeof(prot_ver));
336 ceph_pagelist_append(req->r_trail,
337 &timeout, sizeof(timeout));
338 }
339 case CEPH_OSD_OP_NOTIFY_ACK:
340 case CEPH_OSD_OP_WATCH:
341 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
342 dst->watch.ver = cpu_to_le64(src->watch.ver);
343 dst->watch.flag = src->watch.flag;
344 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700345 default:
346 pr_err("unrecognized osd opcode %d\n", dst->op);
347 WARN_ON(1);
348 break;
349 }
350 dst->payload_len = cpu_to_le32(src->payload_len);
351}
352
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700353/*
354 * build new request AND message
355 *
356 */
357void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700358 u64 off, u64 *plen,
359 struct ceph_osd_req_op *src_ops,
360 struct ceph_snap_context *snapc,
361 struct timespec *mtime,
362 const char *oid,
363 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700364{
365 struct ceph_msg *msg = req->r_request;
366 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700367 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700368 struct ceph_osd_op *op;
369 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700370 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700371 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700372 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700373 u64 data_len = 0;
374 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375
376 head = msg->front.iov_base;
377 op = (void *)(head + 1);
378 p = (void *)(op + num_op);
379
380 req->r_snapc = ceph_get_snap_context(snapc);
381
382 head->client_inc = cpu_to_le32(1); /* always, for now. */
383 head->flags = cpu_to_le32(flags);
384 if (flags & CEPH_OSD_FLAG_WRITE)
385 ceph_encode_timespec(&head->mtime, mtime);
386 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700388
389 /* fill in oid */
390 head->object_len = cpu_to_le32(oid_len);
391 memcpy(p, oid, oid_len);
392 p += oid_len;
393
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700394 src_op = src_ops;
395 while (src_op->op) {
396 osd_req_encode_op(req, op, src_op);
397 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700398 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700399 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700400
401 if (req->r_trail)
402 data_len += req->r_trail->length;
403
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700404 if (snapc) {
405 head->snap_seq = cpu_to_le64(snapc->seq);
406 head->num_snaps = cpu_to_le32(snapc->num_snaps);
407 for (i = 0; i < snapc->num_snaps; i++) {
408 put_unaligned_le64(snapc->snaps[i], p);
409 p += sizeof(u64);
410 }
411 }
412
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700413 if (flags & CEPH_OSD_FLAG_WRITE) {
414 req->r_request->hdr.data_off = cpu_to_le16(off);
415 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
416 } else if (data_len) {
417 req->r_request->hdr.data_off = 0;
418 req->r_request->hdr.data_len = cpu_to_le32(data_len);
419 }
420
Sage Weilc5c6b192010-11-09 12:40:00 -0800421 req->r_request->page_alignment = req->r_page_alignment;
422
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700423 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
424 msg_size = p - msg->front.iov_base;
425 msg->front.iov_len = msg_size;
426 msg->hdr.front_len = cpu_to_le32(msg_size);
427 return;
428}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700429EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430
Sage Weilf24e9982009-10-06 11:31:10 -0700431/*
432 * build new request AND message, calculate layout, and adjust file
433 * extent as needed.
434 *
435 * if the file was recently truncated, we include information about its
436 * old and new size so that the object can be updated appropriately. (we
437 * avoid synchronously deleting truncated objects because it's slow.)
438 *
439 * if @do_sync, include a 'startsync' command so that the osd will flush
440 * data quickly.
441 */
442struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
443 struct ceph_file_layout *layout,
444 struct ceph_vino vino,
445 u64 off, u64 *plen,
446 int opcode, int flags,
447 struct ceph_snap_context *snapc,
448 int do_sync,
449 u32 truncate_seq,
450 u64 truncate_size,
451 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800452 bool use_mempool, int num_reply,
453 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700454{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700455 struct ceph_osd_req_op ops[3];
456 struct ceph_osd_request *req;
457
458 ops[0].op = opcode;
459 ops[0].extent.truncate_seq = truncate_seq;
460 ops[0].extent.truncate_size = truncate_size;
461 ops[0].payload_len = 0;
462
463 if (do_sync) {
464 ops[1].op = CEPH_OSD_OP_STARTSYNC;
465 ops[1].payload_len = 0;
466 ops[2].op = 0;
467 } else
468 ops[1].op = 0;
469
470 req = ceph_osdc_alloc_request(osdc, flags,
471 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700472 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700473 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700474 if (!req)
475 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700476
477 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700478 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700479 req->r_file_layout = *layout; /* keep a copy */
480
Sage Weil9bb0ce22011-06-13 16:20:18 -0700481 /* in case it differs from natural (file) alignment that
482 calc_layout filled in for us */
483 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800484 req->r_page_alignment = page_align;
485
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700486 ceph_osdc_build_request(req, off, plen, ops,
487 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700488 mtime,
489 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700490
Sage Weilf24e9982009-10-06 11:31:10 -0700491 return req;
492}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700493EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700494
495/*
496 * We keep osd requests in an rbtree, sorted by ->r_tid.
497 */
498static void __insert_request(struct ceph_osd_client *osdc,
499 struct ceph_osd_request *new)
500{
501 struct rb_node **p = &osdc->requests.rb_node;
502 struct rb_node *parent = NULL;
503 struct ceph_osd_request *req = NULL;
504
505 while (*p) {
506 parent = *p;
507 req = rb_entry(parent, struct ceph_osd_request, r_node);
508 if (new->r_tid < req->r_tid)
509 p = &(*p)->rb_left;
510 else if (new->r_tid > req->r_tid)
511 p = &(*p)->rb_right;
512 else
513 BUG();
514 }
515
516 rb_link_node(&new->r_node, parent, p);
517 rb_insert_color(&new->r_node, &osdc->requests);
518}
519
520static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
521 u64 tid)
522{
523 struct ceph_osd_request *req;
524 struct rb_node *n = osdc->requests.rb_node;
525
526 while (n) {
527 req = rb_entry(n, struct ceph_osd_request, r_node);
528 if (tid < req->r_tid)
529 n = n->rb_left;
530 else if (tid > req->r_tid)
531 n = n->rb_right;
532 else
533 return req;
534 }
535 return NULL;
536}
537
538static struct ceph_osd_request *
539__lookup_request_ge(struct ceph_osd_client *osdc,
540 u64 tid)
541{
542 struct ceph_osd_request *req;
543 struct rb_node *n = osdc->requests.rb_node;
544
545 while (n) {
546 req = rb_entry(n, struct ceph_osd_request, r_node);
547 if (tid < req->r_tid) {
548 if (!n->rb_left)
549 return req;
550 n = n->rb_left;
551 } else if (tid > req->r_tid) {
552 n = n->rb_right;
553 } else {
554 return req;
555 }
556 }
557 return NULL;
558}
559
Sage Weil6f6c7002011-01-17 20:34:08 -0800560/*
561 * Resubmit requests pending on the given osd.
562 */
563static void __kick_osd_requests(struct ceph_osd_client *osdc,
564 struct ceph_osd *osd)
565{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700566 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800567 int err;
568
569 dout("__kick_osd_requests osd%d\n", osd->o_osd);
570 err = __reset_osd(osdc, osd);
571 if (err == -EAGAIN)
572 return;
573
574 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
575 list_move(&req->r_req_lru_item, &osdc->req_unsent);
576 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
577 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700578 if (!req->r_linger)
579 req->r_flags |= CEPH_OSD_FLAG_RETRY;
580 }
581
582 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
583 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700584 /*
585 * reregister request prior to unregistering linger so
586 * that r_osd is preserved.
587 */
588 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700589 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700590 list_add(&req->r_req_lru_item, &osdc->req_unsent);
591 list_add(&req->r_osd_item, &req->r_osd->o_requests);
592 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700593 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
594 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800595 }
596}
597
598static void kick_osd_requests(struct ceph_osd_client *osdc,
599 struct ceph_osd *kickosd)
600{
601 mutex_lock(&osdc->request_mutex);
602 __kick_osd_requests(osdc, kickosd);
603 mutex_unlock(&osdc->request_mutex);
604}
Sage Weilf24e9982009-10-06 11:31:10 -0700605
606/*
Sage Weil81b024e2009-10-09 10:29:18 -0700607 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700608 */
609static void osd_reset(struct ceph_connection *con)
610{
611 struct ceph_osd *osd = con->private;
612 struct ceph_osd_client *osdc;
613
614 if (!osd)
615 return;
616 dout("osd_reset osd%d\n", osd->o_osd);
617 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700618 down_read(&osdc->map_sem);
Sage Weil6f6c7002011-01-17 20:34:08 -0800619 kick_osd_requests(osdc, osd);
620 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700621 up_read(&osdc->map_sem);
622}
623
624/*
625 * Track open sessions with osds.
626 */
627static struct ceph_osd *create_osd(struct ceph_osd_client *osdc)
628{
629 struct ceph_osd *osd;
630
631 osd = kzalloc(sizeof(*osd), GFP_NOFS);
632 if (!osd)
633 return NULL;
634
635 atomic_set(&osd->o_ref, 1);
636 osd->o_osdc = osdc;
637 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700638 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800639 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700640 osd->o_incarnation = 1;
641
642 ceph_con_init(osdc->client->msgr, &osd->o_con);
643 osd->o_con.private = osd;
644 osd->o_con.ops = &osd_con_ops;
645 osd->o_con.peer_name.type = CEPH_ENTITY_TYPE_OSD;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800646
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800647 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700648 return osd;
649}
650
651static struct ceph_osd *get_osd(struct ceph_osd *osd)
652{
653 if (atomic_inc_not_zero(&osd->o_ref)) {
654 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
655 atomic_read(&osd->o_ref));
656 return osd;
657 } else {
658 dout("get_osd %p FAIL\n", osd);
659 return NULL;
660 }
661}
662
663static void put_osd(struct ceph_osd *osd)
664{
665 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
666 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500667 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700668 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
669
Alex Eldera2556512012-05-16 15:16:39 -0500670 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500671 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700672 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700673 }
Sage Weilf24e9982009-10-06 11:31:10 -0700674}
675
676/*
677 * remove an osd from our map
678 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800679static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700680{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800681 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700682 BUG_ON(!list_empty(&osd->o_requests));
683 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800684 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700685 ceph_con_close(&osd->o_con);
686 put_osd(osd);
687}
688
Sage Weilaca420b2011-08-31 14:45:53 -0700689static void remove_all_osds(struct ceph_osd_client *osdc)
690{
691 dout("__remove_old_osds %p\n", osdc);
692 mutex_lock(&osdc->request_mutex);
693 while (!RB_EMPTY_ROOT(&osdc->osds)) {
694 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
695 struct ceph_osd, o_node);
696 __remove_osd(osdc, osd);
697 }
698 mutex_unlock(&osdc->request_mutex);
699}
700
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800701static void __move_osd_to_lru(struct ceph_osd_client *osdc,
702 struct ceph_osd *osd)
703{
704 dout("__move_osd_to_lru %p\n", osd);
705 BUG_ON(!list_empty(&osd->o_osd_lru));
706 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700707 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800708}
709
710static void __remove_osd_from_lru(struct ceph_osd *osd)
711{
712 dout("__remove_osd_from_lru %p\n", osd);
713 if (!list_empty(&osd->o_osd_lru))
714 list_del_init(&osd->o_osd_lru);
715}
716
Sage Weilaca420b2011-08-31 14:45:53 -0700717static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800718{
719 struct ceph_osd *osd, *nosd;
720
721 dout("__remove_old_osds %p\n", osdc);
722 mutex_lock(&osdc->request_mutex);
723 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700724 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800725 break;
726 __remove_osd(osdc, osd);
727 }
728 mutex_unlock(&osdc->request_mutex);
729}
730
Sage Weilf24e9982009-10-06 11:31:10 -0700731/*
732 * reset osd connect
733 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800734static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700735{
Sage Weil87b315a2010-03-22 14:51:18 -0700736 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700737 int ret = 0;
738
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800739 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700740 if (list_empty(&osd->o_requests) &&
741 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800742 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700743 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
744 &osd->o_con.peer_addr,
745 sizeof(osd->o_con.peer_addr)) == 0 &&
746 !ceph_con_opened(&osd->o_con)) {
747 dout(" osd addr hasn't changed and connection never opened,"
748 " letting msgr retry");
749 /* touch each r_stamp for handle_timeout()'s benfit */
750 list_for_each_entry(req, &osd->o_requests, r_osd_item)
751 req->r_stamp = jiffies;
752 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700753 } else {
754 ceph_con_close(&osd->o_con);
755 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
756 osd->o_incarnation++;
757 }
758 return ret;
759}
760
761static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
762{
763 struct rb_node **p = &osdc->osds.rb_node;
764 struct rb_node *parent = NULL;
765 struct ceph_osd *osd = NULL;
766
Sage Weilaca420b2011-08-31 14:45:53 -0700767 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700768 while (*p) {
769 parent = *p;
770 osd = rb_entry(parent, struct ceph_osd, o_node);
771 if (new->o_osd < osd->o_osd)
772 p = &(*p)->rb_left;
773 else if (new->o_osd > osd->o_osd)
774 p = &(*p)->rb_right;
775 else
776 BUG();
777 }
778
779 rb_link_node(&new->o_node, parent, p);
780 rb_insert_color(&new->o_node, &osdc->osds);
781}
782
783static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
784{
785 struct ceph_osd *osd;
786 struct rb_node *n = osdc->osds.rb_node;
787
788 while (n) {
789 osd = rb_entry(n, struct ceph_osd, o_node);
790 if (o < osd->o_osd)
791 n = n->rb_left;
792 else if (o > osd->o_osd)
793 n = n->rb_right;
794 else
795 return osd;
796 }
797 return NULL;
798}
799
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800800static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
801{
802 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700803 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800804}
805
806static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
807{
808 cancel_delayed_work(&osdc->timeout_work);
809}
Sage Weilf24e9982009-10-06 11:31:10 -0700810
811/*
812 * Register request, assign tid. If this is the first request, set up
813 * the timeout event.
814 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700815static void __register_request(struct ceph_osd_client *osdc,
816 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700817{
Sage Weilf24e9982009-10-06 11:31:10 -0700818 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800819 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700820 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700821 __insert_request(osdc, req);
822 ceph_osdc_get_request(req);
823 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700824 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800825 dout(" first request, scheduling timeout\n");
826 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700827 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700828}
829
830static void register_request(struct ceph_osd_client *osdc,
831 struct ceph_osd_request *req)
832{
833 mutex_lock(&osdc->request_mutex);
834 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700835 mutex_unlock(&osdc->request_mutex);
836}
837
838/*
839 * called under osdc->request_mutex
840 */
841static void __unregister_request(struct ceph_osd_client *osdc,
842 struct ceph_osd_request *req)
843{
844 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
845 rb_erase(&req->r_node, &osdc->requests);
846 osdc->num_requests--;
847
Sage Weil0ba64782009-10-08 16:57:16 -0700848 if (req->r_osd) {
849 /* make sure the original request isn't in flight. */
850 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
851
852 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700853 if (list_empty(&req->r_osd->o_requests) &&
854 list_empty(&req->r_osd->o_linger_requests)) {
855 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800856 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700857 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700858 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700859 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700860 }
Sage Weilf24e9982009-10-06 11:31:10 -0700861
862 ceph_osdc_put_request(req);
863
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800864 list_del_init(&req->r_req_lru_item);
865 if (osdc->num_requests == 0) {
866 dout(" no requests, canceling timeout\n");
867 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700868 }
869}
870
871/*
872 * Cancel a previously queued request message
873 */
874static void __cancel_request(struct ceph_osd_request *req)
875{
Sage Weil6bc18872010-09-27 10:18:52 -0700876 if (req->r_sent && req->r_osd) {
Sage Weilf24e9982009-10-06 11:31:10 -0700877 ceph_con_revoke(&req->r_osd->o_con, req->r_request);
878 req->r_sent = 0;
879 }
880}
881
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700882static void __register_linger_request(struct ceph_osd_client *osdc,
883 struct ceph_osd_request *req)
884{
885 dout("__register_linger_request %p\n", req);
886 list_add_tail(&req->r_linger_item, &osdc->req_linger);
887 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
888}
889
890static void __unregister_linger_request(struct ceph_osd_client *osdc,
891 struct ceph_osd_request *req)
892{
893 dout("__unregister_linger_request %p\n", req);
894 if (req->r_osd) {
895 list_del_init(&req->r_linger_item);
896 list_del_init(&req->r_linger_osd);
897
898 if (list_empty(&req->r_osd->o_requests) &&
899 list_empty(&req->r_osd->o_linger_requests)) {
900 dout("moving osd to %p lru\n", req->r_osd);
901 __move_osd_to_lru(osdc, req->r_osd);
902 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700903 if (list_empty(&req->r_osd_item))
904 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700905 }
906}
907
908void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
909 struct ceph_osd_request *req)
910{
911 mutex_lock(&osdc->request_mutex);
912 if (req->r_linger) {
913 __unregister_linger_request(osdc, req);
914 ceph_osdc_put_request(req);
915 }
916 mutex_unlock(&osdc->request_mutex);
917}
918EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
919
920void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
921 struct ceph_osd_request *req)
922{
923 if (!req->r_linger) {
924 dout("set_request_linger %p\n", req);
925 req->r_linger = 1;
926 /*
927 * caller is now responsible for calling
928 * unregister_linger_request
929 */
930 ceph_osdc_get_request(req);
931 }
932}
933EXPORT_SYMBOL(ceph_osdc_set_request_linger);
934
Sage Weilf24e9982009-10-06 11:31:10 -0700935/*
936 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
937 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300938 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800939 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700940 *
941 * Return 0 if unchanged, 1 if changed, or negative on error.
942 *
943 * Caller should hold map_sem for read and request_mutex.
944 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800945static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700946 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700947{
948 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800949 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700950 int acting[CEPH_PG_MAX_SIZE];
951 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700952 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700953
Sage Weil6f6c7002011-01-17 20:34:08 -0800954 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700955 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
956 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800957 if (err) {
958 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700959 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800960 }
Sage Weil51042122009-11-04 11:39:12 -0800961 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800962 req->r_pgid = pgid;
963
Sage Weild85b7052010-05-10 10:24:48 -0700964 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
965 if (err > 0) {
966 o = acting[0];
967 num = err;
968 }
Sage Weilf24e9982009-10-06 11:31:10 -0700969
Sage Weil38d64532011-10-14 13:33:55 -0700970 if ((!force_resend &&
971 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700972 req->r_sent >= req->r_osd->o_incarnation &&
973 req->r_num_pg_osds == num &&
974 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700975 (req->r_osd == NULL && o == -1))
976 return 0; /* no change */
977
Sage Weil6f6c7002011-01-17 20:34:08 -0800978 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800979 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700980 req->r_osd ? req->r_osd->o_osd : -1);
981
Sage Weild85b7052010-05-10 10:24:48 -0700982 /* record full pg acting set */
983 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
984 req->r_num_pg_osds = num;
985
Sage Weilf24e9982009-10-06 11:31:10 -0700986 if (req->r_osd) {
987 __cancel_request(req);
988 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700989 req->r_osd = NULL;
990 }
991
992 req->r_osd = __lookup_osd(osdc, o);
993 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800994 err = -ENOMEM;
995 req->r_osd = create_osd(osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -0800996 if (!req->r_osd) {
997 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800998 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -0800999 }
Sage Weilf24e9982009-10-06 11:31:10 -07001000
Sage Weil6f6c7002011-01-17 20:34:08 -08001001 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001002 req->r_osd->o_osd = o;
1003 req->r_osd->o_con.peer_name.num = cpu_to_le64(o);
1004 __insert_osd(osdc, req->r_osd);
1005
1006 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1007 }
1008
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001009 if (req->r_osd) {
1010 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001011 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001012 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1013 } else {
1014 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001015 }
Sage Weild85b7052010-05-10 10:24:48 -07001016 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001017
1018out:
Sage Weilf24e9982009-10-06 11:31:10 -07001019 return err;
1020}
1021
1022/*
1023 * caller should hold map_sem (for read) and request_mutex
1024 */
Sage Weil56e925b2012-01-03 12:34:34 -08001025static void __send_request(struct ceph_osd_client *osdc,
1026 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001027{
1028 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001029
1030 dout("send_request %p tid %llu to osd%d flags %d\n",
1031 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1032
1033 reqhead = req->r_request->front.iov_base;
1034 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1035 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1036 reqhead->reassert_version = req->r_reassert_version;
1037
Sage Weil3dd72fc2010-03-22 14:42:30 -07001038 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001039 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001040
1041 ceph_msg_get(req->r_request); /* send consumes a ref */
1042 ceph_con_send(&req->r_osd->o_con, req->r_request);
1043 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001044}
1045
1046/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001047 * Send any requests in the queue (req_unsent).
1048 */
1049static void send_queued(struct ceph_osd_client *osdc)
1050{
1051 struct ceph_osd_request *req, *tmp;
1052
1053 dout("send_queued\n");
1054 mutex_lock(&osdc->request_mutex);
1055 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1056 __send_request(osdc, req);
1057 }
1058 mutex_unlock(&osdc->request_mutex);
1059}
1060
1061/*
Sage Weilf24e9982009-10-06 11:31:10 -07001062 * Timeout callback, called every N seconds when 1 or more osd
1063 * requests has been active for more than N seconds. When this
1064 * happens, we ping all OSDs with requests who have timed out to
1065 * ensure any communications channel reset is detected. Reset the
1066 * request timeouts another N seconds in the future as we go.
1067 * Reschedule the timeout event another N seconds in future (unless
1068 * there are no open requests).
1069 */
1070static void handle_timeout(struct work_struct *work)
1071{
1072 struct ceph_osd_client *osdc =
1073 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001074 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001075 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001076 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001077 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001078 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001079 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001080 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001081 dout("timeout\n");
1082 down_read(&osdc->map_sem);
1083
1084 ceph_monc_request_next_osdmap(&osdc->client->monc);
1085
1086 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001087
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001088 /*
1089 * reset osds that appear to be _really_ unresponsive. this
1090 * is a failsafe measure.. we really shouldn't be getting to
1091 * this point if the system is working properly. the monitors
1092 * should mark the osd as failed and we should find out about
1093 * it from an updated osd map.
1094 */
Sage Weilf26e6812010-04-21 11:09:38 -07001095 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001096 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1097 r_req_lru_item);
1098
Sage Weil4cf9d542011-07-26 11:27:24 -07001099 /* hasn't been long enough since we sent it? */
Sage Weil3dd72fc2010-03-22 14:42:30 -07001100 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001101 break;
1102
Sage Weil4cf9d542011-07-26 11:27:24 -07001103 /* hasn't been long enough since it was acked? */
1104 if (req->r_request->ack_stamp == 0 ||
1105 time_before(jiffies, req->r_request->ack_stamp + timeout))
1106 break;
1107
Sage Weil3dd72fc2010-03-22 14:42:30 -07001108 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001109 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001110 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001111
1112 osd = req->r_osd;
1113 BUG_ON(!osd);
1114 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1115 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001116 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001117 }
1118
1119 /*
1120 * ping osds that are a bit slow. this ensures that if there
1121 * is a break in the TCP connection we will notice, and reopen
1122 * a connection with that osd (from the fault callback).
1123 */
1124 INIT_LIST_HEAD(&slow_osds);
1125 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001126 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001127 break;
1128
1129 osd = req->r_osd;
1130 BUG_ON(!osd);
1131 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001132 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001133 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1134 }
1135 while (!list_empty(&slow_osds)) {
1136 osd = list_entry(slow_osds.next, struct ceph_osd,
1137 o_keepalive_item);
1138 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001139 ceph_con_keepalive(&osd->o_con);
1140 }
1141
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001142 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001143 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001144 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001145 up_read(&osdc->map_sem);
1146}
1147
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001148static void handle_osds_timeout(struct work_struct *work)
1149{
1150 struct ceph_osd_client *osdc =
1151 container_of(work, struct ceph_osd_client,
1152 osds_timeout_work.work);
1153 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001154 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001155
1156 dout("osds timeout\n");
1157 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001158 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001159 up_read(&osdc->map_sem);
1160
1161 schedule_delayed_work(&osdc->osds_timeout_work,
1162 round_jiffies_relative(delay));
1163}
1164
Sage Weil25845472011-06-03 09:37:09 -07001165static void complete_request(struct ceph_osd_request *req)
1166{
1167 if (req->r_safe_callback)
1168 req->r_safe_callback(req, NULL);
1169 complete_all(&req->r_safe_completion); /* fsync waiter */
1170}
1171
Sage Weilf24e9982009-10-06 11:31:10 -07001172/*
1173 * handle osd op reply. either call the callback if it is specified,
1174 * or do the completion to wake up the waiting thread.
1175 */
Sage Weil350b1c32009-12-22 10:45:45 -08001176static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1177 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001178{
1179 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1180 struct ceph_osd_request *req;
1181 u64 tid;
1182 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001183 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001184
Sage Weil6df058c2009-12-22 11:24:33 -08001185 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001186 if (msg->front.iov_len < sizeof(*rhead))
1187 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001188 numops = le32_to_cpu(rhead->num_ops);
1189 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001190 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001191 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1192 numops * sizeof(struct ceph_osd_op))
1193 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001194 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001195 /* lookup */
1196 mutex_lock(&osdc->request_mutex);
1197 req = __lookup_request(osdc, tid);
1198 if (req == NULL) {
1199 dout("handle_reply tid %llu dne\n", tid);
1200 mutex_unlock(&osdc->request_mutex);
1201 return;
1202 }
1203 ceph_osdc_get_request(req);
1204 flags = le32_to_cpu(rhead->flags);
1205
Sage Weil350b1c32009-12-22 10:45:45 -08001206 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001207 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001208 * avoid a (safe but slower) revoke later.
1209 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001210 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001211 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001212 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -08001213 ceph_con_put(con);
1214 }
1215
Sage Weilf24e9982009-10-06 11:31:10 -07001216 if (!req->r_got_reply) {
1217 unsigned bytes;
1218
1219 req->r_result = le32_to_cpu(rhead->result);
1220 bytes = le32_to_cpu(msg->hdr.data_len);
1221 dout("handle_reply result %d bytes %d\n", req->r_result,
1222 bytes);
1223 if (req->r_result == 0)
1224 req->r_result = bytes;
1225
1226 /* in case this is a write and we need to replay, */
1227 req->r_reassert_version = rhead->reassert_version;
1228
1229 req->r_got_reply = 1;
1230 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1231 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001232 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001233 goto done;
1234 }
1235
1236 dout("handle_reply tid %llu flags %d\n", tid, flags);
1237
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001238 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1239 __register_linger_request(osdc, req);
1240
Sage Weilf24e9982009-10-06 11:31:10 -07001241 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001242 if (result < 0 ||
1243 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001244 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1245 __unregister_request(osdc, req);
1246
1247 mutex_unlock(&osdc->request_mutex);
1248
1249 if (req->r_callback)
1250 req->r_callback(req, msg);
1251 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001252 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001253
Sage Weil25845472011-06-03 09:37:09 -07001254 if (flags & CEPH_OSD_FLAG_ONDISK)
1255 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001256
1257done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001258 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001259 ceph_osdc_put_request(req);
1260 return;
1261
1262bad:
1263 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1264 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1265 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001266 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001267}
1268
Sage Weil6f6c7002011-01-17 20:34:08 -08001269static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001270{
Sage Weilf24e9982009-10-06 11:31:10 -07001271 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001272
Sage Weil6f6c7002011-01-17 20:34:08 -08001273 for (p = rb_first(&osdc->osds); p; p = n) {
1274 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001275
Sage Weil6f6c7002011-01-17 20:34:08 -08001276 n = rb_next(p);
1277 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1278 memcmp(&osd->o_con.peer_addr,
1279 ceph_osd_addr(osdc->osdmap,
1280 osd->o_osd),
1281 sizeof(struct ceph_entity_addr)) != 0)
1282 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001283 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001284}
1285
1286/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001287 * Requeue requests whose mapping to an OSD has changed. If requests map to
1288 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001289 *
1290 * Caller should hold map_sem for read and request_mutex.
1291 */
Sage Weil38d64532011-10-14 13:33:55 -07001292static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001293{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001294 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001295 struct rb_node *p;
1296 int needmap = 0;
1297 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001298
Sage Weil38d64532011-10-14 13:33:55 -07001299 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001300 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001301 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1302 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil38d64532011-10-14 13:33:55 -07001303 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001304 if (err < 0)
1305 continue; /* error */
1306 if (req->r_osd == NULL) {
1307 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1308 needmap++; /* request a newer map */
1309 } else if (err > 0) {
1310 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1311 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001312 if (!req->r_linger)
1313 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001314 }
1315 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001316
1317 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1318 r_linger_item) {
1319 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1320
Sage Weil38d64532011-10-14 13:33:55 -07001321 err = __map_request(osdc, req, force_resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001322 if (err == 0)
1323 continue; /* no change and no osd was specified */
1324 if (err < 0)
1325 continue; /* hrm! */
1326 if (req->r_osd == NULL) {
1327 dout("tid %llu maps to no valid osd\n", req->r_tid);
1328 needmap++; /* request a newer map */
1329 continue;
1330 }
1331
1332 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1333 req->r_osd ? req->r_osd->o_osd : -1);
1334 __unregister_linger_request(osdc, req);
1335 __register_request(osdc, req);
1336 }
Sage Weilf24e9982009-10-06 11:31:10 -07001337 mutex_unlock(&osdc->request_mutex);
1338
1339 if (needmap) {
1340 dout("%d requests for down osds, need new map\n", needmap);
1341 ceph_monc_request_next_osdmap(&osdc->client->monc);
1342 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001343}
Sage Weil6f6c7002011-01-17 20:34:08 -08001344
1345
Sage Weilf24e9982009-10-06 11:31:10 -07001346/*
1347 * Process updated osd map.
1348 *
1349 * The message contains any number of incremental and full maps, normally
1350 * indicating some sort of topology change in the cluster. Kick requests
1351 * off to different OSDs as needed.
1352 */
1353void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1354{
1355 void *p, *end, *next;
1356 u32 nr_maps, maplen;
1357 u32 epoch;
1358 struct ceph_osdmap *newmap = NULL, *oldmap;
1359 int err;
1360 struct ceph_fsid fsid;
1361
1362 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1363 p = msg->front.iov_base;
1364 end = p + msg->front.iov_len;
1365
1366 /* verify fsid */
1367 ceph_decode_need(&p, end, sizeof(fsid), bad);
1368 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001369 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1370 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001371
1372 down_write(&osdc->map_sem);
1373
1374 /* incremental maps */
1375 ceph_decode_32_safe(&p, end, nr_maps, bad);
1376 dout(" %d inc maps\n", nr_maps);
1377 while (nr_maps > 0) {
1378 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001379 epoch = ceph_decode_32(&p);
1380 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001381 ceph_decode_need(&p, end, maplen, bad);
1382 next = p + maplen;
1383 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1384 dout("applying incremental map %u len %d\n",
1385 epoch, maplen);
1386 newmap = osdmap_apply_incremental(&p, next,
1387 osdc->osdmap,
1388 osdc->client->msgr);
1389 if (IS_ERR(newmap)) {
1390 err = PTR_ERR(newmap);
1391 goto bad;
1392 }
Sage Weil30dc6382009-12-21 14:49:37 -08001393 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001394 if (newmap != osdc->osdmap) {
1395 ceph_osdmap_destroy(osdc->osdmap);
1396 osdc->osdmap = newmap;
1397 }
Sage Weil38d64532011-10-14 13:33:55 -07001398 kick_requests(osdc, 0);
Sage Weil6f6c7002011-01-17 20:34:08 -08001399 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001400 } else {
1401 dout("ignoring incremental map %u len %d\n",
1402 epoch, maplen);
1403 }
1404 p = next;
1405 nr_maps--;
1406 }
1407 if (newmap)
1408 goto done;
1409
1410 /* full maps */
1411 ceph_decode_32_safe(&p, end, nr_maps, bad);
1412 dout(" %d full maps\n", nr_maps);
1413 while (nr_maps) {
1414 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001415 epoch = ceph_decode_32(&p);
1416 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001417 ceph_decode_need(&p, end, maplen, bad);
1418 if (nr_maps > 1) {
1419 dout("skipping non-latest full map %u len %d\n",
1420 epoch, maplen);
1421 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1422 dout("skipping full map %u len %d, "
1423 "older than our %u\n", epoch, maplen,
1424 osdc->osdmap->epoch);
1425 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001426 int skipped_map = 0;
1427
Sage Weilf24e9982009-10-06 11:31:10 -07001428 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;
Sage Weil38d64532011-10-14 13:33:55 -07001437 if (oldmap) {
1438 if (oldmap->epoch + 1 < newmap->epoch)
1439 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001440 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001441 }
1442 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001443 }
1444 p += maplen;
1445 nr_maps--;
1446 }
1447
1448done:
1449 downgrade_write(&osdc->map_sem);
1450 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001451
1452 /*
1453 * subscribe to subsequent osdmap updates if full to ensure
1454 * we find out when we are no longer full and stop returning
1455 * ENOSPC.
1456 */
1457 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1458 ceph_monc_request_next_osdmap(&osdc->client->monc);
1459
Sage Weil6f6c7002011-01-17 20:34:08 -08001460 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001461 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001462 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001463 return;
1464
1465bad:
1466 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001467 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001468 up_write(&osdc->map_sem);
1469 return;
1470}
1471
Sage Weilf24e9982009-10-06 11:31:10 -07001472/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001473 * watch/notify callback event infrastructure
1474 *
1475 * These callbacks are used both for watch and notify operations.
1476 */
1477static void __release_event(struct kref *kref)
1478{
1479 struct ceph_osd_event *event =
1480 container_of(kref, struct ceph_osd_event, kref);
1481
1482 dout("__release_event %p\n", event);
1483 kfree(event);
1484}
1485
1486static void get_event(struct ceph_osd_event *event)
1487{
1488 kref_get(&event->kref);
1489}
1490
1491void ceph_osdc_put_event(struct ceph_osd_event *event)
1492{
1493 kref_put(&event->kref, __release_event);
1494}
1495EXPORT_SYMBOL(ceph_osdc_put_event);
1496
1497static void __insert_event(struct ceph_osd_client *osdc,
1498 struct ceph_osd_event *new)
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 (new->cookie < event->cookie)
1508 p = &(*p)->rb_left;
1509 else if (new->cookie > event->cookie)
1510 p = &(*p)->rb_right;
1511 else
1512 BUG();
1513 }
1514
1515 rb_link_node(&new->node, parent, p);
1516 rb_insert_color(&new->node, &osdc->event_tree);
1517}
1518
1519static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1520 u64 cookie)
1521{
1522 struct rb_node **p = &osdc->event_tree.rb_node;
1523 struct rb_node *parent = NULL;
1524 struct ceph_osd_event *event = NULL;
1525
1526 while (*p) {
1527 parent = *p;
1528 event = rb_entry(parent, struct ceph_osd_event, node);
1529 if (cookie < event->cookie)
1530 p = &(*p)->rb_left;
1531 else if (cookie > event->cookie)
1532 p = &(*p)->rb_right;
1533 else
1534 return event;
1535 }
1536 return NULL;
1537}
1538
1539static void __remove_event(struct ceph_osd_event *event)
1540{
1541 struct ceph_osd_client *osdc = event->osdc;
1542
1543 if (!RB_EMPTY_NODE(&event->node)) {
1544 dout("__remove_event removed %p\n", event);
1545 rb_erase(&event->node, &osdc->event_tree);
1546 ceph_osdc_put_event(event);
1547 } else {
1548 dout("__remove_event didn't remove %p\n", event);
1549 }
1550}
1551
1552int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1553 void (*event_cb)(u64, u64, u8, void *),
1554 int one_shot, void *data,
1555 struct ceph_osd_event **pevent)
1556{
1557 struct ceph_osd_event *event;
1558
1559 event = kmalloc(sizeof(*event), GFP_NOIO);
1560 if (!event)
1561 return -ENOMEM;
1562
1563 dout("create_event %p\n", event);
1564 event->cb = event_cb;
1565 event->one_shot = one_shot;
1566 event->data = data;
1567 event->osdc = osdc;
1568 INIT_LIST_HEAD(&event->osd_node);
1569 kref_init(&event->kref); /* one ref for us */
1570 kref_get(&event->kref); /* one ref for the caller */
1571 init_completion(&event->completion);
1572
1573 spin_lock(&osdc->event_lock);
1574 event->cookie = ++osdc->event_count;
1575 __insert_event(osdc, event);
1576 spin_unlock(&osdc->event_lock);
1577
1578 *pevent = event;
1579 return 0;
1580}
1581EXPORT_SYMBOL(ceph_osdc_create_event);
1582
1583void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1584{
1585 struct ceph_osd_client *osdc = event->osdc;
1586
1587 dout("cancel_event %p\n", event);
1588 spin_lock(&osdc->event_lock);
1589 __remove_event(event);
1590 spin_unlock(&osdc->event_lock);
1591 ceph_osdc_put_event(event); /* caller's */
1592}
1593EXPORT_SYMBOL(ceph_osdc_cancel_event);
1594
1595
1596static void do_event_work(struct work_struct *work)
1597{
1598 struct ceph_osd_event_work *event_work =
1599 container_of(work, struct ceph_osd_event_work, work);
1600 struct ceph_osd_event *event = event_work->event;
1601 u64 ver = event_work->ver;
1602 u64 notify_id = event_work->notify_id;
1603 u8 opcode = event_work->opcode;
1604
1605 dout("do_event_work completing %p\n", event);
1606 event->cb(ver, notify_id, opcode, event->data);
1607 complete(&event->completion);
1608 dout("do_event_work completed %p\n", event);
1609 ceph_osdc_put_event(event);
1610 kfree(event_work);
1611}
1612
1613
1614/*
1615 * Process osd watch notifications
1616 */
1617void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1618{
1619 void *p, *end;
1620 u8 proto_ver;
1621 u64 cookie, ver, notify_id;
1622 u8 opcode;
1623 struct ceph_osd_event *event;
1624 struct ceph_osd_event_work *event_work;
1625
1626 p = msg->front.iov_base;
1627 end = p + msg->front.iov_len;
1628
1629 ceph_decode_8_safe(&p, end, proto_ver, bad);
1630 ceph_decode_8_safe(&p, end, opcode, bad);
1631 ceph_decode_64_safe(&p, end, cookie, bad);
1632 ceph_decode_64_safe(&p, end, ver, bad);
1633 ceph_decode_64_safe(&p, end, notify_id, bad);
1634
1635 spin_lock(&osdc->event_lock);
1636 event = __find_event(osdc, cookie);
1637 if (event) {
1638 get_event(event);
1639 if (event->one_shot)
1640 __remove_event(event);
1641 }
1642 spin_unlock(&osdc->event_lock);
1643 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1644 cookie, ver, event);
1645 if (event) {
1646 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001647 if (!event_work) {
1648 dout("ERROR: could not allocate event_work\n");
1649 goto done_err;
1650 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001651 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001652 event_work->event = event;
1653 event_work->ver = ver;
1654 event_work->notify_id = notify_id;
1655 event_work->opcode = opcode;
1656 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1657 dout("WARNING: failed to queue notify event work\n");
1658 goto done_err;
1659 }
1660 }
1661
1662 return;
1663
1664done_err:
1665 complete(&event->completion);
1666 ceph_osdc_put_event(event);
1667 return;
1668
1669bad:
1670 pr_err("osdc handle_watch_notify corrupt msg\n");
1671 return;
1672}
1673
1674int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1675{
1676 int err;
1677
1678 dout("wait_event %p\n", event);
1679 err = wait_for_completion_interruptible_timeout(&event->completion,
1680 timeout * HZ);
1681 ceph_osdc_put_event(event);
1682 if (err > 0)
1683 err = 0;
1684 dout("wait_event %p returns %d\n", event, err);
1685 return err;
1686}
1687EXPORT_SYMBOL(ceph_osdc_wait_event);
1688
1689/*
Sage Weilf24e9982009-10-06 11:31:10 -07001690 * Register request, send initial attempt.
1691 */
1692int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1693 struct ceph_osd_request *req,
1694 bool nofail)
1695{
Sage Weilc1ea8822009-10-08 16:55:47 -07001696 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001697
1698 req->r_request->pages = req->r_pages;
1699 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001700#ifdef CONFIG_BLOCK
1701 req->r_request->bio = req->r_bio;
1702#endif
1703 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001704
1705 register_request(osdc, req);
1706
1707 down_read(&osdc->map_sem);
1708 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001709 /*
1710 * a racing kick_requests() may have sent the message for us
1711 * while we dropped request_mutex above, so only send now if
1712 * the request still han't been touched yet.
1713 */
1714 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001715 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001716 if (rc < 0) {
1717 if (nofail) {
1718 dout("osdc_start_request failed map, "
1719 " will retry %lld\n", req->r_tid);
1720 rc = 0;
1721 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001722 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001723 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001724 if (req->r_osd == NULL) {
1725 dout("send_request %p no up osds in pg\n", req);
1726 ceph_monc_request_next_osdmap(&osdc->client->monc);
1727 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001728 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001729 }
Sage Weil56e925b2012-01-03 12:34:34 -08001730 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001731 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001732
1733out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001734 mutex_unlock(&osdc->request_mutex);
1735 up_read(&osdc->map_sem);
1736 return rc;
1737}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001738EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001739
1740/*
1741 * wait for a request to complete
1742 */
1743int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1744 struct ceph_osd_request *req)
1745{
1746 int rc;
1747
1748 rc = wait_for_completion_interruptible(&req->r_completion);
1749 if (rc < 0) {
1750 mutex_lock(&osdc->request_mutex);
1751 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001752 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001753 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001754 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001755 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001756 return rc;
1757 }
1758
1759 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1760 return req->r_result;
1761}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001762EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001763
1764/*
1765 * sync - wait for all in-flight requests to flush. avoid starvation.
1766 */
1767void ceph_osdc_sync(struct ceph_osd_client *osdc)
1768{
1769 struct ceph_osd_request *req;
1770 u64 last_tid, next_tid = 0;
1771
1772 mutex_lock(&osdc->request_mutex);
1773 last_tid = osdc->last_tid;
1774 while (1) {
1775 req = __lookup_request_ge(osdc, next_tid);
1776 if (!req)
1777 break;
1778 if (req->r_tid > last_tid)
1779 break;
1780
1781 next_tid = req->r_tid + 1;
1782 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1783 continue;
1784
1785 ceph_osdc_get_request(req);
1786 mutex_unlock(&osdc->request_mutex);
1787 dout("sync waiting on tid %llu (last is %llu)\n",
1788 req->r_tid, last_tid);
1789 wait_for_completion(&req->r_safe_completion);
1790 mutex_lock(&osdc->request_mutex);
1791 ceph_osdc_put_request(req);
1792 }
1793 mutex_unlock(&osdc->request_mutex);
1794 dout("sync done (thru tid %llu)\n", last_tid);
1795}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001796EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001797
1798/*
1799 * init, shutdown
1800 */
1801int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1802{
1803 int err;
1804
1805 dout("init\n");
1806 osdc->client = client;
1807 osdc->osdmap = NULL;
1808 init_rwsem(&osdc->map_sem);
1809 init_completion(&osdc->map_waiters);
1810 osdc->last_requested_map = 0;
1811 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001812 osdc->last_tid = 0;
1813 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001814 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001815 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001816 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001817 INIT_LIST_HEAD(&osdc->req_unsent);
1818 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001819 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001820 osdc->num_requests = 0;
1821 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001822 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001823 spin_lock_init(&osdc->event_lock);
1824 osdc->event_tree = RB_ROOT;
1825 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001826
1827 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001828 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001829
Sage Weil5f44f142009-11-18 14:52:18 -08001830 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001831 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1832 sizeof(struct ceph_osd_request));
1833 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001834 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001835
Sage Weil4f482802010-04-24 09:56:35 -07001836 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1837 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001838 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001839 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001840 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001841 OSD_OPREPLY_FRONT_LEN, 10, true,
1842 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001843 if (err < 0)
1844 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001845
1846 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1847 if (IS_ERR(osdc->notify_wq)) {
1848 err = PTR_ERR(osdc->notify_wq);
1849 osdc->notify_wq = NULL;
1850 goto out_msgpool;
1851 }
Sage Weilf24e9982009-10-06 11:31:10 -07001852 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001853
Sage Weilc16e7862010-03-01 13:02:00 -08001854out_msgpool:
1855 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001856out_mempool:
1857 mempool_destroy(osdc->req_mempool);
1858out:
1859 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001860}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001861EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001862
1863void ceph_osdc_stop(struct ceph_osd_client *osdc)
1864{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001865 flush_workqueue(osdc->notify_wq);
1866 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001867 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001868 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001869 if (osdc->osdmap) {
1870 ceph_osdmap_destroy(osdc->osdmap);
1871 osdc->osdmap = NULL;
1872 }
Sage Weilaca420b2011-08-31 14:45:53 -07001873 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001874 mempool_destroy(osdc->req_mempool);
1875 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001876 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001877}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001878EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001879
1880/*
1881 * Read some contiguous pages. If we cross a stripe boundary, shorten
1882 * *plen. Return number of bytes read, or error.
1883 */
1884int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1885 struct ceph_vino vino, struct ceph_file_layout *layout,
1886 u64 off, u64 *plen,
1887 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001888 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001889{
1890 struct ceph_osd_request *req;
1891 int rc = 0;
1892
1893 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1894 vino.snap, off, *plen);
1895 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1896 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1897 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001898 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001899 if (!req)
1900 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001901
1902 /* it may be a short read due to an object boundary */
1903 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001904
Sage Weilb7495fc2010-11-09 12:43:12 -08001905 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1906 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001907
1908 rc = ceph_osdc_start_request(osdc, req, false);
1909 if (!rc)
1910 rc = ceph_osdc_wait_request(osdc, req);
1911
1912 ceph_osdc_put_request(req);
1913 dout("readpages result %d\n", rc);
1914 return rc;
1915}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001916EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001917
1918/*
1919 * do a synchronous write on N pages
1920 */
1921int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1922 struct ceph_file_layout *layout,
1923 struct ceph_snap_context *snapc,
1924 u64 off, u64 len,
1925 u32 truncate_seq, u64 truncate_size,
1926 struct timespec *mtime,
1927 struct page **pages, int num_pages,
1928 int flags, int do_sync, bool nofail)
1929{
1930 struct ceph_osd_request *req;
1931 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001932 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001933
1934 BUG_ON(vino.snap != CEPH_NOSNAP);
1935 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1936 CEPH_OSD_OP_WRITE,
1937 flags | CEPH_OSD_FLAG_ONDISK |
1938 CEPH_OSD_FLAG_WRITE,
1939 snapc, do_sync,
1940 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001941 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001942 if (!req)
1943 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001944
1945 /* it may be a short write due to an object boundary */
1946 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001947 dout("writepages %llu~%llu (%d pages)\n", off, len,
1948 req->r_num_pages);
1949
1950 rc = ceph_osdc_start_request(osdc, req, nofail);
1951 if (!rc)
1952 rc = ceph_osdc_wait_request(osdc, req);
1953
1954 ceph_osdc_put_request(req);
1955 if (rc == 0)
1956 rc = len;
1957 dout("writepages result %d\n", rc);
1958 return rc;
1959}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001960EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001961
1962/*
1963 * handle incoming message
1964 */
1965static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1966{
1967 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001968 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001969 int type = le16_to_cpu(msg->hdr.type);
1970
1971 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001972 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001973 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001974
1975 switch (type) {
1976 case CEPH_MSG_OSD_MAP:
1977 ceph_osdc_handle_map(osdc, msg);
1978 break;
1979 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001980 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001981 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001982 case CEPH_MSG_WATCH_NOTIFY:
1983 handle_watch_notify(osdc, msg);
1984 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001985
1986 default:
1987 pr_err("received unknown message type %d %s\n", type,
1988 ceph_msg_type_name(type));
1989 }
Sage Weil4a32f932010-06-13 10:27:53 -07001990out:
Sage Weilf24e9982009-10-06 11:31:10 -07001991 ceph_msg_put(msg);
1992}
1993
Sage Weil5b3a4db2010-02-19 21:43:23 -08001994/*
Sage Weil21b667f2010-03-04 10:22:59 -08001995 * lookup and return message for incoming reply. set up reply message
1996 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001997 */
1998static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001999 struct ceph_msg_header *hdr,
2000 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002001{
2002 struct ceph_osd *osd = con->private;
2003 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002004 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002005 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002006 int front = le32_to_cpu(hdr->front_len);
2007 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002008 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002009
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002010 tid = le64_to_cpu(hdr->tid);
2011 mutex_lock(&osdc->request_mutex);
2012 req = __lookup_request(osdc, tid);
2013 if (!req) {
2014 *skip = 1;
2015 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08002016 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08002017 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002018 goto out;
2019 }
Sage Weilc16e7862010-03-01 13:02:00 -08002020
2021 if (req->r_con_filling_msg) {
2022 dout("get_reply revoking msg %p from old con %p\n",
2023 req->r_reply, req->r_con_filling_msg);
2024 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
2025 ceph_con_put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002026 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002027 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002028
Sage Weilc16e7862010-03-01 13:02:00 -08002029 if (front > req->r_reply->front.iov_len) {
2030 pr_warning("get_reply front %d > preallocated %d\n",
2031 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002032 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002033 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002034 goto out;
2035 ceph_msg_put(req->r_reply);
2036 req->r_reply = m;
2037 }
2038 m = ceph_msg_get(req->r_reply);
2039
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002040 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002041 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002042
2043 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002044 pr_warning("tid %lld reply has %d bytes %d pages, we"
2045 " had only %d pages ready\n", tid, data_len,
2046 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002047 *skip = 1;
2048 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002049 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002050 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002051 }
Sage Weil21b667f2010-03-04 10:22:59 -08002052 m->pages = req->r_pages;
2053 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002054 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002055#ifdef CONFIG_BLOCK
2056 m->bio = req->r_bio;
2057#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002058 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002059 *skip = 0;
Sage Weilc16e7862010-03-01 13:02:00 -08002060 req->r_con_filling_msg = ceph_con_get(con);
2061 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002062
2063out:
2064 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002065 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002066
2067}
2068
2069static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2070 struct ceph_msg_header *hdr,
2071 int *skip)
2072{
2073 struct ceph_osd *osd = con->private;
2074 int type = le16_to_cpu(hdr->type);
2075 int front = le32_to_cpu(hdr->front_len);
2076
2077 switch (type) {
2078 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002079 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002080 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002081 case CEPH_MSG_OSD_OPREPLY:
2082 return get_reply(con, hdr, skip);
2083 default:
2084 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2085 osd->o_osd);
2086 *skip = 1;
2087 return NULL;
2088 }
Sage Weilf24e9982009-10-06 11:31:10 -07002089}
2090
2091/*
2092 * Wrappers to refcount containing ceph_osd struct
2093 */
2094static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2095{
2096 struct ceph_osd *osd = con->private;
2097 if (get_osd(osd))
2098 return con;
2099 return NULL;
2100}
2101
2102static void put_osd_con(struct ceph_connection *con)
2103{
2104 struct ceph_osd *osd = con->private;
2105 put_osd(osd);
2106}
2107
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002108/*
2109 * authentication
2110 */
2111static int get_authorizer(struct ceph_connection *con,
Sage Weil213c99e2010-08-03 10:25:11 -07002112 void **buf, int *len, int *proto,
2113 void **reply_buf, int *reply_len, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002114{
2115 struct ceph_osd *o = con->private;
2116 struct ceph_osd_client *osdc = o->o_osdc;
2117 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002118 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002119 int ret = 0;
2120
Alex Elder74f18692012-05-16 15:16:39 -05002121 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002122 if (ac->ops && ac->ops->destroy_authorizer)
2123 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002124 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002125 }
Alex Eldera2556512012-05-16 15:16:39 -05002126 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Elder74f18692012-05-16 15:16:39 -05002127 ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD, auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002128 if (ret)
Sage Weil213c99e2010-08-03 10:25:11 -07002129 return ret;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002130 }
2131
2132 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002133 *buf = auth->authorizer_buf;
2134 *len = auth->authorizer_buf_len;
2135 *reply_buf = auth->authorizer_reply_buf;
2136 *reply_len = auth->authorizer_reply_buf_len;
2137
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002138 return 0;
2139}
2140
2141
2142static int verify_authorizer_reply(struct ceph_connection *con, int len)
2143{
2144 struct ceph_osd *o = con->private;
2145 struct ceph_osd_client *osdc = o->o_osdc;
2146 struct ceph_auth_client *ac = osdc->client->monc.auth;
2147
Alex Eldera2556512012-05-16 15:16:39 -05002148 /*
2149 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2150 * XXX which do we do: succeed or fail?
2151 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002152 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002153}
2154
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002155static int invalidate_authorizer(struct ceph_connection *con)
2156{
2157 struct ceph_osd *o = con->private;
2158 struct ceph_osd_client *osdc = o->o_osdc;
2159 struct ceph_auth_client *ac = osdc->client->monc.auth;
2160
Alex Eldera2556512012-05-16 15:16:39 -05002161 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002162 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2163
2164 return ceph_monc_validate_auth(&osdc->client->monc);
2165}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002166
Tobias Klauser9e327892010-05-20 10:40:19 +02002167static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002168 .get = get_osd_con,
2169 .put = put_osd_con,
2170 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002171 .get_authorizer = get_authorizer,
2172 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002173 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002174 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002175 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002176};