blob: ad78705a4affc8113aa50a85384d85a53079206a [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);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800142 if (req->r_con_filling_msg) {
Sage Weil350b1c32009-12-22 10:45:45 -0800143 dout("release_request revoking pages %p from con %p\n",
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800144 req->r_pages, req->r_con_filling_msg);
145 ceph_con_revoke_message(req->r_con_filling_msg,
146 req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700147 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800148 }
Alex Elderab8cb342012-06-04 14:43:32 -0500149 if (req->r_reply)
150 ceph_msg_put(req->r_reply);
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 */
Alex Eldere10006f2012-05-26 23:26:43 -0500627static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700628{
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;
Alex Eldere10006f2012-05-26 23:26:43 -0500637 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -0700638 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700639 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800640 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700641 osd->o_incarnation = 1;
642
Alex Elder1bfd89f2012-05-26 23:26:43 -0500643 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr,
644 CEPH_ENTITY_TYPE_OSD, onum);
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);
Alex Eldera2556512012-05-16 15:16:39 -0500666 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700667 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
668
Alex Eldera2556512012-05-16 15:16:39 -0500669 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500670 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700671 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700672 }
Sage Weilf24e9982009-10-06 11:31:10 -0700673}
674
675/*
676 * remove an osd from our map
677 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800678static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700679{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800680 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700681 BUG_ON(!list_empty(&osd->o_requests));
682 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800683 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700684 ceph_con_close(&osd->o_con);
685 put_osd(osd);
686}
687
Sage Weilaca420b2011-08-31 14:45:53 -0700688static void remove_all_osds(struct ceph_osd_client *osdc)
689{
690 dout("__remove_old_osds %p\n", osdc);
691 mutex_lock(&osdc->request_mutex);
692 while (!RB_EMPTY_ROOT(&osdc->osds)) {
693 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
694 struct ceph_osd, o_node);
695 __remove_osd(osdc, osd);
696 }
697 mutex_unlock(&osdc->request_mutex);
698}
699
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800700static void __move_osd_to_lru(struct ceph_osd_client *osdc,
701 struct ceph_osd *osd)
702{
703 dout("__move_osd_to_lru %p\n", osd);
704 BUG_ON(!list_empty(&osd->o_osd_lru));
705 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700706 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800707}
708
709static void __remove_osd_from_lru(struct ceph_osd *osd)
710{
711 dout("__remove_osd_from_lru %p\n", osd);
712 if (!list_empty(&osd->o_osd_lru))
713 list_del_init(&osd->o_osd_lru);
714}
715
Sage Weilaca420b2011-08-31 14:45:53 -0700716static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800717{
718 struct ceph_osd *osd, *nosd;
719
720 dout("__remove_old_osds %p\n", osdc);
721 mutex_lock(&osdc->request_mutex);
722 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700723 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800724 break;
725 __remove_osd(osdc, osd);
726 }
727 mutex_unlock(&osdc->request_mutex);
728}
729
Sage Weilf24e9982009-10-06 11:31:10 -0700730/*
731 * reset osd connect
732 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800733static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700734{
Sage Weil87b315a2010-03-22 14:51:18 -0700735 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700736 int ret = 0;
737
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800738 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700739 if (list_empty(&osd->o_requests) &&
740 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800741 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700742 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
743 &osd->o_con.peer_addr,
744 sizeof(osd->o_con.peer_addr)) == 0 &&
745 !ceph_con_opened(&osd->o_con)) {
746 dout(" osd addr hasn't changed and connection never opened,"
747 " letting msgr retry");
748 /* touch each r_stamp for handle_timeout()'s benfit */
749 list_for_each_entry(req, &osd->o_requests, r_osd_item)
750 req->r_stamp = jiffies;
751 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700752 } else {
753 ceph_con_close(&osd->o_con);
754 ceph_con_open(&osd->o_con, &osdc->osdmap->osd_addr[osd->o_osd]);
755 osd->o_incarnation++;
756 }
757 return ret;
758}
759
760static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
761{
762 struct rb_node **p = &osdc->osds.rb_node;
763 struct rb_node *parent = NULL;
764 struct ceph_osd *osd = NULL;
765
Sage Weilaca420b2011-08-31 14:45:53 -0700766 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700767 while (*p) {
768 parent = *p;
769 osd = rb_entry(parent, struct ceph_osd, o_node);
770 if (new->o_osd < osd->o_osd)
771 p = &(*p)->rb_left;
772 else if (new->o_osd > osd->o_osd)
773 p = &(*p)->rb_right;
774 else
775 BUG();
776 }
777
778 rb_link_node(&new->o_node, parent, p);
779 rb_insert_color(&new->o_node, &osdc->osds);
780}
781
782static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
783{
784 struct ceph_osd *osd;
785 struct rb_node *n = osdc->osds.rb_node;
786
787 while (n) {
788 osd = rb_entry(n, struct ceph_osd, o_node);
789 if (o < osd->o_osd)
790 n = n->rb_left;
791 else if (o > osd->o_osd)
792 n = n->rb_right;
793 else
794 return osd;
795 }
796 return NULL;
797}
798
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800799static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
800{
801 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700802 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800803}
804
805static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
806{
807 cancel_delayed_work(&osdc->timeout_work);
808}
Sage Weilf24e9982009-10-06 11:31:10 -0700809
810/*
811 * Register request, assign tid. If this is the first request, set up
812 * the timeout event.
813 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700814static void __register_request(struct ceph_osd_client *osdc,
815 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700816{
Sage Weilf24e9982009-10-06 11:31:10 -0700817 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800818 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700819 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700820 __insert_request(osdc, req);
821 ceph_osdc_get_request(req);
822 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700823 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800824 dout(" first request, scheduling timeout\n");
825 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700826 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700827}
828
829static void register_request(struct ceph_osd_client *osdc,
830 struct ceph_osd_request *req)
831{
832 mutex_lock(&osdc->request_mutex);
833 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700834 mutex_unlock(&osdc->request_mutex);
835}
836
837/*
838 * called under osdc->request_mutex
839 */
840static void __unregister_request(struct ceph_osd_client *osdc,
841 struct ceph_osd_request *req)
842{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500843 if (RB_EMPTY_NODE(&req->r_node)) {
844 dout("__unregister_request %p tid %lld not registered\n",
845 req, req->r_tid);
846 return;
847 }
848
Sage Weilf24e9982009-10-06 11:31:10 -0700849 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
850 rb_erase(&req->r_node, &osdc->requests);
851 osdc->num_requests--;
852
Sage Weil0ba64782009-10-08 16:57:16 -0700853 if (req->r_osd) {
854 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500855 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700856
857 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700858 if (list_empty(&req->r_osd->o_requests) &&
859 list_empty(&req->r_osd->o_linger_requests)) {
860 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800861 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700862 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700863 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700864 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700865 }
Sage Weilf24e9982009-10-06 11:31:10 -0700866
867 ceph_osdc_put_request(req);
868
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800869 list_del_init(&req->r_req_lru_item);
870 if (osdc->num_requests == 0) {
871 dout(" no requests, canceling timeout\n");
872 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700873 }
874}
875
876/*
877 * Cancel a previously queued request message
878 */
879static void __cancel_request(struct ceph_osd_request *req)
880{
Sage Weil6bc18872010-09-27 10:18:52 -0700881 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500882 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700883 req->r_sent = 0;
884 }
885}
886
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700887static void __register_linger_request(struct ceph_osd_client *osdc,
888 struct ceph_osd_request *req)
889{
890 dout("__register_linger_request %p\n", req);
891 list_add_tail(&req->r_linger_item, &osdc->req_linger);
892 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
893}
894
895static void __unregister_linger_request(struct ceph_osd_client *osdc,
896 struct ceph_osd_request *req)
897{
898 dout("__unregister_linger_request %p\n", req);
899 if (req->r_osd) {
900 list_del_init(&req->r_linger_item);
901 list_del_init(&req->r_linger_osd);
902
903 if (list_empty(&req->r_osd->o_requests) &&
904 list_empty(&req->r_osd->o_linger_requests)) {
905 dout("moving osd to %p lru\n", req->r_osd);
906 __move_osd_to_lru(osdc, req->r_osd);
907 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700908 if (list_empty(&req->r_osd_item))
909 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700910 }
911}
912
913void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
914 struct ceph_osd_request *req)
915{
916 mutex_lock(&osdc->request_mutex);
917 if (req->r_linger) {
918 __unregister_linger_request(osdc, req);
919 ceph_osdc_put_request(req);
920 }
921 mutex_unlock(&osdc->request_mutex);
922}
923EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
924
925void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
927{
928 if (!req->r_linger) {
929 dout("set_request_linger %p\n", req);
930 req->r_linger = 1;
931 /*
932 * caller is now responsible for calling
933 * unregister_linger_request
934 */
935 ceph_osdc_get_request(req);
936 }
937}
938EXPORT_SYMBOL(ceph_osdc_set_request_linger);
939
Sage Weilf24e9982009-10-06 11:31:10 -0700940/*
941 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
942 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300943 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800944 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700945 *
946 * Return 0 if unchanged, 1 if changed, or negative on error.
947 *
948 * Caller should hold map_sem for read and request_mutex.
949 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800950static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700951 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700952{
953 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800954 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700955 int acting[CEPH_PG_MAX_SIZE];
956 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700957 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700958
Sage Weil6f6c7002011-01-17 20:34:08 -0800959 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700960 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
961 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800962 if (err) {
963 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700964 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800965 }
Sage Weil51042122009-11-04 11:39:12 -0800966 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800967 req->r_pgid = pgid;
968
Sage Weild85b7052010-05-10 10:24:48 -0700969 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
970 if (err > 0) {
971 o = acting[0];
972 num = err;
973 }
Sage Weilf24e9982009-10-06 11:31:10 -0700974
Sage Weil38d64532011-10-14 13:33:55 -0700975 if ((!force_resend &&
976 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700977 req->r_sent >= req->r_osd->o_incarnation &&
978 req->r_num_pg_osds == num &&
979 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700980 (req->r_osd == NULL && o == -1))
981 return 0; /* no change */
982
Sage Weil6f6c7002011-01-17 20:34:08 -0800983 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800984 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700985 req->r_osd ? req->r_osd->o_osd : -1);
986
Sage Weild85b7052010-05-10 10:24:48 -0700987 /* record full pg acting set */
988 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
989 req->r_num_pg_osds = num;
990
Sage Weilf24e9982009-10-06 11:31:10 -0700991 if (req->r_osd) {
992 __cancel_request(req);
993 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700994 req->r_osd = NULL;
995 }
996
997 req->r_osd = __lookup_osd(osdc, o);
998 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800999 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001000 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001001 if (!req->r_osd) {
1002 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001003 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001004 }
Sage Weilf24e9982009-10-06 11:31:10 -07001005
Sage Weil6f6c7002011-01-17 20:34:08 -08001006 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001007 __insert_osd(osdc, req->r_osd);
1008
1009 ceph_con_open(&req->r_osd->o_con, &osdc->osdmap->osd_addr[o]);
1010 }
1011
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001012 if (req->r_osd) {
1013 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001014 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001015 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1016 } else {
1017 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001018 }
Sage Weild85b7052010-05-10 10:24:48 -07001019 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001020
1021out:
Sage Weilf24e9982009-10-06 11:31:10 -07001022 return err;
1023}
1024
1025/*
1026 * caller should hold map_sem (for read) and request_mutex
1027 */
Sage Weil56e925b2012-01-03 12:34:34 -08001028static void __send_request(struct ceph_osd_client *osdc,
1029 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001030{
1031 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001032
1033 dout("send_request %p tid %llu to osd%d flags %d\n",
1034 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1035
1036 reqhead = req->r_request->front.iov_base;
1037 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1038 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1039 reqhead->reassert_version = req->r_reassert_version;
1040
Sage Weil3dd72fc2010-03-22 14:42:30 -07001041 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001042 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001043
1044 ceph_msg_get(req->r_request); /* send consumes a ref */
1045 ceph_con_send(&req->r_osd->o_con, req->r_request);
1046 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001047}
1048
1049/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001050 * Send any requests in the queue (req_unsent).
1051 */
1052static void send_queued(struct ceph_osd_client *osdc)
1053{
1054 struct ceph_osd_request *req, *tmp;
1055
1056 dout("send_queued\n");
1057 mutex_lock(&osdc->request_mutex);
1058 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1059 __send_request(osdc, req);
1060 }
1061 mutex_unlock(&osdc->request_mutex);
1062}
1063
1064/*
Sage Weilf24e9982009-10-06 11:31:10 -07001065 * Timeout callback, called every N seconds when 1 or more osd
1066 * requests has been active for more than N seconds. When this
1067 * happens, we ping all OSDs with requests who have timed out to
1068 * ensure any communications channel reset is detected. Reset the
1069 * request timeouts another N seconds in the future as we go.
1070 * Reschedule the timeout event another N seconds in future (unless
1071 * there are no open requests).
1072 */
1073static void handle_timeout(struct work_struct *work)
1074{
1075 struct ceph_osd_client *osdc =
1076 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001077 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001078 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001079 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001080 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001081 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001082 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001083 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001084 dout("timeout\n");
1085 down_read(&osdc->map_sem);
1086
1087 ceph_monc_request_next_osdmap(&osdc->client->monc);
1088
1089 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001090
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001091 /*
1092 * reset osds that appear to be _really_ unresponsive. this
1093 * is a failsafe measure.. we really shouldn't be getting to
1094 * this point if the system is working properly. the monitors
1095 * should mark the osd as failed and we should find out about
1096 * it from an updated osd map.
1097 */
Sage Weilf26e6812010-04-21 11:09:38 -07001098 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001099 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1100 r_req_lru_item);
1101
Sage Weil4cf9d542011-07-26 11:27:24 -07001102 /* hasn't been long enough since we sent it? */
Sage Weil3dd72fc2010-03-22 14:42:30 -07001103 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001104 break;
1105
Sage Weil4cf9d542011-07-26 11:27:24 -07001106 /* hasn't been long enough since it was acked? */
1107 if (req->r_request->ack_stamp == 0 ||
1108 time_before(jiffies, req->r_request->ack_stamp + timeout))
1109 break;
1110
Sage Weil3dd72fc2010-03-22 14:42:30 -07001111 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001112 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001113 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001114
1115 osd = req->r_osd;
1116 BUG_ON(!osd);
1117 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1118 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001119 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001120 }
1121
1122 /*
1123 * ping osds that are a bit slow. this ensures that if there
1124 * is a break in the TCP connection we will notice, and reopen
1125 * a connection with that osd (from the fault callback).
1126 */
1127 INIT_LIST_HEAD(&slow_osds);
1128 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001129 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001130 break;
1131
1132 osd = req->r_osd;
1133 BUG_ON(!osd);
1134 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001135 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001136 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1137 }
1138 while (!list_empty(&slow_osds)) {
1139 osd = list_entry(slow_osds.next, struct ceph_osd,
1140 o_keepalive_item);
1141 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001142 ceph_con_keepalive(&osd->o_con);
1143 }
1144
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001145 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001146 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001147 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001148 up_read(&osdc->map_sem);
1149}
1150
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001151static void handle_osds_timeout(struct work_struct *work)
1152{
1153 struct ceph_osd_client *osdc =
1154 container_of(work, struct ceph_osd_client,
1155 osds_timeout_work.work);
1156 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001157 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001158
1159 dout("osds timeout\n");
1160 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001161 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001162 up_read(&osdc->map_sem);
1163
1164 schedule_delayed_work(&osdc->osds_timeout_work,
1165 round_jiffies_relative(delay));
1166}
1167
Sage Weil25845472011-06-03 09:37:09 -07001168static void complete_request(struct ceph_osd_request *req)
1169{
1170 if (req->r_safe_callback)
1171 req->r_safe_callback(req, NULL);
1172 complete_all(&req->r_safe_completion); /* fsync waiter */
1173}
1174
Sage Weilf24e9982009-10-06 11:31:10 -07001175/*
1176 * handle osd op reply. either call the callback if it is specified,
1177 * or do the completion to wake up the waiting thread.
1178 */
Sage Weil350b1c32009-12-22 10:45:45 -08001179static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1180 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001181{
1182 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1183 struct ceph_osd_request *req;
1184 u64 tid;
1185 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001186 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001187
Sage Weil6df058c2009-12-22 11:24:33 -08001188 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001189 if (msg->front.iov_len < sizeof(*rhead))
1190 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001191 numops = le32_to_cpu(rhead->num_ops);
1192 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001193 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001194 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1195 numops * sizeof(struct ceph_osd_op))
1196 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001197 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001198 /* lookup */
1199 mutex_lock(&osdc->request_mutex);
1200 req = __lookup_request(osdc, tid);
1201 if (req == NULL) {
1202 dout("handle_reply tid %llu dne\n", tid);
1203 mutex_unlock(&osdc->request_mutex);
1204 return;
1205 }
1206 ceph_osdc_get_request(req);
1207 flags = le32_to_cpu(rhead->flags);
1208
Sage Weil350b1c32009-12-22 10:45:45 -08001209 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001210 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001211 * avoid a (safe but slower) revoke later.
1212 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001213 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001214 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001215 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001216 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001217 }
1218
Sage Weilf24e9982009-10-06 11:31:10 -07001219 if (!req->r_got_reply) {
1220 unsigned bytes;
1221
1222 req->r_result = le32_to_cpu(rhead->result);
1223 bytes = le32_to_cpu(msg->hdr.data_len);
1224 dout("handle_reply result %d bytes %d\n", req->r_result,
1225 bytes);
1226 if (req->r_result == 0)
1227 req->r_result = bytes;
1228
1229 /* in case this is a write and we need to replay, */
1230 req->r_reassert_version = rhead->reassert_version;
1231
1232 req->r_got_reply = 1;
1233 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1234 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001235 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001236 goto done;
1237 }
1238
1239 dout("handle_reply tid %llu flags %d\n", tid, flags);
1240
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001241 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1242 __register_linger_request(osdc, req);
1243
Sage Weilf24e9982009-10-06 11:31:10 -07001244 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001245 if (result < 0 ||
1246 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001247 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1248 __unregister_request(osdc, req);
1249
1250 mutex_unlock(&osdc->request_mutex);
1251
1252 if (req->r_callback)
1253 req->r_callback(req, msg);
1254 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001255 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001256
Sage Weil25845472011-06-03 09:37:09 -07001257 if (flags & CEPH_OSD_FLAG_ONDISK)
1258 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001259
1260done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001261 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001262 ceph_osdc_put_request(req);
1263 return;
1264
1265bad:
1266 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1267 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1268 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001269 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001270}
1271
Sage Weil6f6c7002011-01-17 20:34:08 -08001272static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001273{
Sage Weilf24e9982009-10-06 11:31:10 -07001274 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001275
Sage Weil6f6c7002011-01-17 20:34:08 -08001276 for (p = rb_first(&osdc->osds); p; p = n) {
1277 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001278
Sage Weil6f6c7002011-01-17 20:34:08 -08001279 n = rb_next(p);
1280 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1281 memcmp(&osd->o_con.peer_addr,
1282 ceph_osd_addr(osdc->osdmap,
1283 osd->o_osd),
1284 sizeof(struct ceph_entity_addr)) != 0)
1285 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001286 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001287}
1288
1289/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001290 * Requeue requests whose mapping to an OSD has changed. If requests map to
1291 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001292 *
1293 * Caller should hold map_sem for read and request_mutex.
1294 */
Sage Weil38d64532011-10-14 13:33:55 -07001295static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001296{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001297 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001298 struct rb_node *p;
1299 int needmap = 0;
1300 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001301
Sage Weil38d64532011-10-14 13:33:55 -07001302 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001303 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001304 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1305 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil38d64532011-10-14 13:33:55 -07001306 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001307 if (err < 0)
1308 continue; /* error */
1309 if (req->r_osd == NULL) {
1310 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1311 needmap++; /* request a newer map */
1312 } else if (err > 0) {
1313 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1314 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001315 if (!req->r_linger)
1316 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001317 }
1318 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001319
1320 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1321 r_linger_item) {
1322 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1323
Sage Weil38d64532011-10-14 13:33:55 -07001324 err = __map_request(osdc, req, force_resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001325 if (err == 0)
1326 continue; /* no change and no osd was specified */
1327 if (err < 0)
1328 continue; /* hrm! */
1329 if (req->r_osd == NULL) {
1330 dout("tid %llu maps to no valid osd\n", req->r_tid);
1331 needmap++; /* request a newer map */
1332 continue;
1333 }
1334
1335 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1336 req->r_osd ? req->r_osd->o_osd : -1);
1337 __unregister_linger_request(osdc, req);
1338 __register_request(osdc, req);
1339 }
Sage Weilf24e9982009-10-06 11:31:10 -07001340 mutex_unlock(&osdc->request_mutex);
1341
1342 if (needmap) {
1343 dout("%d requests for down osds, need new map\n", needmap);
1344 ceph_monc_request_next_osdmap(&osdc->client->monc);
1345 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001346}
Sage Weil6f6c7002011-01-17 20:34:08 -08001347
1348
Sage Weilf24e9982009-10-06 11:31:10 -07001349/*
1350 * Process updated osd map.
1351 *
1352 * The message contains any number of incremental and full maps, normally
1353 * indicating some sort of topology change in the cluster. Kick requests
1354 * off to different OSDs as needed.
1355 */
1356void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1357{
1358 void *p, *end, *next;
1359 u32 nr_maps, maplen;
1360 u32 epoch;
1361 struct ceph_osdmap *newmap = NULL, *oldmap;
1362 int err;
1363 struct ceph_fsid fsid;
1364
1365 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1366 p = msg->front.iov_base;
1367 end = p + msg->front.iov_len;
1368
1369 /* verify fsid */
1370 ceph_decode_need(&p, end, sizeof(fsid), bad);
1371 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001372 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1373 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001374
1375 down_write(&osdc->map_sem);
1376
1377 /* incremental maps */
1378 ceph_decode_32_safe(&p, end, nr_maps, bad);
1379 dout(" %d inc maps\n", nr_maps);
1380 while (nr_maps > 0) {
1381 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001382 epoch = ceph_decode_32(&p);
1383 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001384 ceph_decode_need(&p, end, maplen, bad);
1385 next = p + maplen;
1386 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1387 dout("applying incremental map %u len %d\n",
1388 epoch, maplen);
1389 newmap = osdmap_apply_incremental(&p, next,
1390 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001391 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001392 if (IS_ERR(newmap)) {
1393 err = PTR_ERR(newmap);
1394 goto bad;
1395 }
Sage Weil30dc6382009-12-21 14:49:37 -08001396 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001397 if (newmap != osdc->osdmap) {
1398 ceph_osdmap_destroy(osdc->osdmap);
1399 osdc->osdmap = newmap;
1400 }
Sage Weil38d64532011-10-14 13:33:55 -07001401 kick_requests(osdc, 0);
Sage Weil6f6c7002011-01-17 20:34:08 -08001402 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001403 } else {
1404 dout("ignoring incremental map %u len %d\n",
1405 epoch, maplen);
1406 }
1407 p = next;
1408 nr_maps--;
1409 }
1410 if (newmap)
1411 goto done;
1412
1413 /* full maps */
1414 ceph_decode_32_safe(&p, end, nr_maps, bad);
1415 dout(" %d full maps\n", nr_maps);
1416 while (nr_maps) {
1417 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001418 epoch = ceph_decode_32(&p);
1419 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001420 ceph_decode_need(&p, end, maplen, bad);
1421 if (nr_maps > 1) {
1422 dout("skipping non-latest full map %u len %d\n",
1423 epoch, maplen);
1424 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1425 dout("skipping full map %u len %d, "
1426 "older than our %u\n", epoch, maplen,
1427 osdc->osdmap->epoch);
1428 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001429 int skipped_map = 0;
1430
Sage Weilf24e9982009-10-06 11:31:10 -07001431 dout("taking full map %u len %d\n", epoch, maplen);
1432 newmap = osdmap_decode(&p, p+maplen);
1433 if (IS_ERR(newmap)) {
1434 err = PTR_ERR(newmap);
1435 goto bad;
1436 }
Sage Weil30dc6382009-12-21 14:49:37 -08001437 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001438 oldmap = osdc->osdmap;
1439 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001440 if (oldmap) {
1441 if (oldmap->epoch + 1 < newmap->epoch)
1442 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001443 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001444 }
1445 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001446 }
1447 p += maplen;
1448 nr_maps--;
1449 }
1450
1451done:
1452 downgrade_write(&osdc->map_sem);
1453 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001454
1455 /*
1456 * subscribe to subsequent osdmap updates if full to ensure
1457 * we find out when we are no longer full and stop returning
1458 * ENOSPC.
1459 */
1460 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1461 ceph_monc_request_next_osdmap(&osdc->client->monc);
1462
Sage Weil6f6c7002011-01-17 20:34:08 -08001463 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001464 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001465 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001466 return;
1467
1468bad:
1469 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001470 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001471 up_write(&osdc->map_sem);
1472 return;
1473}
1474
Sage Weilf24e9982009-10-06 11:31:10 -07001475/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001476 * watch/notify callback event infrastructure
1477 *
1478 * These callbacks are used both for watch and notify operations.
1479 */
1480static void __release_event(struct kref *kref)
1481{
1482 struct ceph_osd_event *event =
1483 container_of(kref, struct ceph_osd_event, kref);
1484
1485 dout("__release_event %p\n", event);
1486 kfree(event);
1487}
1488
1489static void get_event(struct ceph_osd_event *event)
1490{
1491 kref_get(&event->kref);
1492}
1493
1494void ceph_osdc_put_event(struct ceph_osd_event *event)
1495{
1496 kref_put(&event->kref, __release_event);
1497}
1498EXPORT_SYMBOL(ceph_osdc_put_event);
1499
1500static void __insert_event(struct ceph_osd_client *osdc,
1501 struct ceph_osd_event *new)
1502{
1503 struct rb_node **p = &osdc->event_tree.rb_node;
1504 struct rb_node *parent = NULL;
1505 struct ceph_osd_event *event = NULL;
1506
1507 while (*p) {
1508 parent = *p;
1509 event = rb_entry(parent, struct ceph_osd_event, node);
1510 if (new->cookie < event->cookie)
1511 p = &(*p)->rb_left;
1512 else if (new->cookie > event->cookie)
1513 p = &(*p)->rb_right;
1514 else
1515 BUG();
1516 }
1517
1518 rb_link_node(&new->node, parent, p);
1519 rb_insert_color(&new->node, &osdc->event_tree);
1520}
1521
1522static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1523 u64 cookie)
1524{
1525 struct rb_node **p = &osdc->event_tree.rb_node;
1526 struct rb_node *parent = NULL;
1527 struct ceph_osd_event *event = NULL;
1528
1529 while (*p) {
1530 parent = *p;
1531 event = rb_entry(parent, struct ceph_osd_event, node);
1532 if (cookie < event->cookie)
1533 p = &(*p)->rb_left;
1534 else if (cookie > event->cookie)
1535 p = &(*p)->rb_right;
1536 else
1537 return event;
1538 }
1539 return NULL;
1540}
1541
1542static void __remove_event(struct ceph_osd_event *event)
1543{
1544 struct ceph_osd_client *osdc = event->osdc;
1545
1546 if (!RB_EMPTY_NODE(&event->node)) {
1547 dout("__remove_event removed %p\n", event);
1548 rb_erase(&event->node, &osdc->event_tree);
1549 ceph_osdc_put_event(event);
1550 } else {
1551 dout("__remove_event didn't remove %p\n", event);
1552 }
1553}
1554
1555int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1556 void (*event_cb)(u64, u64, u8, void *),
1557 int one_shot, void *data,
1558 struct ceph_osd_event **pevent)
1559{
1560 struct ceph_osd_event *event;
1561
1562 event = kmalloc(sizeof(*event), GFP_NOIO);
1563 if (!event)
1564 return -ENOMEM;
1565
1566 dout("create_event %p\n", event);
1567 event->cb = event_cb;
1568 event->one_shot = one_shot;
1569 event->data = data;
1570 event->osdc = osdc;
1571 INIT_LIST_HEAD(&event->osd_node);
1572 kref_init(&event->kref); /* one ref for us */
1573 kref_get(&event->kref); /* one ref for the caller */
1574 init_completion(&event->completion);
1575
1576 spin_lock(&osdc->event_lock);
1577 event->cookie = ++osdc->event_count;
1578 __insert_event(osdc, event);
1579 spin_unlock(&osdc->event_lock);
1580
1581 *pevent = event;
1582 return 0;
1583}
1584EXPORT_SYMBOL(ceph_osdc_create_event);
1585
1586void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1587{
1588 struct ceph_osd_client *osdc = event->osdc;
1589
1590 dout("cancel_event %p\n", event);
1591 spin_lock(&osdc->event_lock);
1592 __remove_event(event);
1593 spin_unlock(&osdc->event_lock);
1594 ceph_osdc_put_event(event); /* caller's */
1595}
1596EXPORT_SYMBOL(ceph_osdc_cancel_event);
1597
1598
1599static void do_event_work(struct work_struct *work)
1600{
1601 struct ceph_osd_event_work *event_work =
1602 container_of(work, struct ceph_osd_event_work, work);
1603 struct ceph_osd_event *event = event_work->event;
1604 u64 ver = event_work->ver;
1605 u64 notify_id = event_work->notify_id;
1606 u8 opcode = event_work->opcode;
1607
1608 dout("do_event_work completing %p\n", event);
1609 event->cb(ver, notify_id, opcode, event->data);
1610 complete(&event->completion);
1611 dout("do_event_work completed %p\n", event);
1612 ceph_osdc_put_event(event);
1613 kfree(event_work);
1614}
1615
1616
1617/*
1618 * Process osd watch notifications
1619 */
1620void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1621{
1622 void *p, *end;
1623 u8 proto_ver;
1624 u64 cookie, ver, notify_id;
1625 u8 opcode;
1626 struct ceph_osd_event *event;
1627 struct ceph_osd_event_work *event_work;
1628
1629 p = msg->front.iov_base;
1630 end = p + msg->front.iov_len;
1631
1632 ceph_decode_8_safe(&p, end, proto_ver, bad);
1633 ceph_decode_8_safe(&p, end, opcode, bad);
1634 ceph_decode_64_safe(&p, end, cookie, bad);
1635 ceph_decode_64_safe(&p, end, ver, bad);
1636 ceph_decode_64_safe(&p, end, notify_id, bad);
1637
1638 spin_lock(&osdc->event_lock);
1639 event = __find_event(osdc, cookie);
1640 if (event) {
1641 get_event(event);
1642 if (event->one_shot)
1643 __remove_event(event);
1644 }
1645 spin_unlock(&osdc->event_lock);
1646 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1647 cookie, ver, event);
1648 if (event) {
1649 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001650 if (!event_work) {
1651 dout("ERROR: could not allocate event_work\n");
1652 goto done_err;
1653 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001654 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001655 event_work->event = event;
1656 event_work->ver = ver;
1657 event_work->notify_id = notify_id;
1658 event_work->opcode = opcode;
1659 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1660 dout("WARNING: failed to queue notify event work\n");
1661 goto done_err;
1662 }
1663 }
1664
1665 return;
1666
1667done_err:
1668 complete(&event->completion);
1669 ceph_osdc_put_event(event);
1670 return;
1671
1672bad:
1673 pr_err("osdc handle_watch_notify corrupt msg\n");
1674 return;
1675}
1676
1677int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1678{
1679 int err;
1680
1681 dout("wait_event %p\n", event);
1682 err = wait_for_completion_interruptible_timeout(&event->completion,
1683 timeout * HZ);
1684 ceph_osdc_put_event(event);
1685 if (err > 0)
1686 err = 0;
1687 dout("wait_event %p returns %d\n", event, err);
1688 return err;
1689}
1690EXPORT_SYMBOL(ceph_osdc_wait_event);
1691
1692/*
Sage Weilf24e9982009-10-06 11:31:10 -07001693 * Register request, send initial attempt.
1694 */
1695int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1696 struct ceph_osd_request *req,
1697 bool nofail)
1698{
Sage Weilc1ea8822009-10-08 16:55:47 -07001699 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001700
1701 req->r_request->pages = req->r_pages;
1702 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001703#ifdef CONFIG_BLOCK
1704 req->r_request->bio = req->r_bio;
1705#endif
1706 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001707
1708 register_request(osdc, req);
1709
1710 down_read(&osdc->map_sem);
1711 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001712 /*
1713 * a racing kick_requests() may have sent the message for us
1714 * while we dropped request_mutex above, so only send now if
1715 * the request still han't been touched yet.
1716 */
1717 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001718 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001719 if (rc < 0) {
1720 if (nofail) {
1721 dout("osdc_start_request failed map, "
1722 " will retry %lld\n", req->r_tid);
1723 rc = 0;
1724 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001725 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001726 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001727 if (req->r_osd == NULL) {
1728 dout("send_request %p no up osds in pg\n", req);
1729 ceph_monc_request_next_osdmap(&osdc->client->monc);
1730 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001731 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001732 }
Sage Weil56e925b2012-01-03 12:34:34 -08001733 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001734 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001735
1736out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001737 mutex_unlock(&osdc->request_mutex);
1738 up_read(&osdc->map_sem);
1739 return rc;
1740}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001741EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001742
1743/*
1744 * wait for a request to complete
1745 */
1746int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1747 struct ceph_osd_request *req)
1748{
1749 int rc;
1750
1751 rc = wait_for_completion_interruptible(&req->r_completion);
1752 if (rc < 0) {
1753 mutex_lock(&osdc->request_mutex);
1754 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001755 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001756 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001757 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001758 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001759 return rc;
1760 }
1761
1762 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1763 return req->r_result;
1764}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001765EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001766
1767/*
1768 * sync - wait for all in-flight requests to flush. avoid starvation.
1769 */
1770void ceph_osdc_sync(struct ceph_osd_client *osdc)
1771{
1772 struct ceph_osd_request *req;
1773 u64 last_tid, next_tid = 0;
1774
1775 mutex_lock(&osdc->request_mutex);
1776 last_tid = osdc->last_tid;
1777 while (1) {
1778 req = __lookup_request_ge(osdc, next_tid);
1779 if (!req)
1780 break;
1781 if (req->r_tid > last_tid)
1782 break;
1783
1784 next_tid = req->r_tid + 1;
1785 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1786 continue;
1787
1788 ceph_osdc_get_request(req);
1789 mutex_unlock(&osdc->request_mutex);
1790 dout("sync waiting on tid %llu (last is %llu)\n",
1791 req->r_tid, last_tid);
1792 wait_for_completion(&req->r_safe_completion);
1793 mutex_lock(&osdc->request_mutex);
1794 ceph_osdc_put_request(req);
1795 }
1796 mutex_unlock(&osdc->request_mutex);
1797 dout("sync done (thru tid %llu)\n", last_tid);
1798}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001799EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001800
1801/*
1802 * init, shutdown
1803 */
1804int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1805{
1806 int err;
1807
1808 dout("init\n");
1809 osdc->client = client;
1810 osdc->osdmap = NULL;
1811 init_rwsem(&osdc->map_sem);
1812 init_completion(&osdc->map_waiters);
1813 osdc->last_requested_map = 0;
1814 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001815 osdc->last_tid = 0;
1816 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001817 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001818 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001819 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001820 INIT_LIST_HEAD(&osdc->req_unsent);
1821 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001822 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001823 osdc->num_requests = 0;
1824 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001825 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001826 spin_lock_init(&osdc->event_lock);
1827 osdc->event_tree = RB_ROOT;
1828 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001829
1830 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001831 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001832
Sage Weil5f44f142009-11-18 14:52:18 -08001833 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001834 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1835 sizeof(struct ceph_osd_request));
1836 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001837 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001838
Sage Weil4f482802010-04-24 09:56:35 -07001839 err = ceph_msgpool_init(&osdc->msgpool_op, OSD_OP_FRONT_LEN, 10, true,
1840 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001841 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001842 goto out_mempool;
Sage Weilc16e7862010-03-01 13:02:00 -08001843 err = ceph_msgpool_init(&osdc->msgpool_op_reply,
Sage Weil4f482802010-04-24 09:56:35 -07001844 OSD_OPREPLY_FRONT_LEN, 10, true,
1845 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001846 if (err < 0)
1847 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001848
1849 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1850 if (IS_ERR(osdc->notify_wq)) {
1851 err = PTR_ERR(osdc->notify_wq);
1852 osdc->notify_wq = NULL;
1853 goto out_msgpool;
1854 }
Sage Weilf24e9982009-10-06 11:31:10 -07001855 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001856
Sage Weilc16e7862010-03-01 13:02:00 -08001857out_msgpool:
1858 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001859out_mempool:
1860 mempool_destroy(osdc->req_mempool);
1861out:
1862 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001863}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001864EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001865
1866void ceph_osdc_stop(struct ceph_osd_client *osdc)
1867{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001868 flush_workqueue(osdc->notify_wq);
1869 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001870 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001871 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001872 if (osdc->osdmap) {
1873 ceph_osdmap_destroy(osdc->osdmap);
1874 osdc->osdmap = NULL;
1875 }
Sage Weilaca420b2011-08-31 14:45:53 -07001876 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001877 mempool_destroy(osdc->req_mempool);
1878 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001879 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001880}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001881EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001882
1883/*
1884 * Read some contiguous pages. If we cross a stripe boundary, shorten
1885 * *plen. Return number of bytes read, or error.
1886 */
1887int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1888 struct ceph_vino vino, struct ceph_file_layout *layout,
1889 u64 off, u64 *plen,
1890 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001891 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001892{
1893 struct ceph_osd_request *req;
1894 int rc = 0;
1895
1896 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1897 vino.snap, off, *plen);
1898 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1899 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1900 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001901 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001902 if (!req)
1903 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001904
1905 /* it may be a short read due to an object boundary */
1906 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001907
Sage Weilb7495fc2010-11-09 12:43:12 -08001908 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1909 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001910
1911 rc = ceph_osdc_start_request(osdc, req, false);
1912 if (!rc)
1913 rc = ceph_osdc_wait_request(osdc, req);
1914
1915 ceph_osdc_put_request(req);
1916 dout("readpages result %d\n", rc);
1917 return rc;
1918}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001919EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001920
1921/*
1922 * do a synchronous write on N pages
1923 */
1924int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1925 struct ceph_file_layout *layout,
1926 struct ceph_snap_context *snapc,
1927 u64 off, u64 len,
1928 u32 truncate_seq, u64 truncate_size,
1929 struct timespec *mtime,
1930 struct page **pages, int num_pages,
1931 int flags, int do_sync, bool nofail)
1932{
1933 struct ceph_osd_request *req;
1934 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001935 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001936
1937 BUG_ON(vino.snap != CEPH_NOSNAP);
1938 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1939 CEPH_OSD_OP_WRITE,
1940 flags | CEPH_OSD_FLAG_ONDISK |
1941 CEPH_OSD_FLAG_WRITE,
1942 snapc, do_sync,
1943 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001944 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001945 if (!req)
1946 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001947
1948 /* it may be a short write due to an object boundary */
1949 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001950 dout("writepages %llu~%llu (%d pages)\n", off, len,
1951 req->r_num_pages);
1952
1953 rc = ceph_osdc_start_request(osdc, req, nofail);
1954 if (!rc)
1955 rc = ceph_osdc_wait_request(osdc, req);
1956
1957 ceph_osdc_put_request(req);
1958 if (rc == 0)
1959 rc = len;
1960 dout("writepages result %d\n", rc);
1961 return rc;
1962}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001963EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001964
1965/*
1966 * handle incoming message
1967 */
1968static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1969{
1970 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001971 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001972 int type = le16_to_cpu(msg->hdr.type);
1973
1974 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001975 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001976 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001977
1978 switch (type) {
1979 case CEPH_MSG_OSD_MAP:
1980 ceph_osdc_handle_map(osdc, msg);
1981 break;
1982 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001983 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001984 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001985 case CEPH_MSG_WATCH_NOTIFY:
1986 handle_watch_notify(osdc, msg);
1987 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001988
1989 default:
1990 pr_err("received unknown message type %d %s\n", type,
1991 ceph_msg_type_name(type));
1992 }
Sage Weil4a32f932010-06-13 10:27:53 -07001993out:
Sage Weilf24e9982009-10-06 11:31:10 -07001994 ceph_msg_put(msg);
1995}
1996
Sage Weil5b3a4db2010-02-19 21:43:23 -08001997/*
Sage Weil21b667f2010-03-04 10:22:59 -08001998 * lookup and return message for incoming reply. set up reply message
1999 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002000 */
2001static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002002 struct ceph_msg_header *hdr,
2003 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002004{
2005 struct ceph_osd *osd = con->private;
2006 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002007 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002008 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002009 int front = le32_to_cpu(hdr->front_len);
2010 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002011 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002012
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002013 tid = le64_to_cpu(hdr->tid);
2014 mutex_lock(&osdc->request_mutex);
2015 req = __lookup_request(osdc, tid);
2016 if (!req) {
2017 *skip = 1;
2018 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08002019 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08002020 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002021 goto out;
2022 }
Sage Weilc16e7862010-03-01 13:02:00 -08002023
2024 if (req->r_con_filling_msg) {
2025 dout("get_reply revoking msg %p from old con %p\n",
2026 req->r_reply, req->r_con_filling_msg);
2027 ceph_con_revoke_message(req->r_con_filling_msg, req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002028 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002029 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002030 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002031
Sage Weilc16e7862010-03-01 13:02:00 -08002032 if (front > req->r_reply->front.iov_len) {
2033 pr_warning("get_reply front %d > preallocated %d\n",
2034 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002035 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002036 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002037 goto out;
2038 ceph_msg_put(req->r_reply);
2039 req->r_reply = m;
2040 }
2041 m = ceph_msg_get(req->r_reply);
2042
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002043 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002044 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002045
2046 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002047 pr_warning("tid %lld reply has %d bytes %d pages, we"
2048 " had only %d pages ready\n", tid, data_len,
2049 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002050 *skip = 1;
2051 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002052 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002053 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002054 }
Sage Weil21b667f2010-03-04 10:22:59 -08002055 m->pages = req->r_pages;
2056 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002057 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002058#ifdef CONFIG_BLOCK
2059 m->bio = req->r_bio;
2060#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002061 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002062 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002063 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002064 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002065
2066out:
2067 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002068 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002069
2070}
2071
2072static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2073 struct ceph_msg_header *hdr,
2074 int *skip)
2075{
2076 struct ceph_osd *osd = con->private;
2077 int type = le16_to_cpu(hdr->type);
2078 int front = le32_to_cpu(hdr->front_len);
2079
Alex Elder1c20f2d2012-06-04 14:43:32 -05002080 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002081 switch (type) {
2082 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002083 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002084 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002085 case CEPH_MSG_OSD_OPREPLY:
2086 return get_reply(con, hdr, skip);
2087 default:
2088 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2089 osd->o_osd);
2090 *skip = 1;
2091 return NULL;
2092 }
Sage Weilf24e9982009-10-06 11:31:10 -07002093}
2094
2095/*
2096 * Wrappers to refcount containing ceph_osd struct
2097 */
2098static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2099{
2100 struct ceph_osd *osd = con->private;
2101 if (get_osd(osd))
2102 return con;
2103 return NULL;
2104}
2105
2106static void put_osd_con(struct ceph_connection *con)
2107{
2108 struct ceph_osd *osd = con->private;
2109 put_osd(osd);
2110}
2111
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002112/*
2113 * authentication
2114 */
Alex Eldera3530df2012-05-16 15:16:39 -05002115/*
2116 * Note: returned pointer is the address of a structure that's
2117 * managed separately. Caller must *not* attempt to free it.
2118 */
2119static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002120 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002121{
2122 struct ceph_osd *o = con->private;
2123 struct ceph_osd_client *osdc = o->o_osdc;
2124 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002125 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002126
Alex Elder74f18692012-05-16 15:16:39 -05002127 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002128 if (ac->ops && ac->ops->destroy_authorizer)
2129 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002130 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002131 }
Alex Eldera2556512012-05-16 15:16:39 -05002132 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002133 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2134 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002135 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002136 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002137 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002138 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002139
Alex Eldera3530df2012-05-16 15:16:39 -05002140 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002141}
2142
2143
2144static int verify_authorizer_reply(struct ceph_connection *con, int len)
2145{
2146 struct ceph_osd *o = con->private;
2147 struct ceph_osd_client *osdc = o->o_osdc;
2148 struct ceph_auth_client *ac = osdc->client->monc.auth;
2149
Alex Eldera2556512012-05-16 15:16:39 -05002150 /*
2151 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2152 * XXX which do we do: succeed or fail?
2153 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002154 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002155}
2156
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002157static int invalidate_authorizer(struct ceph_connection *con)
2158{
2159 struct ceph_osd *o = con->private;
2160 struct ceph_osd_client *osdc = o->o_osdc;
2161 struct ceph_auth_client *ac = osdc->client->monc.auth;
2162
Alex Eldera2556512012-05-16 15:16:39 -05002163 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002164 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2165
2166 return ceph_monc_validate_auth(&osdc->client->monc);
2167}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002168
Tobias Klauser9e327892010-05-20 10:40:19 +02002169static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002170 .get = get_osd_con,
2171 .put = put_osd_con,
2172 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002173 .get_authorizer = get_authorizer,
2174 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002175 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002176 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002177 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002178};