blob: f7b56e23988c62e9ea776d9dd31b1a53c8b450e3 [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
Sage Weild63b77f2012-09-24 20:59:48 -070055int ceph_calc_raw_layout(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070056 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 */
Sage Weild63b77f2012-09-24 20:59:48 -070065 int r;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070066
67 reqhead->snapid = cpu_to_le64(snapid);
68
69 /* object extent? */
Sage Weild63b77f2012-09-24 20:59:48 -070070 r = ceph_calc_file_object_mapping(layout, off, plen, bno,
71 &objoff, &objlen);
72 if (r < 0)
73 return r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -070074 if (*plen < orig_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070075 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070076 orig_len - *plen, off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070077
Yehuda Sadeh68b44762010-04-06 15:01:27 -070078 if (op_has_extent(op->op)) {
79 op->extent.offset = objoff;
80 op->extent.length = objlen;
81 }
82 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080083 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070084 if (op->op == CEPH_OSD_OP_WRITE)
85 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070086
87 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
88 *bno, objoff, objlen, req->r_num_pages);
Sage Weild63b77f2012-09-24 20:59:48 -070089 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070090}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070091EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070092
Sage Weilf24e9982009-10-06 11:31:10 -070093/*
94 * Implement client access to distributed object storage cluster.
95 *
96 * All data objects are stored within a cluster/cloud of OSDs, or
97 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
98 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
99 * remote daemons serving up and coordinating consistent and safe
100 * access to storage.
101 *
102 * Cluster membership and the mapping of data objects onto storage devices
103 * are described by the osd map.
104 *
105 * We keep track of pending OSD requests (read, write), resubmit
106 * requests to different OSDs when the cluster topology/data layout
107 * change, or retry the affected requests when the communications
108 * channel with an OSD is reset.
109 */
110
111/*
112 * calculate the mapping of a file extent onto an object, and fill out the
113 * request accordingly. shorten extent as necessary if it crosses an
114 * object boundary.
115 *
116 * fill osd op in request message.
117 */
Sage Weild63b77f2012-09-24 20:59:48 -0700118static int calc_layout(struct ceph_osd_client *osdc,
119 struct ceph_vino vino,
120 struct ceph_file_layout *layout,
121 u64 off, u64 *plen,
122 struct ceph_osd_request *req,
123 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700124{
Sage Weilf24e9982009-10-06 11:31:10 -0700125 u64 bno;
Sage Weild63b77f2012-09-24 20:59:48 -0700126 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700127
Sage Weild63b77f2012-09-24 20:59:48 -0700128 r = ceph_calc_raw_layout(osdc, layout, vino.snap, off,
129 plen, &bno, req, op);
130 if (r < 0)
131 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700132
Sage Weil2dab0362011-05-12 14:29:51 -0700133 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
Sage Weilf24e9982009-10-06 11:31:10 -0700134 req->r_oid_len = strlen(req->r_oid);
Sage Weild63b77f2012-09-24 20:59:48 -0700135
136 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700137}
138
Sage Weilf24e9982009-10-06 11:31:10 -0700139/*
140 * requests
141 */
Sage Weil415e49a2009-12-07 13:37:03 -0800142void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700143{
Sage Weil415e49a2009-12-07 13:37:03 -0800144 struct ceph_osd_request *req = container_of(kref,
145 struct ceph_osd_request,
146 r_kref);
147
148 if (req->r_request)
149 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800150 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -0500151 dout("%s revoking pages %p from con %p\n", __func__,
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800152 req->r_pages, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500153 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700154 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800155 }
Alex Elderab8cb342012-06-04 14:43:32 -0500156 if (req->r_reply)
157 ceph_msg_put(req->r_reply);
Sage Weil415e49a2009-12-07 13:37:03 -0800158 if (req->r_own_pages)
159 ceph_release_page_vector(req->r_pages,
160 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700161#ifdef CONFIG_BLOCK
162 if (req->r_bio)
163 bio_put(req->r_bio);
164#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800165 ceph_put_snap_context(req->r_snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700166 if (req->r_trail) {
167 ceph_pagelist_release(req->r_trail);
168 kfree(req->r_trail);
169 }
Sage Weil415e49a2009-12-07 13:37:03 -0800170 if (req->r_mempool)
171 mempool_free(req, req->r_osdc->req_mempool);
172 else
173 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700174}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700175EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700176
177static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
178{
179 int i = 0;
180
181 if (needs_trail)
182 *needs_trail = 0;
183 while (ops[i].op) {
184 if (needs_trail && op_needs_trail(ops[i].op))
185 *needs_trail = 1;
186 i++;
187 }
188
189 return i;
190}
191
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700192struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
193 int flags,
194 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700195 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700196 bool use_mempool,
197 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700198 struct page **pages,
199 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700200{
201 struct ceph_osd_request *req;
202 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700203 int needs_trail;
204 int num_op = get_num_ops(ops, &needs_trail);
205 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700206
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700207 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700208
209 if (use_mempool) {
210 req = mempool_alloc(osdc->req_mempool, gfp_flags);
211 memset(req, 0, sizeof(*req));
212 } else {
213 req = kzalloc(sizeof(*req), gfp_flags);
214 }
215 if (req == NULL)
216 return NULL;
217
218 req->r_osdc = osdc;
219 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700220
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700221 kref_init(&req->r_kref);
222 init_completion(&req->r_completion);
223 init_completion(&req->r_safe_completion);
Sage Weilcd430452012-07-09 14:31:41 -0700224 rb_init_node(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700225 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700226 INIT_LIST_HEAD(&req->r_linger_item);
227 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700228 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700229 INIT_LIST_HEAD(&req->r_osd_item);
230
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700231 req->r_flags = flags;
232
233 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
234
235 /* create reply message */
236 if (use_mempool)
237 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
238 else
239 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700240 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700241 if (!msg) {
242 ceph_osdc_put_request(req);
243 return NULL;
244 }
245 req->r_reply = msg;
246
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700247 /* allocate space for the trailing data */
248 if (needs_trail) {
249 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
250 if (!req->r_trail) {
251 ceph_osdc_put_request(req);
252 return NULL;
253 }
254 ceph_pagelist_init(req->r_trail);
255 }
Sage Weild50b4092012-07-09 14:22:34 -0700256
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700257 /* create request message; allow space for oid */
Stratos Psomadakis224736d2011-11-10 15:45:37 +0200258 msg_size += MAX_OBJ_NAME_SIZE;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700259 if (snapc)
260 msg_size += sizeof(u64) * snapc->num_snaps;
261 if (use_mempool)
262 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
263 else
Sage Weilb61c2762011-08-09 15:03:46 -0700264 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700265 if (!msg) {
266 ceph_osdc_put_request(req);
267 return NULL;
268 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700269
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700270 memset(msg->front.iov_base, 0, msg->front.iov_len);
271
272 req->r_request = msg;
273 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700274#ifdef CONFIG_BLOCK
275 if (bio) {
276 req->r_bio = bio;
277 bio_get(req->r_bio);
278 }
279#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700280
281 return req;
282}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700283EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700284
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700285static void osd_req_encode_op(struct ceph_osd_request *req,
286 struct ceph_osd_op *dst,
287 struct ceph_osd_req_op *src)
288{
289 dst->op = cpu_to_le16(src->op);
290
Alex Elder065a68f2012-04-20 15:49:43 -0500291 switch (src->op) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700292 case CEPH_OSD_OP_READ:
293 case CEPH_OSD_OP_WRITE:
294 dst->extent.offset =
295 cpu_to_le64(src->extent.offset);
296 dst->extent.length =
297 cpu_to_le64(src->extent.length);
298 dst->extent.truncate_size =
299 cpu_to_le64(src->extent.truncate_size);
300 dst->extent.truncate_seq =
301 cpu_to_le32(src->extent.truncate_seq);
302 break;
303
304 case CEPH_OSD_OP_GETXATTR:
305 case CEPH_OSD_OP_SETXATTR:
306 case CEPH_OSD_OP_CMPXATTR:
307 BUG_ON(!req->r_trail);
308
309 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
310 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
311 dst->xattr.cmp_op = src->xattr.cmp_op;
312 dst->xattr.cmp_mode = src->xattr.cmp_mode;
313 ceph_pagelist_append(req->r_trail, src->xattr.name,
314 src->xattr.name_len);
315 ceph_pagelist_append(req->r_trail, src->xattr.val,
316 src->xattr.value_len);
317 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700318 case CEPH_OSD_OP_CALL:
319 BUG_ON(!req->r_trail);
320
321 dst->cls.class_len = src->cls.class_len;
322 dst->cls.method_len = src->cls.method_len;
323 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
324
325 ceph_pagelist_append(req->r_trail, src->cls.class_name,
326 src->cls.class_len);
327 ceph_pagelist_append(req->r_trail, src->cls.method_name,
328 src->cls.method_len);
329 ceph_pagelist_append(req->r_trail, src->cls.indata,
330 src->cls.indata_len);
331 break;
332 case CEPH_OSD_OP_ROLLBACK:
333 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
334 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700335 case CEPH_OSD_OP_STARTSYNC:
336 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700337 case CEPH_OSD_OP_NOTIFY:
338 {
339 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
340 __le32 timeout = cpu_to_le32(src->watch.timeout);
341
342 BUG_ON(!req->r_trail);
343
344 ceph_pagelist_append(req->r_trail,
345 &prot_ver, sizeof(prot_ver));
346 ceph_pagelist_append(req->r_trail,
347 &timeout, sizeof(timeout));
348 }
349 case CEPH_OSD_OP_NOTIFY_ACK:
350 case CEPH_OSD_OP_WATCH:
351 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
352 dst->watch.ver = cpu_to_le64(src->watch.ver);
353 dst->watch.flag = src->watch.flag;
354 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700355 default:
356 pr_err("unrecognized osd opcode %d\n", dst->op);
357 WARN_ON(1);
358 break;
359 }
360 dst->payload_len = cpu_to_le32(src->payload_len);
361}
362
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700363/*
364 * build new request AND message
365 *
366 */
367void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700368 u64 off, u64 *plen,
369 struct ceph_osd_req_op *src_ops,
370 struct ceph_snap_context *snapc,
371 struct timespec *mtime,
372 const char *oid,
373 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374{
375 struct ceph_msg *msg = req->r_request;
376 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700377 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700378 struct ceph_osd_op *op;
379 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700380 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700381 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700382 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700383 u64 data_len = 0;
384 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700385
386 head = msg->front.iov_base;
387 op = (void *)(head + 1);
388 p = (void *)(op + num_op);
389
390 req->r_snapc = ceph_get_snap_context(snapc);
391
392 head->client_inc = cpu_to_le32(1); /* always, for now. */
393 head->flags = cpu_to_le32(flags);
394 if (flags & CEPH_OSD_FLAG_WRITE)
395 ceph_encode_timespec(&head->mtime, mtime);
396 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700398
399 /* fill in oid */
400 head->object_len = cpu_to_le32(oid_len);
401 memcpy(p, oid, oid_len);
402 p += oid_len;
403
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700404 src_op = src_ops;
405 while (src_op->op) {
406 osd_req_encode_op(req, op, src_op);
407 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700408 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700409 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700410
411 if (req->r_trail)
412 data_len += req->r_trail->length;
413
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700414 if (snapc) {
415 head->snap_seq = cpu_to_le64(snapc->seq);
416 head->num_snaps = cpu_to_le32(snapc->num_snaps);
417 for (i = 0; i < snapc->num_snaps; i++) {
418 put_unaligned_le64(snapc->snaps[i], p);
419 p += sizeof(u64);
420 }
421 }
422
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700423 if (flags & CEPH_OSD_FLAG_WRITE) {
424 req->r_request->hdr.data_off = cpu_to_le16(off);
425 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
426 } else if (data_len) {
427 req->r_request->hdr.data_off = 0;
428 req->r_request->hdr.data_len = cpu_to_le32(data_len);
429 }
430
Sage Weilc5c6b192010-11-09 12:40:00 -0800431 req->r_request->page_alignment = req->r_page_alignment;
432
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700433 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
434 msg_size = p - msg->front.iov_base;
435 msg->front.iov_len = msg_size;
436 msg->hdr.front_len = cpu_to_le32(msg_size);
437 return;
438}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700439EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700440
Sage Weilf24e9982009-10-06 11:31:10 -0700441/*
442 * build new request AND message, calculate layout, and adjust file
443 * extent as needed.
444 *
445 * if the file was recently truncated, we include information about its
446 * old and new size so that the object can be updated appropriately. (we
447 * avoid synchronously deleting truncated objects because it's slow.)
448 *
449 * if @do_sync, include a 'startsync' command so that the osd will flush
450 * data quickly.
451 */
452struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
453 struct ceph_file_layout *layout,
454 struct ceph_vino vino,
455 u64 off, u64 *plen,
456 int opcode, int flags,
457 struct ceph_snap_context *snapc,
458 int do_sync,
459 u32 truncate_seq,
460 u64 truncate_size,
461 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800462 bool use_mempool, int num_reply,
463 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700464{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700465 struct ceph_osd_req_op ops[3];
466 struct ceph_osd_request *req;
467
468 ops[0].op = opcode;
469 ops[0].extent.truncate_seq = truncate_seq;
470 ops[0].extent.truncate_size = truncate_size;
471 ops[0].payload_len = 0;
472
473 if (do_sync) {
474 ops[1].op = CEPH_OSD_OP_STARTSYNC;
475 ops[1].payload_len = 0;
476 ops[2].op = 0;
477 } else
478 ops[1].op = 0;
479
480 req = ceph_osdc_alloc_request(osdc, flags,
481 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700482 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700483 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700484 if (!req)
485 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700486
487 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700488 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700489 req->r_file_layout = *layout; /* keep a copy */
490
Sage Weil9bb0ce22011-06-13 16:20:18 -0700491 /* in case it differs from natural (file) alignment that
492 calc_layout filled in for us */
493 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800494 req->r_page_alignment = page_align;
495
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700496 ceph_osdc_build_request(req, off, plen, ops,
497 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700498 mtime,
499 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700500
Sage Weilf24e9982009-10-06 11:31:10 -0700501 return req;
502}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700503EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700504
505/*
506 * We keep osd requests in an rbtree, sorted by ->r_tid.
507 */
508static void __insert_request(struct ceph_osd_client *osdc,
509 struct ceph_osd_request *new)
510{
511 struct rb_node **p = &osdc->requests.rb_node;
512 struct rb_node *parent = NULL;
513 struct ceph_osd_request *req = NULL;
514
515 while (*p) {
516 parent = *p;
517 req = rb_entry(parent, struct ceph_osd_request, r_node);
518 if (new->r_tid < req->r_tid)
519 p = &(*p)->rb_left;
520 else if (new->r_tid > req->r_tid)
521 p = &(*p)->rb_right;
522 else
523 BUG();
524 }
525
526 rb_link_node(&new->r_node, parent, p);
527 rb_insert_color(&new->r_node, &osdc->requests);
528}
529
530static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
531 u64 tid)
532{
533 struct ceph_osd_request *req;
534 struct rb_node *n = osdc->requests.rb_node;
535
536 while (n) {
537 req = rb_entry(n, struct ceph_osd_request, r_node);
538 if (tid < req->r_tid)
539 n = n->rb_left;
540 else if (tid > req->r_tid)
541 n = n->rb_right;
542 else
543 return req;
544 }
545 return NULL;
546}
547
548static struct ceph_osd_request *
549__lookup_request_ge(struct ceph_osd_client *osdc,
550 u64 tid)
551{
552 struct ceph_osd_request *req;
553 struct rb_node *n = osdc->requests.rb_node;
554
555 while (n) {
556 req = rb_entry(n, struct ceph_osd_request, r_node);
557 if (tid < req->r_tid) {
558 if (!n->rb_left)
559 return req;
560 n = n->rb_left;
561 } else if (tid > req->r_tid) {
562 n = n->rb_right;
563 } else {
564 return req;
565 }
566 }
567 return NULL;
568}
569
Sage Weil6f6c7002011-01-17 20:34:08 -0800570/*
571 * Resubmit requests pending on the given osd.
572 */
573static void __kick_osd_requests(struct ceph_osd_client *osdc,
574 struct ceph_osd *osd)
575{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700576 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800577 int err;
578
579 dout("__kick_osd_requests osd%d\n", osd->o_osd);
580 err = __reset_osd(osdc, osd);
581 if (err == -EAGAIN)
582 return;
583
584 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
585 list_move(&req->r_req_lru_item, &osdc->req_unsent);
586 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
587 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700588 if (!req->r_linger)
589 req->r_flags |= CEPH_OSD_FLAG_RETRY;
590 }
591
592 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
593 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700594 /*
595 * reregister request prior to unregistering linger so
596 * that r_osd is preserved.
597 */
598 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700599 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700600 list_add(&req->r_req_lru_item, &osdc->req_unsent);
601 list_add(&req->r_osd_item, &req->r_osd->o_requests);
602 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700603 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
604 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800605 }
606}
607
608static void kick_osd_requests(struct ceph_osd_client *osdc,
609 struct ceph_osd *kickosd)
610{
611 mutex_lock(&osdc->request_mutex);
612 __kick_osd_requests(osdc, kickosd);
613 mutex_unlock(&osdc->request_mutex);
614}
Sage Weilf24e9982009-10-06 11:31:10 -0700615
616/*
Sage Weil81b024e2009-10-09 10:29:18 -0700617 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700618 */
619static void osd_reset(struct ceph_connection *con)
620{
621 struct ceph_osd *osd = con->private;
622 struct ceph_osd_client *osdc;
623
624 if (!osd)
625 return;
626 dout("osd_reset osd%d\n", osd->o_osd);
627 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700628 down_read(&osdc->map_sem);
Sage Weil6f6c7002011-01-17 20:34:08 -0800629 kick_osd_requests(osdc, osd);
630 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700631 up_read(&osdc->map_sem);
632}
633
634/*
635 * Track open sessions with osds.
636 */
Alex Eldere10006f2012-05-26 23:26:43 -0500637static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700638{
639 struct ceph_osd *osd;
640
641 osd = kzalloc(sizeof(*osd), GFP_NOFS);
642 if (!osd)
643 return NULL;
644
645 atomic_set(&osd->o_ref, 1);
646 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500647 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -0700648 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700649 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800650 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700651 osd->o_incarnation = 1;
652
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700653 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800654
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800655 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700656 return osd;
657}
658
659static struct ceph_osd *get_osd(struct ceph_osd *osd)
660{
661 if (atomic_inc_not_zero(&osd->o_ref)) {
662 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
663 atomic_read(&osd->o_ref));
664 return osd;
665 } else {
666 dout("get_osd %p FAIL\n", osd);
667 return NULL;
668 }
669}
670
671static void put_osd(struct ceph_osd *osd)
672{
673 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
674 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500675 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700676 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
677
Alex Eldera2556512012-05-16 15:16:39 -0500678 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500679 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700680 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700681 }
Sage Weilf24e9982009-10-06 11:31:10 -0700682}
683
684/*
685 * remove an osd from our map
686 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800687static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700688{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800689 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700690 BUG_ON(!list_empty(&osd->o_requests));
691 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800692 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700693 ceph_con_close(&osd->o_con);
694 put_osd(osd);
695}
696
Sage Weilaca420b2011-08-31 14:45:53 -0700697static void remove_all_osds(struct ceph_osd_client *osdc)
698{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500699 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700700 mutex_lock(&osdc->request_mutex);
701 while (!RB_EMPTY_ROOT(&osdc->osds)) {
702 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
703 struct ceph_osd, o_node);
704 __remove_osd(osdc, osd);
705 }
706 mutex_unlock(&osdc->request_mutex);
707}
708
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800709static void __move_osd_to_lru(struct ceph_osd_client *osdc,
710 struct ceph_osd *osd)
711{
712 dout("__move_osd_to_lru %p\n", osd);
713 BUG_ON(!list_empty(&osd->o_osd_lru));
714 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700715 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800716}
717
718static void __remove_osd_from_lru(struct ceph_osd *osd)
719{
720 dout("__remove_osd_from_lru %p\n", osd);
721 if (!list_empty(&osd->o_osd_lru))
722 list_del_init(&osd->o_osd_lru);
723}
724
Sage Weilaca420b2011-08-31 14:45:53 -0700725static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800726{
727 struct ceph_osd *osd, *nosd;
728
729 dout("__remove_old_osds %p\n", osdc);
730 mutex_lock(&osdc->request_mutex);
731 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700732 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800733 break;
734 __remove_osd(osdc, osd);
735 }
736 mutex_unlock(&osdc->request_mutex);
737}
738
Sage Weilf24e9982009-10-06 11:31:10 -0700739/*
740 * reset osd connect
741 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800742static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700743{
Sage Weil87b315a2010-03-22 14:51:18 -0700744 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700745 int ret = 0;
746
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800747 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700748 if (list_empty(&osd->o_requests) &&
749 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800750 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700751 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
752 &osd->o_con.peer_addr,
753 sizeof(osd->o_con.peer_addr)) == 0 &&
754 !ceph_con_opened(&osd->o_con)) {
755 dout(" osd addr hasn't changed and connection never opened,"
756 " letting msgr retry");
757 /* touch each r_stamp for handle_timeout()'s benfit */
758 list_for_each_entry(req, &osd->o_requests, r_osd_item)
759 req->r_stamp = jiffies;
760 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700761 } else {
762 ceph_con_close(&osd->o_con);
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700763 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
764 &osdc->osdmap->osd_addr[osd->o_osd]);
Sage Weilf24e9982009-10-06 11:31:10 -0700765 osd->o_incarnation++;
766 }
767 return ret;
768}
769
770static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
771{
772 struct rb_node **p = &osdc->osds.rb_node;
773 struct rb_node *parent = NULL;
774 struct ceph_osd *osd = NULL;
775
Sage Weilaca420b2011-08-31 14:45:53 -0700776 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700777 while (*p) {
778 parent = *p;
779 osd = rb_entry(parent, struct ceph_osd, o_node);
780 if (new->o_osd < osd->o_osd)
781 p = &(*p)->rb_left;
782 else if (new->o_osd > osd->o_osd)
783 p = &(*p)->rb_right;
784 else
785 BUG();
786 }
787
788 rb_link_node(&new->o_node, parent, p);
789 rb_insert_color(&new->o_node, &osdc->osds);
790}
791
792static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
793{
794 struct ceph_osd *osd;
795 struct rb_node *n = osdc->osds.rb_node;
796
797 while (n) {
798 osd = rb_entry(n, struct ceph_osd, o_node);
799 if (o < osd->o_osd)
800 n = n->rb_left;
801 else if (o > osd->o_osd)
802 n = n->rb_right;
803 else
804 return osd;
805 }
806 return NULL;
807}
808
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800809static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
810{
811 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700812 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800813}
814
815static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
816{
817 cancel_delayed_work(&osdc->timeout_work);
818}
Sage Weilf24e9982009-10-06 11:31:10 -0700819
820/*
821 * Register request, assign tid. If this is the first request, set up
822 * the timeout event.
823 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700824static void __register_request(struct ceph_osd_client *osdc,
825 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700826{
Sage Weilf24e9982009-10-06 11:31:10 -0700827 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800828 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700829 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700830 __insert_request(osdc, req);
831 ceph_osdc_get_request(req);
832 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700833 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800834 dout(" first request, scheduling timeout\n");
835 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700836 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700837}
838
839static void register_request(struct ceph_osd_client *osdc,
840 struct ceph_osd_request *req)
841{
842 mutex_lock(&osdc->request_mutex);
843 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700844 mutex_unlock(&osdc->request_mutex);
845}
846
847/*
848 * called under osdc->request_mutex
849 */
850static void __unregister_request(struct ceph_osd_client *osdc,
851 struct ceph_osd_request *req)
852{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500853 if (RB_EMPTY_NODE(&req->r_node)) {
854 dout("__unregister_request %p tid %lld not registered\n",
855 req, req->r_tid);
856 return;
857 }
858
Sage Weilf24e9982009-10-06 11:31:10 -0700859 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
860 rb_erase(&req->r_node, &osdc->requests);
861 osdc->num_requests--;
862
Sage Weil0ba64782009-10-08 16:57:16 -0700863 if (req->r_osd) {
864 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500865 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700866
867 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700868 if (list_empty(&req->r_osd->o_requests) &&
869 list_empty(&req->r_osd->o_linger_requests)) {
870 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800871 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700872 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700873 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700874 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700875 }
Sage Weilf24e9982009-10-06 11:31:10 -0700876
877 ceph_osdc_put_request(req);
878
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800879 list_del_init(&req->r_req_lru_item);
880 if (osdc->num_requests == 0) {
881 dout(" no requests, canceling timeout\n");
882 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700883 }
884}
885
886/*
887 * Cancel a previously queued request message
888 */
889static void __cancel_request(struct ceph_osd_request *req)
890{
Sage Weil6bc18872010-09-27 10:18:52 -0700891 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500892 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700893 req->r_sent = 0;
894 }
895}
896
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700897static void __register_linger_request(struct ceph_osd_client *osdc,
898 struct ceph_osd_request *req)
899{
900 dout("__register_linger_request %p\n", req);
901 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700902 if (req->r_osd)
903 list_add_tail(&req->r_linger_osd,
904 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700905}
906
907static void __unregister_linger_request(struct ceph_osd_client *osdc,
908 struct ceph_osd_request *req)
909{
910 dout("__unregister_linger_request %p\n", req);
911 if (req->r_osd) {
912 list_del_init(&req->r_linger_item);
913 list_del_init(&req->r_linger_osd);
914
915 if (list_empty(&req->r_osd->o_requests) &&
916 list_empty(&req->r_osd->o_linger_requests)) {
917 dout("moving osd to %p lru\n", req->r_osd);
918 __move_osd_to_lru(osdc, req->r_osd);
919 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700920 if (list_empty(&req->r_osd_item))
921 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700922 }
923}
924
925void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
927{
928 mutex_lock(&osdc->request_mutex);
929 if (req->r_linger) {
930 __unregister_linger_request(osdc, req);
931 ceph_osdc_put_request(req);
932 }
933 mutex_unlock(&osdc->request_mutex);
934}
935EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
936
937void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
938 struct ceph_osd_request *req)
939{
940 if (!req->r_linger) {
941 dout("set_request_linger %p\n", req);
942 req->r_linger = 1;
943 /*
944 * caller is now responsible for calling
945 * unregister_linger_request
946 */
947 ceph_osdc_get_request(req);
948 }
949}
950EXPORT_SYMBOL(ceph_osdc_set_request_linger);
951
Sage Weilf24e9982009-10-06 11:31:10 -0700952/*
953 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
954 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300955 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800956 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700957 *
958 * Return 0 if unchanged, 1 if changed, or negative on error.
959 *
960 * Caller should hold map_sem for read and request_mutex.
961 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800962static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700963 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700964{
965 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800966 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700967 int acting[CEPH_PG_MAX_SIZE];
968 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700969 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700970
Sage Weil6f6c7002011-01-17 20:34:08 -0800971 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700972 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
973 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800974 if (err) {
975 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700976 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800977 }
Sage Weil51042122009-11-04 11:39:12 -0800978 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800979 req->r_pgid = pgid;
980
Sage Weild85b7052010-05-10 10:24:48 -0700981 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
982 if (err > 0) {
983 o = acting[0];
984 num = err;
985 }
Sage Weilf24e9982009-10-06 11:31:10 -0700986
Sage Weil38d64532011-10-14 13:33:55 -0700987 if ((!force_resend &&
988 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700989 req->r_sent >= req->r_osd->o_incarnation &&
990 req->r_num_pg_osds == num &&
991 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700992 (req->r_osd == NULL && o == -1))
993 return 0; /* no change */
994
Sage Weil6f6c7002011-01-17 20:34:08 -0800995 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800996 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700997 req->r_osd ? req->r_osd->o_osd : -1);
998
Sage Weild85b7052010-05-10 10:24:48 -0700999 /* record full pg acting set */
1000 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
1001 req->r_num_pg_osds = num;
1002
Sage Weilf24e9982009-10-06 11:31:10 -07001003 if (req->r_osd) {
1004 __cancel_request(req);
1005 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001006 req->r_osd = NULL;
1007 }
1008
1009 req->r_osd = __lookup_osd(osdc, o);
1010 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001011 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001012 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001013 if (!req->r_osd) {
1014 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001015 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001016 }
Sage Weilf24e9982009-10-06 11:31:10 -07001017
Sage Weil6f6c7002011-01-17 20:34:08 -08001018 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 __insert_osd(osdc, req->r_osd);
1020
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001021 ceph_con_open(&req->r_osd->o_con,
1022 CEPH_ENTITY_TYPE_OSD, o,
1023 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001024 }
1025
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001026 if (req->r_osd) {
1027 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001028 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001029 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1030 } else {
1031 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001032 }
Sage Weild85b7052010-05-10 10:24:48 -07001033 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001034
1035out:
Sage Weilf24e9982009-10-06 11:31:10 -07001036 return err;
1037}
1038
1039/*
1040 * caller should hold map_sem (for read) and request_mutex
1041 */
Sage Weil56e925b2012-01-03 12:34:34 -08001042static void __send_request(struct ceph_osd_client *osdc,
1043 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001044{
1045 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001046
1047 dout("send_request %p tid %llu to osd%d flags %d\n",
1048 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1049
1050 reqhead = req->r_request->front.iov_base;
1051 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1052 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1053 reqhead->reassert_version = req->r_reassert_version;
1054
Sage Weil3dd72fc2010-03-22 14:42:30 -07001055 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001056 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001057
1058 ceph_msg_get(req->r_request); /* send consumes a ref */
1059 ceph_con_send(&req->r_osd->o_con, req->r_request);
1060 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001061}
1062
1063/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001064 * Send any requests in the queue (req_unsent).
1065 */
1066static void send_queued(struct ceph_osd_client *osdc)
1067{
1068 struct ceph_osd_request *req, *tmp;
1069
1070 dout("send_queued\n");
1071 mutex_lock(&osdc->request_mutex);
1072 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1073 __send_request(osdc, req);
1074 }
1075 mutex_unlock(&osdc->request_mutex);
1076}
1077
1078/*
Sage Weilf24e9982009-10-06 11:31:10 -07001079 * Timeout callback, called every N seconds when 1 or more osd
1080 * requests has been active for more than N seconds. When this
1081 * happens, we ping all OSDs with requests who have timed out to
1082 * ensure any communications channel reset is detected. Reset the
1083 * request timeouts another N seconds in the future as we go.
1084 * Reschedule the timeout event another N seconds in future (unless
1085 * there are no open requests).
1086 */
1087static void handle_timeout(struct work_struct *work)
1088{
1089 struct ceph_osd_client *osdc =
1090 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001091 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001092 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001093 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001094 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001095 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001096 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001097 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001098 dout("timeout\n");
1099 down_read(&osdc->map_sem);
1100
1101 ceph_monc_request_next_osdmap(&osdc->client->monc);
1102
1103 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001104
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001105 /*
1106 * reset osds that appear to be _really_ unresponsive. this
1107 * is a failsafe measure.. we really shouldn't be getting to
1108 * this point if the system is working properly. the monitors
1109 * should mark the osd as failed and we should find out about
1110 * it from an updated osd map.
1111 */
Sage Weilf26e6812010-04-21 11:09:38 -07001112 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001113 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1114 r_req_lru_item);
1115
Sage Weil4cf9d542011-07-26 11:27:24 -07001116 /* hasn't been long enough since we sent it? */
Sage Weil3dd72fc2010-03-22 14:42:30 -07001117 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001118 break;
1119
Sage Weil4cf9d542011-07-26 11:27:24 -07001120 /* hasn't been long enough since it was acked? */
1121 if (req->r_request->ack_stamp == 0 ||
1122 time_before(jiffies, req->r_request->ack_stamp + timeout))
1123 break;
1124
Sage Weil3dd72fc2010-03-22 14:42:30 -07001125 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001126 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001127 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001128
1129 osd = req->r_osd;
1130 BUG_ON(!osd);
1131 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1132 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001133 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001134 }
1135
1136 /*
1137 * ping osds that are a bit slow. this ensures that if there
1138 * is a break in the TCP connection we will notice, and reopen
1139 * a connection with that osd (from the fault callback).
1140 */
1141 INIT_LIST_HEAD(&slow_osds);
1142 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001143 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001144 break;
1145
1146 osd = req->r_osd;
1147 BUG_ON(!osd);
1148 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001149 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001150 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1151 }
1152 while (!list_empty(&slow_osds)) {
1153 osd = list_entry(slow_osds.next, struct ceph_osd,
1154 o_keepalive_item);
1155 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001156 ceph_con_keepalive(&osd->o_con);
1157 }
1158
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001159 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001160 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001161 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001162 up_read(&osdc->map_sem);
1163}
1164
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001165static void handle_osds_timeout(struct work_struct *work)
1166{
1167 struct ceph_osd_client *osdc =
1168 container_of(work, struct ceph_osd_client,
1169 osds_timeout_work.work);
1170 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001171 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001172
1173 dout("osds timeout\n");
1174 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001175 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001176 up_read(&osdc->map_sem);
1177
1178 schedule_delayed_work(&osdc->osds_timeout_work,
1179 round_jiffies_relative(delay));
1180}
1181
Sage Weil25845472011-06-03 09:37:09 -07001182static void complete_request(struct ceph_osd_request *req)
1183{
1184 if (req->r_safe_callback)
1185 req->r_safe_callback(req, NULL);
1186 complete_all(&req->r_safe_completion); /* fsync waiter */
1187}
1188
Sage Weilf24e9982009-10-06 11:31:10 -07001189/*
1190 * handle osd op reply. either call the callback if it is specified,
1191 * or do the completion to wake up the waiting thread.
1192 */
Sage Weil350b1c32009-12-22 10:45:45 -08001193static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1194 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001195{
1196 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1197 struct ceph_osd_request *req;
1198 u64 tid;
1199 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001200 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001201
Sage Weil6df058c2009-12-22 11:24:33 -08001202 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001203 if (msg->front.iov_len < sizeof(*rhead))
1204 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001205 numops = le32_to_cpu(rhead->num_ops);
1206 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001207 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001208 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1209 numops * sizeof(struct ceph_osd_op))
1210 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001211 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001212 /* lookup */
1213 mutex_lock(&osdc->request_mutex);
1214 req = __lookup_request(osdc, tid);
1215 if (req == NULL) {
1216 dout("handle_reply tid %llu dne\n", tid);
1217 mutex_unlock(&osdc->request_mutex);
1218 return;
1219 }
1220 ceph_osdc_get_request(req);
1221 flags = le32_to_cpu(rhead->flags);
1222
Sage Weil350b1c32009-12-22 10:45:45 -08001223 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001224 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001225 * avoid a (safe but slower) revoke later.
1226 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001227 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001228 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001229 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001230 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001231 }
1232
Sage Weilf24e9982009-10-06 11:31:10 -07001233 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001234 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001235
1236 req->r_result = le32_to_cpu(rhead->result);
1237 bytes = le32_to_cpu(msg->hdr.data_len);
1238 dout("handle_reply result %d bytes %d\n", req->r_result,
1239 bytes);
1240 if (req->r_result == 0)
1241 req->r_result = bytes;
1242
1243 /* in case this is a write and we need to replay, */
1244 req->r_reassert_version = rhead->reassert_version;
1245
1246 req->r_got_reply = 1;
1247 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1248 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001249 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001250 goto done;
1251 }
1252
1253 dout("handle_reply tid %llu flags %d\n", tid, flags);
1254
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001255 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1256 __register_linger_request(osdc, req);
1257
Sage Weilf24e9982009-10-06 11:31:10 -07001258 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001259 if (result < 0 ||
1260 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001261 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1262 __unregister_request(osdc, req);
1263
1264 mutex_unlock(&osdc->request_mutex);
1265
1266 if (req->r_callback)
1267 req->r_callback(req, msg);
1268 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001269 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001270
Sage Weil25845472011-06-03 09:37:09 -07001271 if (flags & CEPH_OSD_FLAG_ONDISK)
1272 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001273
1274done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001275 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001276 ceph_osdc_put_request(req);
1277 return;
1278
1279bad:
1280 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1281 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1282 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001283 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001284}
1285
Sage Weil6f6c7002011-01-17 20:34:08 -08001286static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001287{
Sage Weilf24e9982009-10-06 11:31:10 -07001288 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001289
Sage Weil6f6c7002011-01-17 20:34:08 -08001290 for (p = rb_first(&osdc->osds); p; p = n) {
1291 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001292
Sage Weil6f6c7002011-01-17 20:34:08 -08001293 n = rb_next(p);
1294 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1295 memcmp(&osd->o_con.peer_addr,
1296 ceph_osd_addr(osdc->osdmap,
1297 osd->o_osd),
1298 sizeof(struct ceph_entity_addr)) != 0)
1299 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001300 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001301}
1302
1303/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001304 * Requeue requests whose mapping to an OSD has changed. If requests map to
1305 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001306 *
1307 * Caller should hold map_sem for read and request_mutex.
1308 */
Sage Weil38d64532011-10-14 13:33:55 -07001309static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001310{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001311 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001312 struct rb_node *p;
1313 int needmap = 0;
1314 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001315
Sage Weil38d64532011-10-14 13:33:55 -07001316 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001317 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001318 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001319 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001320 p = rb_next(p);
Sage Weil38d64532011-10-14 13:33:55 -07001321 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001322 if (err < 0)
1323 continue; /* error */
1324 if (req->r_osd == NULL) {
1325 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1326 needmap++; /* request a newer map */
1327 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001328 if (!req->r_linger) {
1329 dout("%p tid %llu requeued on osd%d\n", req,
1330 req->r_tid,
1331 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001332 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001333 }
1334 }
1335 if (req->r_linger && list_empty(&req->r_linger_item)) {
1336 /*
1337 * register as a linger so that we will
1338 * re-submit below and get a new tid
1339 */
1340 dout("%p tid %llu restart on osd%d\n",
1341 req, req->r_tid,
1342 req->r_osd ? req->r_osd->o_osd : -1);
1343 __register_linger_request(osdc, req);
1344 __unregister_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001345 }
1346 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001347
1348 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1349 r_linger_item) {
1350 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1351
Sage Weil38d64532011-10-14 13:33:55 -07001352 err = __map_request(osdc, req, force_resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001353 if (err == 0)
1354 continue; /* no change and no osd was specified */
1355 if (err < 0)
1356 continue; /* hrm! */
1357 if (req->r_osd == NULL) {
1358 dout("tid %llu maps to no valid osd\n", req->r_tid);
1359 needmap++; /* request a newer map */
1360 continue;
1361 }
1362
1363 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1364 req->r_osd ? req->r_osd->o_osd : -1);
1365 __unregister_linger_request(osdc, req);
1366 __register_request(osdc, req);
1367 }
Sage Weilf24e9982009-10-06 11:31:10 -07001368 mutex_unlock(&osdc->request_mutex);
1369
1370 if (needmap) {
1371 dout("%d requests for down osds, need new map\n", needmap);
1372 ceph_monc_request_next_osdmap(&osdc->client->monc);
1373 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001374}
Sage Weil6f6c7002011-01-17 20:34:08 -08001375
1376
Sage Weilf24e9982009-10-06 11:31:10 -07001377/*
1378 * Process updated osd map.
1379 *
1380 * The message contains any number of incremental and full maps, normally
1381 * indicating some sort of topology change in the cluster. Kick requests
1382 * off to different OSDs as needed.
1383 */
1384void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1385{
1386 void *p, *end, *next;
1387 u32 nr_maps, maplen;
1388 u32 epoch;
1389 struct ceph_osdmap *newmap = NULL, *oldmap;
1390 int err;
1391 struct ceph_fsid fsid;
1392
1393 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1394 p = msg->front.iov_base;
1395 end = p + msg->front.iov_len;
1396
1397 /* verify fsid */
1398 ceph_decode_need(&p, end, sizeof(fsid), bad);
1399 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001400 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1401 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001402
1403 down_write(&osdc->map_sem);
1404
1405 /* incremental maps */
1406 ceph_decode_32_safe(&p, end, nr_maps, bad);
1407 dout(" %d inc maps\n", nr_maps);
1408 while (nr_maps > 0) {
1409 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001410 epoch = ceph_decode_32(&p);
1411 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001412 ceph_decode_need(&p, end, maplen, bad);
1413 next = p + maplen;
1414 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1415 dout("applying incremental map %u len %d\n",
1416 epoch, maplen);
1417 newmap = osdmap_apply_incremental(&p, next,
1418 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001419 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001420 if (IS_ERR(newmap)) {
1421 err = PTR_ERR(newmap);
1422 goto bad;
1423 }
Sage Weil30dc6382009-12-21 14:49:37 -08001424 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001425 if (newmap != osdc->osdmap) {
1426 ceph_osdmap_destroy(osdc->osdmap);
1427 osdc->osdmap = newmap;
1428 }
Sage Weil38d64532011-10-14 13:33:55 -07001429 kick_requests(osdc, 0);
Sage Weil6f6c7002011-01-17 20:34:08 -08001430 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001431 } else {
1432 dout("ignoring incremental map %u len %d\n",
1433 epoch, maplen);
1434 }
1435 p = next;
1436 nr_maps--;
1437 }
1438 if (newmap)
1439 goto done;
1440
1441 /* full maps */
1442 ceph_decode_32_safe(&p, end, nr_maps, bad);
1443 dout(" %d full maps\n", nr_maps);
1444 while (nr_maps) {
1445 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001446 epoch = ceph_decode_32(&p);
1447 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001448 ceph_decode_need(&p, end, maplen, bad);
1449 if (nr_maps > 1) {
1450 dout("skipping non-latest full map %u len %d\n",
1451 epoch, maplen);
1452 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1453 dout("skipping full map %u len %d, "
1454 "older than our %u\n", epoch, maplen,
1455 osdc->osdmap->epoch);
1456 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001457 int skipped_map = 0;
1458
Sage Weilf24e9982009-10-06 11:31:10 -07001459 dout("taking full map %u len %d\n", epoch, maplen);
1460 newmap = osdmap_decode(&p, p+maplen);
1461 if (IS_ERR(newmap)) {
1462 err = PTR_ERR(newmap);
1463 goto bad;
1464 }
Sage Weil30dc6382009-12-21 14:49:37 -08001465 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001466 oldmap = osdc->osdmap;
1467 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001468 if (oldmap) {
1469 if (oldmap->epoch + 1 < newmap->epoch)
1470 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001471 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001472 }
1473 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001474 }
1475 p += maplen;
1476 nr_maps--;
1477 }
1478
1479done:
1480 downgrade_write(&osdc->map_sem);
1481 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001482
1483 /*
1484 * subscribe to subsequent osdmap updates if full to ensure
1485 * we find out when we are no longer full and stop returning
1486 * ENOSPC.
1487 */
1488 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1489 ceph_monc_request_next_osdmap(&osdc->client->monc);
1490
Sage Weil6f6c7002011-01-17 20:34:08 -08001491 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001492 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001493 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001494 return;
1495
1496bad:
1497 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001498 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001499 up_write(&osdc->map_sem);
1500 return;
1501}
1502
Sage Weilf24e9982009-10-06 11:31:10 -07001503/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001504 * watch/notify callback event infrastructure
1505 *
1506 * These callbacks are used both for watch and notify operations.
1507 */
1508static void __release_event(struct kref *kref)
1509{
1510 struct ceph_osd_event *event =
1511 container_of(kref, struct ceph_osd_event, kref);
1512
1513 dout("__release_event %p\n", event);
1514 kfree(event);
1515}
1516
1517static void get_event(struct ceph_osd_event *event)
1518{
1519 kref_get(&event->kref);
1520}
1521
1522void ceph_osdc_put_event(struct ceph_osd_event *event)
1523{
1524 kref_put(&event->kref, __release_event);
1525}
1526EXPORT_SYMBOL(ceph_osdc_put_event);
1527
1528static void __insert_event(struct ceph_osd_client *osdc,
1529 struct ceph_osd_event *new)
1530{
1531 struct rb_node **p = &osdc->event_tree.rb_node;
1532 struct rb_node *parent = NULL;
1533 struct ceph_osd_event *event = NULL;
1534
1535 while (*p) {
1536 parent = *p;
1537 event = rb_entry(parent, struct ceph_osd_event, node);
1538 if (new->cookie < event->cookie)
1539 p = &(*p)->rb_left;
1540 else if (new->cookie > event->cookie)
1541 p = &(*p)->rb_right;
1542 else
1543 BUG();
1544 }
1545
1546 rb_link_node(&new->node, parent, p);
1547 rb_insert_color(&new->node, &osdc->event_tree);
1548}
1549
1550static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1551 u64 cookie)
1552{
1553 struct rb_node **p = &osdc->event_tree.rb_node;
1554 struct rb_node *parent = NULL;
1555 struct ceph_osd_event *event = NULL;
1556
1557 while (*p) {
1558 parent = *p;
1559 event = rb_entry(parent, struct ceph_osd_event, node);
1560 if (cookie < event->cookie)
1561 p = &(*p)->rb_left;
1562 else if (cookie > event->cookie)
1563 p = &(*p)->rb_right;
1564 else
1565 return event;
1566 }
1567 return NULL;
1568}
1569
1570static void __remove_event(struct ceph_osd_event *event)
1571{
1572 struct ceph_osd_client *osdc = event->osdc;
1573
1574 if (!RB_EMPTY_NODE(&event->node)) {
1575 dout("__remove_event removed %p\n", event);
1576 rb_erase(&event->node, &osdc->event_tree);
1577 ceph_osdc_put_event(event);
1578 } else {
1579 dout("__remove_event didn't remove %p\n", event);
1580 }
1581}
1582
1583int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1584 void (*event_cb)(u64, u64, u8, void *),
1585 int one_shot, void *data,
1586 struct ceph_osd_event **pevent)
1587{
1588 struct ceph_osd_event *event;
1589
1590 event = kmalloc(sizeof(*event), GFP_NOIO);
1591 if (!event)
1592 return -ENOMEM;
1593
1594 dout("create_event %p\n", event);
1595 event->cb = event_cb;
1596 event->one_shot = one_shot;
1597 event->data = data;
1598 event->osdc = osdc;
1599 INIT_LIST_HEAD(&event->osd_node);
1600 kref_init(&event->kref); /* one ref for us */
1601 kref_get(&event->kref); /* one ref for the caller */
1602 init_completion(&event->completion);
1603
1604 spin_lock(&osdc->event_lock);
1605 event->cookie = ++osdc->event_count;
1606 __insert_event(osdc, event);
1607 spin_unlock(&osdc->event_lock);
1608
1609 *pevent = event;
1610 return 0;
1611}
1612EXPORT_SYMBOL(ceph_osdc_create_event);
1613
1614void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1615{
1616 struct ceph_osd_client *osdc = event->osdc;
1617
1618 dout("cancel_event %p\n", event);
1619 spin_lock(&osdc->event_lock);
1620 __remove_event(event);
1621 spin_unlock(&osdc->event_lock);
1622 ceph_osdc_put_event(event); /* caller's */
1623}
1624EXPORT_SYMBOL(ceph_osdc_cancel_event);
1625
1626
1627static void do_event_work(struct work_struct *work)
1628{
1629 struct ceph_osd_event_work *event_work =
1630 container_of(work, struct ceph_osd_event_work, work);
1631 struct ceph_osd_event *event = event_work->event;
1632 u64 ver = event_work->ver;
1633 u64 notify_id = event_work->notify_id;
1634 u8 opcode = event_work->opcode;
1635
1636 dout("do_event_work completing %p\n", event);
1637 event->cb(ver, notify_id, opcode, event->data);
1638 complete(&event->completion);
1639 dout("do_event_work completed %p\n", event);
1640 ceph_osdc_put_event(event);
1641 kfree(event_work);
1642}
1643
1644
1645/*
1646 * Process osd watch notifications
1647 */
1648void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1649{
1650 void *p, *end;
1651 u8 proto_ver;
1652 u64 cookie, ver, notify_id;
1653 u8 opcode;
1654 struct ceph_osd_event *event;
1655 struct ceph_osd_event_work *event_work;
1656
1657 p = msg->front.iov_base;
1658 end = p + msg->front.iov_len;
1659
1660 ceph_decode_8_safe(&p, end, proto_ver, bad);
1661 ceph_decode_8_safe(&p, end, opcode, bad);
1662 ceph_decode_64_safe(&p, end, cookie, bad);
1663 ceph_decode_64_safe(&p, end, ver, bad);
1664 ceph_decode_64_safe(&p, end, notify_id, bad);
1665
1666 spin_lock(&osdc->event_lock);
1667 event = __find_event(osdc, cookie);
1668 if (event) {
1669 get_event(event);
1670 if (event->one_shot)
1671 __remove_event(event);
1672 }
1673 spin_unlock(&osdc->event_lock);
1674 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1675 cookie, ver, event);
1676 if (event) {
1677 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001678 if (!event_work) {
1679 dout("ERROR: could not allocate event_work\n");
1680 goto done_err;
1681 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001682 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001683 event_work->event = event;
1684 event_work->ver = ver;
1685 event_work->notify_id = notify_id;
1686 event_work->opcode = opcode;
1687 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1688 dout("WARNING: failed to queue notify event work\n");
1689 goto done_err;
1690 }
1691 }
1692
1693 return;
1694
1695done_err:
1696 complete(&event->completion);
1697 ceph_osdc_put_event(event);
1698 return;
1699
1700bad:
1701 pr_err("osdc handle_watch_notify corrupt msg\n");
1702 return;
1703}
1704
1705int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1706{
1707 int err;
1708
1709 dout("wait_event %p\n", event);
1710 err = wait_for_completion_interruptible_timeout(&event->completion,
1711 timeout * HZ);
1712 ceph_osdc_put_event(event);
1713 if (err > 0)
1714 err = 0;
1715 dout("wait_event %p returns %d\n", event, err);
1716 return err;
1717}
1718EXPORT_SYMBOL(ceph_osdc_wait_event);
1719
1720/*
Sage Weilf24e9982009-10-06 11:31:10 -07001721 * Register request, send initial attempt.
1722 */
1723int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1724 struct ceph_osd_request *req,
1725 bool nofail)
1726{
Sage Weilc1ea8822009-10-08 16:55:47 -07001727 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001728
1729 req->r_request->pages = req->r_pages;
1730 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001731#ifdef CONFIG_BLOCK
1732 req->r_request->bio = req->r_bio;
1733#endif
1734 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001735
1736 register_request(osdc, req);
1737
1738 down_read(&osdc->map_sem);
1739 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001740 /*
1741 * a racing kick_requests() may have sent the message for us
1742 * while we dropped request_mutex above, so only send now if
1743 * the request still han't been touched yet.
1744 */
1745 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001746 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001747 if (rc < 0) {
1748 if (nofail) {
1749 dout("osdc_start_request failed map, "
1750 " will retry %lld\n", req->r_tid);
1751 rc = 0;
1752 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001753 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001754 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001755 if (req->r_osd == NULL) {
1756 dout("send_request %p no up osds in pg\n", req);
1757 ceph_monc_request_next_osdmap(&osdc->client->monc);
1758 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001759 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001760 }
Sage Weil56e925b2012-01-03 12:34:34 -08001761 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001762 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001763
1764out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001765 mutex_unlock(&osdc->request_mutex);
1766 up_read(&osdc->map_sem);
1767 return rc;
1768}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001769EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001770
1771/*
1772 * wait for a request to complete
1773 */
1774int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1775 struct ceph_osd_request *req)
1776{
1777 int rc;
1778
1779 rc = wait_for_completion_interruptible(&req->r_completion);
1780 if (rc < 0) {
1781 mutex_lock(&osdc->request_mutex);
1782 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001783 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001784 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001785 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001786 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001787 return rc;
1788 }
1789
1790 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1791 return req->r_result;
1792}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001793EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001794
1795/*
1796 * sync - wait for all in-flight requests to flush. avoid starvation.
1797 */
1798void ceph_osdc_sync(struct ceph_osd_client *osdc)
1799{
1800 struct ceph_osd_request *req;
1801 u64 last_tid, next_tid = 0;
1802
1803 mutex_lock(&osdc->request_mutex);
1804 last_tid = osdc->last_tid;
1805 while (1) {
1806 req = __lookup_request_ge(osdc, next_tid);
1807 if (!req)
1808 break;
1809 if (req->r_tid > last_tid)
1810 break;
1811
1812 next_tid = req->r_tid + 1;
1813 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1814 continue;
1815
1816 ceph_osdc_get_request(req);
1817 mutex_unlock(&osdc->request_mutex);
1818 dout("sync waiting on tid %llu (last is %llu)\n",
1819 req->r_tid, last_tid);
1820 wait_for_completion(&req->r_safe_completion);
1821 mutex_lock(&osdc->request_mutex);
1822 ceph_osdc_put_request(req);
1823 }
1824 mutex_unlock(&osdc->request_mutex);
1825 dout("sync done (thru tid %llu)\n", last_tid);
1826}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001827EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001828
1829/*
1830 * init, shutdown
1831 */
1832int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1833{
1834 int err;
1835
1836 dout("init\n");
1837 osdc->client = client;
1838 osdc->osdmap = NULL;
1839 init_rwsem(&osdc->map_sem);
1840 init_completion(&osdc->map_waiters);
1841 osdc->last_requested_map = 0;
1842 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001843 osdc->last_tid = 0;
1844 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001845 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001846 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001847 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001848 INIT_LIST_HEAD(&osdc->req_unsent);
1849 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001850 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001851 osdc->num_requests = 0;
1852 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001853 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001854 spin_lock_init(&osdc->event_lock);
1855 osdc->event_tree = RB_ROOT;
1856 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001857
1858 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001859 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001860
Sage Weil5f44f142009-11-18 14:52:18 -08001861 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001862 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1863 sizeof(struct ceph_osd_request));
1864 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001865 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001866
Sage Weild50b4092012-07-09 14:22:34 -07001867 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1868 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001869 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001870 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001871 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001872 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001873 OSD_OPREPLY_FRONT_LEN, 10, true,
1874 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001875 if (err < 0)
1876 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001877
1878 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1879 if (IS_ERR(osdc->notify_wq)) {
1880 err = PTR_ERR(osdc->notify_wq);
1881 osdc->notify_wq = NULL;
1882 goto out_msgpool;
1883 }
Sage Weilf24e9982009-10-06 11:31:10 -07001884 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001885
Sage Weilc16e7862010-03-01 13:02:00 -08001886out_msgpool:
1887 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001888out_mempool:
1889 mempool_destroy(osdc->req_mempool);
1890out:
1891 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001892}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001893EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001894
1895void ceph_osdc_stop(struct ceph_osd_client *osdc)
1896{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001897 flush_workqueue(osdc->notify_wq);
1898 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001899 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001900 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001901 if (osdc->osdmap) {
1902 ceph_osdmap_destroy(osdc->osdmap);
1903 osdc->osdmap = NULL;
1904 }
Sage Weilaca420b2011-08-31 14:45:53 -07001905 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001906 mempool_destroy(osdc->req_mempool);
1907 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001908 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001909}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001910EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001911
1912/*
1913 * Read some contiguous pages. If we cross a stripe boundary, shorten
1914 * *plen. Return number of bytes read, or error.
1915 */
1916int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1917 struct ceph_vino vino, struct ceph_file_layout *layout,
1918 u64 off, u64 *plen,
1919 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001920 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001921{
1922 struct ceph_osd_request *req;
1923 int rc = 0;
1924
1925 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1926 vino.snap, off, *plen);
1927 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1928 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1929 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001930 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001931 if (!req)
1932 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001933
1934 /* it may be a short read due to an object boundary */
1935 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001936
Sage Weilb7495fc2010-11-09 12:43:12 -08001937 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1938 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001939
1940 rc = ceph_osdc_start_request(osdc, req, false);
1941 if (!rc)
1942 rc = ceph_osdc_wait_request(osdc, req);
1943
1944 ceph_osdc_put_request(req);
1945 dout("readpages result %d\n", rc);
1946 return rc;
1947}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001948EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001949
1950/*
1951 * do a synchronous write on N pages
1952 */
1953int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1954 struct ceph_file_layout *layout,
1955 struct ceph_snap_context *snapc,
1956 u64 off, u64 len,
1957 u32 truncate_seq, u64 truncate_size,
1958 struct timespec *mtime,
1959 struct page **pages, int num_pages,
1960 int flags, int do_sync, bool nofail)
1961{
1962 struct ceph_osd_request *req;
1963 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001964 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001965
1966 BUG_ON(vino.snap != CEPH_NOSNAP);
1967 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1968 CEPH_OSD_OP_WRITE,
1969 flags | CEPH_OSD_FLAG_ONDISK |
1970 CEPH_OSD_FLAG_WRITE,
1971 snapc, do_sync,
1972 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001973 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001974 if (!req)
1975 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001976
1977 /* it may be a short write due to an object boundary */
1978 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001979 dout("writepages %llu~%llu (%d pages)\n", off, len,
1980 req->r_num_pages);
1981
1982 rc = ceph_osdc_start_request(osdc, req, nofail);
1983 if (!rc)
1984 rc = ceph_osdc_wait_request(osdc, req);
1985
1986 ceph_osdc_put_request(req);
1987 if (rc == 0)
1988 rc = len;
1989 dout("writepages result %d\n", rc);
1990 return rc;
1991}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001992EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001993
1994/*
1995 * handle incoming message
1996 */
1997static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1998{
1999 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002000 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002001 int type = le16_to_cpu(msg->hdr.type);
2002
2003 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002004 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002005 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002006
2007 switch (type) {
2008 case CEPH_MSG_OSD_MAP:
2009 ceph_osdc_handle_map(osdc, msg);
2010 break;
2011 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002012 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002013 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002014 case CEPH_MSG_WATCH_NOTIFY:
2015 handle_watch_notify(osdc, msg);
2016 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002017
2018 default:
2019 pr_err("received unknown message type %d %s\n", type,
2020 ceph_msg_type_name(type));
2021 }
Sage Weil4a32f932010-06-13 10:27:53 -07002022out:
Sage Weilf24e9982009-10-06 11:31:10 -07002023 ceph_msg_put(msg);
2024}
2025
Sage Weil5b3a4db2010-02-19 21:43:23 -08002026/*
Sage Weil21b667f2010-03-04 10:22:59 -08002027 * lookup and return message for incoming reply. set up reply message
2028 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002029 */
2030static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002031 struct ceph_msg_header *hdr,
2032 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002033{
2034 struct ceph_osd *osd = con->private;
2035 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002036 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002037 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002038 int front = le32_to_cpu(hdr->front_len);
2039 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002040 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002041
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002042 tid = le64_to_cpu(hdr->tid);
2043 mutex_lock(&osdc->request_mutex);
2044 req = __lookup_request(osdc, tid);
2045 if (!req) {
2046 *skip = 1;
2047 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002048 dout("get_reply unknown tid %llu from osd%d\n", tid,
2049 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002050 goto out;
2051 }
Sage Weilc16e7862010-03-01 13:02:00 -08002052
2053 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002054 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002055 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002056 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002057 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002058 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002059 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002060
Sage Weilc16e7862010-03-01 13:02:00 -08002061 if (front > req->r_reply->front.iov_len) {
2062 pr_warning("get_reply front %d > preallocated %d\n",
2063 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002064 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002065 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002066 goto out;
2067 ceph_msg_put(req->r_reply);
2068 req->r_reply = m;
2069 }
2070 m = ceph_msg_get(req->r_reply);
2071
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002072 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002073 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002074
2075 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002076 pr_warning("tid %lld reply has %d bytes %d pages, we"
2077 " had only %d pages ready\n", tid, data_len,
2078 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002079 *skip = 1;
2080 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002081 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002082 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002083 }
Sage Weil21b667f2010-03-04 10:22:59 -08002084 m->pages = req->r_pages;
2085 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002086 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002087#ifdef CONFIG_BLOCK
2088 m->bio = req->r_bio;
2089#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002090 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002091 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002092 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002093 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002094
2095out:
2096 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002097 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002098
2099}
2100
2101static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2102 struct ceph_msg_header *hdr,
2103 int *skip)
2104{
2105 struct ceph_osd *osd = con->private;
2106 int type = le16_to_cpu(hdr->type);
2107 int front = le32_to_cpu(hdr->front_len);
2108
Alex Elder1c20f2d2012-06-04 14:43:32 -05002109 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002110 switch (type) {
2111 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002112 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002113 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002114 case CEPH_MSG_OSD_OPREPLY:
2115 return get_reply(con, hdr, skip);
2116 default:
2117 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2118 osd->o_osd);
2119 *skip = 1;
2120 return NULL;
2121 }
Sage Weilf24e9982009-10-06 11:31:10 -07002122}
2123
2124/*
2125 * Wrappers to refcount containing ceph_osd struct
2126 */
2127static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2128{
2129 struct ceph_osd *osd = con->private;
2130 if (get_osd(osd))
2131 return con;
2132 return NULL;
2133}
2134
2135static void put_osd_con(struct ceph_connection *con)
2136{
2137 struct ceph_osd *osd = con->private;
2138 put_osd(osd);
2139}
2140
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002141/*
2142 * authentication
2143 */
Alex Eldera3530df2012-05-16 15:16:39 -05002144/*
2145 * Note: returned pointer is the address of a structure that's
2146 * managed separately. Caller must *not* attempt to free it.
2147 */
2148static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002149 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002150{
2151 struct ceph_osd *o = con->private;
2152 struct ceph_osd_client *osdc = o->o_osdc;
2153 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002154 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002155
Alex Elder74f18692012-05-16 15:16:39 -05002156 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002157 if (ac->ops && ac->ops->destroy_authorizer)
2158 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002159 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002160 }
Alex Eldera2556512012-05-16 15:16:39 -05002161 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002162 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2163 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002164 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002165 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002166 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002167 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002168
Alex Eldera3530df2012-05-16 15:16:39 -05002169 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002170}
2171
2172
2173static int verify_authorizer_reply(struct ceph_connection *con, int len)
2174{
2175 struct ceph_osd *o = con->private;
2176 struct ceph_osd_client *osdc = o->o_osdc;
2177 struct ceph_auth_client *ac = osdc->client->monc.auth;
2178
Alex Eldera2556512012-05-16 15:16:39 -05002179 /*
2180 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2181 * XXX which do we do: succeed or fail?
2182 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002183 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002184}
2185
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002186static int invalidate_authorizer(struct ceph_connection *con)
2187{
2188 struct ceph_osd *o = con->private;
2189 struct ceph_osd_client *osdc = o->o_osdc;
2190 struct ceph_auth_client *ac = osdc->client->monc.auth;
2191
Alex Eldera2556512012-05-16 15:16:39 -05002192 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002193 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2194
2195 return ceph_monc_validate_auth(&osdc->client->monc);
2196}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002197
Tobias Klauser9e327892010-05-20 10:40:19 +02002198static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002199 .get = get_osd_con,
2200 .put = put_osd_con,
2201 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002202 .get_authorizer = get_authorizer,
2203 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002204 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002205 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002206 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002207};