blob: ac7be7202faa73fd754d762b122642162b496995 [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;
Sage Weil68162822012-09-24 21:01:02 -0700467 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700468
469 ops[0].op = opcode;
470 ops[0].extent.truncate_seq = truncate_seq;
471 ops[0].extent.truncate_size = truncate_size;
472 ops[0].payload_len = 0;
473
474 if (do_sync) {
475 ops[1].op = CEPH_OSD_OP_STARTSYNC;
476 ops[1].payload_len = 0;
477 ops[2].op = 0;
478 } else
479 ops[1].op = 0;
480
481 req = ceph_osdc_alloc_request(osdc, flags,
482 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700483 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700484 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700485 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700486 return ERR_PTR(-ENOMEM);
Sage Weilf24e9982009-10-06 11:31:10 -0700487
488 /* calculate max write size */
Sage Weil68162822012-09-24 21:01:02 -0700489 r = calc_layout(osdc, vino, layout, off, plen, req, ops);
490 if (r < 0)
491 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700492 req->r_file_layout = *layout; /* keep a copy */
493
Sage Weil9bb0ce22011-06-13 16:20:18 -0700494 /* in case it differs from natural (file) alignment that
495 calc_layout filled in for us */
496 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800497 req->r_page_alignment = page_align;
498
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700499 ceph_osdc_build_request(req, off, plen, ops,
500 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700501 mtime,
502 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700503
Sage Weilf24e9982009-10-06 11:31:10 -0700504 return req;
505}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700506EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700507
508/*
509 * We keep osd requests in an rbtree, sorted by ->r_tid.
510 */
511static void __insert_request(struct ceph_osd_client *osdc,
512 struct ceph_osd_request *new)
513{
514 struct rb_node **p = &osdc->requests.rb_node;
515 struct rb_node *parent = NULL;
516 struct ceph_osd_request *req = NULL;
517
518 while (*p) {
519 parent = *p;
520 req = rb_entry(parent, struct ceph_osd_request, r_node);
521 if (new->r_tid < req->r_tid)
522 p = &(*p)->rb_left;
523 else if (new->r_tid > req->r_tid)
524 p = &(*p)->rb_right;
525 else
526 BUG();
527 }
528
529 rb_link_node(&new->r_node, parent, p);
530 rb_insert_color(&new->r_node, &osdc->requests);
531}
532
533static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
534 u64 tid)
535{
536 struct ceph_osd_request *req;
537 struct rb_node *n = osdc->requests.rb_node;
538
539 while (n) {
540 req = rb_entry(n, struct ceph_osd_request, r_node);
541 if (tid < req->r_tid)
542 n = n->rb_left;
543 else if (tid > req->r_tid)
544 n = n->rb_right;
545 else
546 return req;
547 }
548 return NULL;
549}
550
551static struct ceph_osd_request *
552__lookup_request_ge(struct ceph_osd_client *osdc,
553 u64 tid)
554{
555 struct ceph_osd_request *req;
556 struct rb_node *n = osdc->requests.rb_node;
557
558 while (n) {
559 req = rb_entry(n, struct ceph_osd_request, r_node);
560 if (tid < req->r_tid) {
561 if (!n->rb_left)
562 return req;
563 n = n->rb_left;
564 } else if (tid > req->r_tid) {
565 n = n->rb_right;
566 } else {
567 return req;
568 }
569 }
570 return NULL;
571}
572
Sage Weil6f6c7002011-01-17 20:34:08 -0800573/*
574 * Resubmit requests pending on the given osd.
575 */
576static void __kick_osd_requests(struct ceph_osd_client *osdc,
577 struct ceph_osd *osd)
578{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700579 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800580 int err;
581
582 dout("__kick_osd_requests osd%d\n", osd->o_osd);
583 err = __reset_osd(osdc, osd);
584 if (err == -EAGAIN)
585 return;
586
587 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
588 list_move(&req->r_req_lru_item, &osdc->req_unsent);
589 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
590 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700591 if (!req->r_linger)
592 req->r_flags |= CEPH_OSD_FLAG_RETRY;
593 }
594
595 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
596 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700597 /*
598 * reregister request prior to unregistering linger so
599 * that r_osd is preserved.
600 */
601 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700602 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700603 list_add(&req->r_req_lru_item, &osdc->req_unsent);
604 list_add(&req->r_osd_item, &req->r_osd->o_requests);
605 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700606 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
607 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800608 }
609}
610
Sage Weilf24e9982009-10-06 11:31:10 -0700611/*
Sage Weil81b024e2009-10-09 10:29:18 -0700612 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700613 */
614static void osd_reset(struct ceph_connection *con)
615{
616 struct ceph_osd *osd = con->private;
617 struct ceph_osd_client *osdc;
618
619 if (!osd)
620 return;
621 dout("osd_reset osd%d\n", osd->o_osd);
622 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700623 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800624 mutex_lock(&osdc->request_mutex);
625 __kick_osd_requests(osdc, osd);
626 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -0800627 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700628 up_read(&osdc->map_sem);
629}
630
631/*
632 * Track open sessions with osds.
633 */
Alex Eldere10006f2012-05-26 23:26:43 -0500634static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700635{
636 struct ceph_osd *osd;
637
638 osd = kzalloc(sizeof(*osd), GFP_NOFS);
639 if (!osd)
640 return NULL;
641
642 atomic_set(&osd->o_ref, 1);
643 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500644 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -0700645 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700646 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800647 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700648 osd->o_incarnation = 1;
649
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700650 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800651
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800652 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700653 return osd;
654}
655
656static struct ceph_osd *get_osd(struct ceph_osd *osd)
657{
658 if (atomic_inc_not_zero(&osd->o_ref)) {
659 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
660 atomic_read(&osd->o_ref));
661 return osd;
662 } else {
663 dout("get_osd %p FAIL\n", osd);
664 return NULL;
665 }
666}
667
668static void put_osd(struct ceph_osd *osd)
669{
670 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
671 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500672 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700673 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
674
Alex Eldera2556512012-05-16 15:16:39 -0500675 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500676 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700677 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700678 }
Sage Weilf24e9982009-10-06 11:31:10 -0700679}
680
681/*
682 * remove an osd from our map
683 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800684static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700685{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800686 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700687 BUG_ON(!list_empty(&osd->o_requests));
688 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800689 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700690 ceph_con_close(&osd->o_con);
691 put_osd(osd);
692}
693
Sage Weilaca420b2011-08-31 14:45:53 -0700694static void remove_all_osds(struct ceph_osd_client *osdc)
695{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500696 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700697 mutex_lock(&osdc->request_mutex);
698 while (!RB_EMPTY_ROOT(&osdc->osds)) {
699 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
700 struct ceph_osd, o_node);
701 __remove_osd(osdc, osd);
702 }
703 mutex_unlock(&osdc->request_mutex);
704}
705
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800706static void __move_osd_to_lru(struct ceph_osd_client *osdc,
707 struct ceph_osd *osd)
708{
709 dout("__move_osd_to_lru %p\n", osd);
710 BUG_ON(!list_empty(&osd->o_osd_lru));
711 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700712 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800713}
714
715static void __remove_osd_from_lru(struct ceph_osd *osd)
716{
717 dout("__remove_osd_from_lru %p\n", osd);
718 if (!list_empty(&osd->o_osd_lru))
719 list_del_init(&osd->o_osd_lru);
720}
721
Sage Weilaca420b2011-08-31 14:45:53 -0700722static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800723{
724 struct ceph_osd *osd, *nosd;
725
726 dout("__remove_old_osds %p\n", osdc);
727 mutex_lock(&osdc->request_mutex);
728 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700729 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800730 break;
731 __remove_osd(osdc, osd);
732 }
733 mutex_unlock(&osdc->request_mutex);
734}
735
Sage Weilf24e9982009-10-06 11:31:10 -0700736/*
737 * reset osd connect
738 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800739static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700740{
Sage Weil87b315a2010-03-22 14:51:18 -0700741 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700742 int ret = 0;
743
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800744 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700745 if (list_empty(&osd->o_requests) &&
746 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800747 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700748 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
749 &osd->o_con.peer_addr,
750 sizeof(osd->o_con.peer_addr)) == 0 &&
751 !ceph_con_opened(&osd->o_con)) {
752 dout(" osd addr hasn't changed and connection never opened,"
753 " letting msgr retry");
754 /* touch each r_stamp for handle_timeout()'s benfit */
755 list_for_each_entry(req, &osd->o_requests, r_osd_item)
756 req->r_stamp = jiffies;
757 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700758 } else {
759 ceph_con_close(&osd->o_con);
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700760 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
761 &osdc->osdmap->osd_addr[osd->o_osd]);
Sage Weilf24e9982009-10-06 11:31:10 -0700762 osd->o_incarnation++;
763 }
764 return ret;
765}
766
767static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
768{
769 struct rb_node **p = &osdc->osds.rb_node;
770 struct rb_node *parent = NULL;
771 struct ceph_osd *osd = NULL;
772
Sage Weilaca420b2011-08-31 14:45:53 -0700773 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700774 while (*p) {
775 parent = *p;
776 osd = rb_entry(parent, struct ceph_osd, o_node);
777 if (new->o_osd < osd->o_osd)
778 p = &(*p)->rb_left;
779 else if (new->o_osd > osd->o_osd)
780 p = &(*p)->rb_right;
781 else
782 BUG();
783 }
784
785 rb_link_node(&new->o_node, parent, p);
786 rb_insert_color(&new->o_node, &osdc->osds);
787}
788
789static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
790{
791 struct ceph_osd *osd;
792 struct rb_node *n = osdc->osds.rb_node;
793
794 while (n) {
795 osd = rb_entry(n, struct ceph_osd, o_node);
796 if (o < osd->o_osd)
797 n = n->rb_left;
798 else if (o > osd->o_osd)
799 n = n->rb_right;
800 else
801 return osd;
802 }
803 return NULL;
804}
805
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800806static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
807{
808 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700809 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800810}
811
812static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
813{
814 cancel_delayed_work(&osdc->timeout_work);
815}
Sage Weilf24e9982009-10-06 11:31:10 -0700816
817/*
818 * Register request, assign tid. If this is the first request, set up
819 * the timeout event.
820 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700821static void __register_request(struct ceph_osd_client *osdc,
822 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700823{
Sage Weilf24e9982009-10-06 11:31:10 -0700824 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800825 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700826 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700827 __insert_request(osdc, req);
828 ceph_osdc_get_request(req);
829 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700830 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800831 dout(" first request, scheduling timeout\n");
832 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700833 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700834}
835
836static void register_request(struct ceph_osd_client *osdc,
837 struct ceph_osd_request *req)
838{
839 mutex_lock(&osdc->request_mutex);
840 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700841 mutex_unlock(&osdc->request_mutex);
842}
843
844/*
845 * called under osdc->request_mutex
846 */
847static void __unregister_request(struct ceph_osd_client *osdc,
848 struct ceph_osd_request *req)
849{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500850 if (RB_EMPTY_NODE(&req->r_node)) {
851 dout("__unregister_request %p tid %lld not registered\n",
852 req, req->r_tid);
853 return;
854 }
855
Sage Weilf24e9982009-10-06 11:31:10 -0700856 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
857 rb_erase(&req->r_node, &osdc->requests);
858 osdc->num_requests--;
859
Sage Weil0ba64782009-10-08 16:57:16 -0700860 if (req->r_osd) {
861 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500862 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700863
864 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700865 if (list_empty(&req->r_osd->o_requests) &&
866 list_empty(&req->r_osd->o_linger_requests)) {
867 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800868 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700869 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700870 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700871 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700872 }
Sage Weilf24e9982009-10-06 11:31:10 -0700873
Alex Elder7d5f2482012-11-29 08:37:03 -0600874 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700875 ceph_osdc_put_request(req);
876
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800877 if (osdc->num_requests == 0) {
878 dout(" no requests, canceling timeout\n");
879 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700880 }
881}
882
883/*
884 * Cancel a previously queued request message
885 */
886static void __cancel_request(struct ceph_osd_request *req)
887{
Sage Weil6bc18872010-09-27 10:18:52 -0700888 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500889 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700890 req->r_sent = 0;
891 }
892}
893
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700894static void __register_linger_request(struct ceph_osd_client *osdc,
895 struct ceph_osd_request *req)
896{
897 dout("__register_linger_request %p\n", req);
898 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700899 if (req->r_osd)
900 list_add_tail(&req->r_linger_osd,
901 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700902}
903
904static void __unregister_linger_request(struct ceph_osd_client *osdc,
905 struct ceph_osd_request *req)
906{
907 dout("__unregister_linger_request %p\n", req);
908 if (req->r_osd) {
909 list_del_init(&req->r_linger_item);
910 list_del_init(&req->r_linger_osd);
911
912 if (list_empty(&req->r_osd->o_requests) &&
913 list_empty(&req->r_osd->o_linger_requests)) {
914 dout("moving osd to %p lru\n", req->r_osd);
915 __move_osd_to_lru(osdc, req->r_osd);
916 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700917 if (list_empty(&req->r_osd_item))
918 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700919 }
920}
921
922void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
923 struct ceph_osd_request *req)
924{
925 mutex_lock(&osdc->request_mutex);
926 if (req->r_linger) {
927 __unregister_linger_request(osdc, req);
928 ceph_osdc_put_request(req);
929 }
930 mutex_unlock(&osdc->request_mutex);
931}
932EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
933
934void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
935 struct ceph_osd_request *req)
936{
937 if (!req->r_linger) {
938 dout("set_request_linger %p\n", req);
939 req->r_linger = 1;
940 /*
941 * caller is now responsible for calling
942 * unregister_linger_request
943 */
944 ceph_osdc_get_request(req);
945 }
946}
947EXPORT_SYMBOL(ceph_osdc_set_request_linger);
948
Sage Weilf24e9982009-10-06 11:31:10 -0700949/*
950 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
951 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300952 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800953 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700954 *
955 * Return 0 if unchanged, 1 if changed, or negative on error.
956 *
957 * Caller should hold map_sem for read and request_mutex.
958 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800959static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700960 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700961{
962 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800963 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700964 int acting[CEPH_PG_MAX_SIZE];
965 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700966 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700967
Sage Weil6f6c7002011-01-17 20:34:08 -0800968 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700969 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
970 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800971 if (err) {
972 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700973 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800974 }
Sage Weil51042122009-11-04 11:39:12 -0800975 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800976 req->r_pgid = pgid;
977
Sage Weild85b7052010-05-10 10:24:48 -0700978 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
979 if (err > 0) {
980 o = acting[0];
981 num = err;
982 }
Sage Weilf24e9982009-10-06 11:31:10 -0700983
Sage Weil38d64532011-10-14 13:33:55 -0700984 if ((!force_resend &&
985 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700986 req->r_sent >= req->r_osd->o_incarnation &&
987 req->r_num_pg_osds == num &&
988 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700989 (req->r_osd == NULL && o == -1))
990 return 0; /* no change */
991
Sage Weil6f6c7002011-01-17 20:34:08 -0800992 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800993 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700994 req->r_osd ? req->r_osd->o_osd : -1);
995
Sage Weild85b7052010-05-10 10:24:48 -0700996 /* record full pg acting set */
997 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
998 req->r_num_pg_osds = num;
999
Sage Weilf24e9982009-10-06 11:31:10 -07001000 if (req->r_osd) {
1001 __cancel_request(req);
1002 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001003 req->r_osd = NULL;
1004 }
1005
1006 req->r_osd = __lookup_osd(osdc, o);
1007 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001008 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001009 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001010 if (!req->r_osd) {
1011 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001012 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001013 }
Sage Weilf24e9982009-10-06 11:31:10 -07001014
Sage Weil6f6c7002011-01-17 20:34:08 -08001015 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001016 __insert_osd(osdc, req->r_osd);
1017
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001018 ceph_con_open(&req->r_osd->o_con,
1019 CEPH_ENTITY_TYPE_OSD, o,
1020 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001021 }
1022
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001023 if (req->r_osd) {
1024 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001025 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001026 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1027 } else {
1028 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001029 }
Sage Weild85b7052010-05-10 10:24:48 -07001030 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001031
1032out:
Sage Weilf24e9982009-10-06 11:31:10 -07001033 return err;
1034}
1035
1036/*
1037 * caller should hold map_sem (for read) and request_mutex
1038 */
Sage Weil56e925b2012-01-03 12:34:34 -08001039static void __send_request(struct ceph_osd_client *osdc,
1040 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001041{
1042 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001043
1044 dout("send_request %p tid %llu to osd%d flags %d\n",
1045 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1046
1047 reqhead = req->r_request->front.iov_base;
1048 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1049 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1050 reqhead->reassert_version = req->r_reassert_version;
1051
Sage Weil3dd72fc2010-03-22 14:42:30 -07001052 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001053 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001054
1055 ceph_msg_get(req->r_request); /* send consumes a ref */
1056 ceph_con_send(&req->r_osd->o_con, req->r_request);
1057 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001058}
1059
1060/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001061 * Send any requests in the queue (req_unsent).
1062 */
1063static void send_queued(struct ceph_osd_client *osdc)
1064{
1065 struct ceph_osd_request *req, *tmp;
1066
1067 dout("send_queued\n");
1068 mutex_lock(&osdc->request_mutex);
1069 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1070 __send_request(osdc, req);
1071 }
1072 mutex_unlock(&osdc->request_mutex);
1073}
1074
1075/*
Sage Weilf24e9982009-10-06 11:31:10 -07001076 * Timeout callback, called every N seconds when 1 or more osd
1077 * requests has been active for more than N seconds. When this
1078 * happens, we ping all OSDs with requests who have timed out to
1079 * ensure any communications channel reset is detected. Reset the
1080 * request timeouts another N seconds in the future as we go.
1081 * Reschedule the timeout event another N seconds in future (unless
1082 * there are no open requests).
1083 */
1084static void handle_timeout(struct work_struct *work)
1085{
1086 struct ceph_osd_client *osdc =
1087 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001088 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001089 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001090 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001091 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001092 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001093 dout("timeout\n");
1094 down_read(&osdc->map_sem);
1095
1096 ceph_monc_request_next_osdmap(&osdc->client->monc);
1097
1098 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001099
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001100 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001101 * ping osds that are a bit slow. this ensures that if there
1102 * is a break in the TCP connection we will notice, and reopen
1103 * a connection with that osd (from the fault callback).
1104 */
1105 INIT_LIST_HEAD(&slow_osds);
1106 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001107 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001108 break;
1109
1110 osd = req->r_osd;
1111 BUG_ON(!osd);
1112 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001113 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001114 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1115 }
1116 while (!list_empty(&slow_osds)) {
1117 osd = list_entry(slow_osds.next, struct ceph_osd,
1118 o_keepalive_item);
1119 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001120 ceph_con_keepalive(&osd->o_con);
1121 }
1122
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001123 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001124 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001125 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001126 up_read(&osdc->map_sem);
1127}
1128
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001129static void handle_osds_timeout(struct work_struct *work)
1130{
1131 struct ceph_osd_client *osdc =
1132 container_of(work, struct ceph_osd_client,
1133 osds_timeout_work.work);
1134 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001135 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001136
1137 dout("osds timeout\n");
1138 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001139 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001140 up_read(&osdc->map_sem);
1141
1142 schedule_delayed_work(&osdc->osds_timeout_work,
1143 round_jiffies_relative(delay));
1144}
1145
Sage Weil25845472011-06-03 09:37:09 -07001146static void complete_request(struct ceph_osd_request *req)
1147{
1148 if (req->r_safe_callback)
1149 req->r_safe_callback(req, NULL);
1150 complete_all(&req->r_safe_completion); /* fsync waiter */
1151}
1152
Sage Weilf24e9982009-10-06 11:31:10 -07001153/*
1154 * handle osd op reply. either call the callback if it is specified,
1155 * or do the completion to wake up the waiting thread.
1156 */
Sage Weil350b1c32009-12-22 10:45:45 -08001157static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1158 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001159{
1160 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1161 struct ceph_osd_request *req;
1162 u64 tid;
1163 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001164 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001165
Sage Weil6df058c2009-12-22 11:24:33 -08001166 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001167 if (msg->front.iov_len < sizeof(*rhead))
1168 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001169 numops = le32_to_cpu(rhead->num_ops);
1170 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001171 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001172 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1173 numops * sizeof(struct ceph_osd_op))
1174 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001175 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001176 /* lookup */
1177 mutex_lock(&osdc->request_mutex);
1178 req = __lookup_request(osdc, tid);
1179 if (req == NULL) {
1180 dout("handle_reply tid %llu dne\n", tid);
1181 mutex_unlock(&osdc->request_mutex);
1182 return;
1183 }
1184 ceph_osdc_get_request(req);
1185 flags = le32_to_cpu(rhead->flags);
1186
Sage Weil350b1c32009-12-22 10:45:45 -08001187 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001188 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001189 * avoid a (safe but slower) revoke later.
1190 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001191 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001192 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001193 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001194 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001195 }
1196
Sage Weilf24e9982009-10-06 11:31:10 -07001197 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001198 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001199
1200 req->r_result = le32_to_cpu(rhead->result);
1201 bytes = le32_to_cpu(msg->hdr.data_len);
1202 dout("handle_reply result %d bytes %d\n", req->r_result,
1203 bytes);
1204 if (req->r_result == 0)
1205 req->r_result = bytes;
1206
1207 /* in case this is a write and we need to replay, */
1208 req->r_reassert_version = rhead->reassert_version;
1209
1210 req->r_got_reply = 1;
1211 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1212 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001213 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001214 goto done;
1215 }
1216
1217 dout("handle_reply tid %llu flags %d\n", tid, flags);
1218
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001219 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1220 __register_linger_request(osdc, req);
1221
Sage Weilf24e9982009-10-06 11:31:10 -07001222 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001223 if (result < 0 ||
1224 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001225 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1226 __unregister_request(osdc, req);
1227
1228 mutex_unlock(&osdc->request_mutex);
1229
1230 if (req->r_callback)
1231 req->r_callback(req, msg);
1232 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001233 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001234
Sage Weil25845472011-06-03 09:37:09 -07001235 if (flags & CEPH_OSD_FLAG_ONDISK)
1236 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001237
1238done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001239 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001240 ceph_osdc_put_request(req);
1241 return;
1242
1243bad:
1244 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1245 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1246 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001247 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001248}
1249
Sage Weil6f6c7002011-01-17 20:34:08 -08001250static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001251{
Sage Weilf24e9982009-10-06 11:31:10 -07001252 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001253
Sage Weil6f6c7002011-01-17 20:34:08 -08001254 for (p = rb_first(&osdc->osds); p; p = n) {
1255 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001256
Sage Weil6f6c7002011-01-17 20:34:08 -08001257 n = rb_next(p);
1258 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1259 memcmp(&osd->o_con.peer_addr,
1260 ceph_osd_addr(osdc->osdmap,
1261 osd->o_osd),
1262 sizeof(struct ceph_entity_addr)) != 0)
1263 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001264 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001265}
1266
1267/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001268 * Requeue requests whose mapping to an OSD has changed. If requests map to
1269 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001270 *
1271 * Caller should hold map_sem for read and request_mutex.
1272 */
Sage Weil38d64532011-10-14 13:33:55 -07001273static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001274{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001275 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001276 struct rb_node *p;
1277 int needmap = 0;
1278 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001279
Sage Weil38d64532011-10-14 13:33:55 -07001280 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001281 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001282 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001283 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001284 p = rb_next(p);
Sage Weil38d64532011-10-14 13:33:55 -07001285 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001286 if (err < 0)
1287 continue; /* error */
1288 if (req->r_osd == NULL) {
1289 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1290 needmap++; /* request a newer map */
1291 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001292 if (!req->r_linger) {
1293 dout("%p tid %llu requeued on osd%d\n", req,
1294 req->r_tid,
1295 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001296 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001297 }
1298 }
1299 if (req->r_linger && list_empty(&req->r_linger_item)) {
1300 /*
1301 * register as a linger so that we will
1302 * re-submit below and get a new tid
1303 */
1304 dout("%p tid %llu restart on osd%d\n",
1305 req, req->r_tid,
1306 req->r_osd ? req->r_osd->o_osd : -1);
1307 __register_linger_request(osdc, req);
1308 __unregister_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001309 }
1310 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001311
1312 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1313 r_linger_item) {
1314 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1315
Sage Weil38d64532011-10-14 13:33:55 -07001316 err = __map_request(osdc, req, force_resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001317 if (err == 0)
1318 continue; /* no change and no osd was specified */
1319 if (err < 0)
1320 continue; /* hrm! */
1321 if (req->r_osd == NULL) {
1322 dout("tid %llu maps to no valid osd\n", req->r_tid);
1323 needmap++; /* request a newer map */
1324 continue;
1325 }
1326
1327 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1328 req->r_osd ? req->r_osd->o_osd : -1);
1329 __unregister_linger_request(osdc, req);
1330 __register_request(osdc, req);
1331 }
Sage Weilf24e9982009-10-06 11:31:10 -07001332 mutex_unlock(&osdc->request_mutex);
1333
1334 if (needmap) {
1335 dout("%d requests for down osds, need new map\n", needmap);
1336 ceph_monc_request_next_osdmap(&osdc->client->monc);
1337 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001338}
Sage Weil6f6c7002011-01-17 20:34:08 -08001339
1340
Sage Weilf24e9982009-10-06 11:31:10 -07001341/*
1342 * Process updated osd map.
1343 *
1344 * The message contains any number of incremental and full maps, normally
1345 * indicating some sort of topology change in the cluster. Kick requests
1346 * off to different OSDs as needed.
1347 */
1348void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1349{
1350 void *p, *end, *next;
1351 u32 nr_maps, maplen;
1352 u32 epoch;
1353 struct ceph_osdmap *newmap = NULL, *oldmap;
1354 int err;
1355 struct ceph_fsid fsid;
1356
1357 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1358 p = msg->front.iov_base;
1359 end = p + msg->front.iov_len;
1360
1361 /* verify fsid */
1362 ceph_decode_need(&p, end, sizeof(fsid), bad);
1363 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001364 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1365 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001366
1367 down_write(&osdc->map_sem);
1368
1369 /* incremental maps */
1370 ceph_decode_32_safe(&p, end, nr_maps, bad);
1371 dout(" %d inc maps\n", nr_maps);
1372 while (nr_maps > 0) {
1373 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001374 epoch = ceph_decode_32(&p);
1375 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001376 ceph_decode_need(&p, end, maplen, bad);
1377 next = p + maplen;
1378 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1379 dout("applying incremental map %u len %d\n",
1380 epoch, maplen);
1381 newmap = osdmap_apply_incremental(&p, next,
1382 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001383 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001384 if (IS_ERR(newmap)) {
1385 err = PTR_ERR(newmap);
1386 goto bad;
1387 }
Sage Weil30dc6382009-12-21 14:49:37 -08001388 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001389 if (newmap != osdc->osdmap) {
1390 ceph_osdmap_destroy(osdc->osdmap);
1391 osdc->osdmap = newmap;
1392 }
Sage Weil38d64532011-10-14 13:33:55 -07001393 kick_requests(osdc, 0);
Sage Weil6f6c7002011-01-17 20:34:08 -08001394 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001395 } else {
1396 dout("ignoring incremental map %u len %d\n",
1397 epoch, maplen);
1398 }
1399 p = next;
1400 nr_maps--;
1401 }
1402 if (newmap)
1403 goto done;
1404
1405 /* full maps */
1406 ceph_decode_32_safe(&p, end, nr_maps, bad);
1407 dout(" %d full maps\n", nr_maps);
1408 while (nr_maps) {
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 if (nr_maps > 1) {
1414 dout("skipping non-latest full map %u len %d\n",
1415 epoch, maplen);
1416 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1417 dout("skipping full map %u len %d, "
1418 "older than our %u\n", epoch, maplen,
1419 osdc->osdmap->epoch);
1420 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001421 int skipped_map = 0;
1422
Sage Weilf24e9982009-10-06 11:31:10 -07001423 dout("taking full map %u len %d\n", epoch, maplen);
1424 newmap = osdmap_decode(&p, p+maplen);
1425 if (IS_ERR(newmap)) {
1426 err = PTR_ERR(newmap);
1427 goto bad;
1428 }
Sage Weil30dc6382009-12-21 14:49:37 -08001429 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001430 oldmap = osdc->osdmap;
1431 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001432 if (oldmap) {
1433 if (oldmap->epoch + 1 < newmap->epoch)
1434 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001435 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001436 }
1437 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001438 }
1439 p += maplen;
1440 nr_maps--;
1441 }
1442
1443done:
1444 downgrade_write(&osdc->map_sem);
1445 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001446
1447 /*
1448 * subscribe to subsequent osdmap updates if full to ensure
1449 * we find out when we are no longer full and stop returning
1450 * ENOSPC.
1451 */
1452 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1453 ceph_monc_request_next_osdmap(&osdc->client->monc);
1454
Sage Weil6f6c7002011-01-17 20:34:08 -08001455 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001456 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001457 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001458 return;
1459
1460bad:
1461 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001462 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001463 up_write(&osdc->map_sem);
1464 return;
1465}
1466
Sage Weilf24e9982009-10-06 11:31:10 -07001467/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001468 * watch/notify callback event infrastructure
1469 *
1470 * These callbacks are used both for watch and notify operations.
1471 */
1472static void __release_event(struct kref *kref)
1473{
1474 struct ceph_osd_event *event =
1475 container_of(kref, struct ceph_osd_event, kref);
1476
1477 dout("__release_event %p\n", event);
1478 kfree(event);
1479}
1480
1481static void get_event(struct ceph_osd_event *event)
1482{
1483 kref_get(&event->kref);
1484}
1485
1486void ceph_osdc_put_event(struct ceph_osd_event *event)
1487{
1488 kref_put(&event->kref, __release_event);
1489}
1490EXPORT_SYMBOL(ceph_osdc_put_event);
1491
1492static void __insert_event(struct ceph_osd_client *osdc,
1493 struct ceph_osd_event *new)
1494{
1495 struct rb_node **p = &osdc->event_tree.rb_node;
1496 struct rb_node *parent = NULL;
1497 struct ceph_osd_event *event = NULL;
1498
1499 while (*p) {
1500 parent = *p;
1501 event = rb_entry(parent, struct ceph_osd_event, node);
1502 if (new->cookie < event->cookie)
1503 p = &(*p)->rb_left;
1504 else if (new->cookie > event->cookie)
1505 p = &(*p)->rb_right;
1506 else
1507 BUG();
1508 }
1509
1510 rb_link_node(&new->node, parent, p);
1511 rb_insert_color(&new->node, &osdc->event_tree);
1512}
1513
1514static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1515 u64 cookie)
1516{
1517 struct rb_node **p = &osdc->event_tree.rb_node;
1518 struct rb_node *parent = NULL;
1519 struct ceph_osd_event *event = NULL;
1520
1521 while (*p) {
1522 parent = *p;
1523 event = rb_entry(parent, struct ceph_osd_event, node);
1524 if (cookie < event->cookie)
1525 p = &(*p)->rb_left;
1526 else if (cookie > event->cookie)
1527 p = &(*p)->rb_right;
1528 else
1529 return event;
1530 }
1531 return NULL;
1532}
1533
1534static void __remove_event(struct ceph_osd_event *event)
1535{
1536 struct ceph_osd_client *osdc = event->osdc;
1537
1538 if (!RB_EMPTY_NODE(&event->node)) {
1539 dout("__remove_event removed %p\n", event);
1540 rb_erase(&event->node, &osdc->event_tree);
1541 ceph_osdc_put_event(event);
1542 } else {
1543 dout("__remove_event didn't remove %p\n", event);
1544 }
1545}
1546
1547int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1548 void (*event_cb)(u64, u64, u8, void *),
1549 int one_shot, void *data,
1550 struct ceph_osd_event **pevent)
1551{
1552 struct ceph_osd_event *event;
1553
1554 event = kmalloc(sizeof(*event), GFP_NOIO);
1555 if (!event)
1556 return -ENOMEM;
1557
1558 dout("create_event %p\n", event);
1559 event->cb = event_cb;
1560 event->one_shot = one_shot;
1561 event->data = data;
1562 event->osdc = osdc;
1563 INIT_LIST_HEAD(&event->osd_node);
1564 kref_init(&event->kref); /* one ref for us */
1565 kref_get(&event->kref); /* one ref for the caller */
1566 init_completion(&event->completion);
1567
1568 spin_lock(&osdc->event_lock);
1569 event->cookie = ++osdc->event_count;
1570 __insert_event(osdc, event);
1571 spin_unlock(&osdc->event_lock);
1572
1573 *pevent = event;
1574 return 0;
1575}
1576EXPORT_SYMBOL(ceph_osdc_create_event);
1577
1578void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1579{
1580 struct ceph_osd_client *osdc = event->osdc;
1581
1582 dout("cancel_event %p\n", event);
1583 spin_lock(&osdc->event_lock);
1584 __remove_event(event);
1585 spin_unlock(&osdc->event_lock);
1586 ceph_osdc_put_event(event); /* caller's */
1587}
1588EXPORT_SYMBOL(ceph_osdc_cancel_event);
1589
1590
1591static void do_event_work(struct work_struct *work)
1592{
1593 struct ceph_osd_event_work *event_work =
1594 container_of(work, struct ceph_osd_event_work, work);
1595 struct ceph_osd_event *event = event_work->event;
1596 u64 ver = event_work->ver;
1597 u64 notify_id = event_work->notify_id;
1598 u8 opcode = event_work->opcode;
1599
1600 dout("do_event_work completing %p\n", event);
1601 event->cb(ver, notify_id, opcode, event->data);
1602 complete(&event->completion);
1603 dout("do_event_work completed %p\n", event);
1604 ceph_osdc_put_event(event);
1605 kfree(event_work);
1606}
1607
1608
1609/*
1610 * Process osd watch notifications
1611 */
1612void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1613{
1614 void *p, *end;
1615 u8 proto_ver;
1616 u64 cookie, ver, notify_id;
1617 u8 opcode;
1618 struct ceph_osd_event *event;
1619 struct ceph_osd_event_work *event_work;
1620
1621 p = msg->front.iov_base;
1622 end = p + msg->front.iov_len;
1623
1624 ceph_decode_8_safe(&p, end, proto_ver, bad);
1625 ceph_decode_8_safe(&p, end, opcode, bad);
1626 ceph_decode_64_safe(&p, end, cookie, bad);
1627 ceph_decode_64_safe(&p, end, ver, bad);
1628 ceph_decode_64_safe(&p, end, notify_id, bad);
1629
1630 spin_lock(&osdc->event_lock);
1631 event = __find_event(osdc, cookie);
1632 if (event) {
1633 get_event(event);
1634 if (event->one_shot)
1635 __remove_event(event);
1636 }
1637 spin_unlock(&osdc->event_lock);
1638 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1639 cookie, ver, event);
1640 if (event) {
1641 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001642 if (!event_work) {
1643 dout("ERROR: could not allocate event_work\n");
1644 goto done_err;
1645 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001646 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001647 event_work->event = event;
1648 event_work->ver = ver;
1649 event_work->notify_id = notify_id;
1650 event_work->opcode = opcode;
1651 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1652 dout("WARNING: failed to queue notify event work\n");
1653 goto done_err;
1654 }
1655 }
1656
1657 return;
1658
1659done_err:
1660 complete(&event->completion);
1661 ceph_osdc_put_event(event);
1662 return;
1663
1664bad:
1665 pr_err("osdc handle_watch_notify corrupt msg\n");
1666 return;
1667}
1668
1669int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1670{
1671 int err;
1672
1673 dout("wait_event %p\n", event);
1674 err = wait_for_completion_interruptible_timeout(&event->completion,
1675 timeout * HZ);
1676 ceph_osdc_put_event(event);
1677 if (err > 0)
1678 err = 0;
1679 dout("wait_event %p returns %d\n", event, err);
1680 return err;
1681}
1682EXPORT_SYMBOL(ceph_osdc_wait_event);
1683
1684/*
Sage Weilf24e9982009-10-06 11:31:10 -07001685 * Register request, send initial attempt.
1686 */
1687int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1688 struct ceph_osd_request *req,
1689 bool nofail)
1690{
Sage Weilc1ea8822009-10-08 16:55:47 -07001691 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001692
1693 req->r_request->pages = req->r_pages;
1694 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001695#ifdef CONFIG_BLOCK
1696 req->r_request->bio = req->r_bio;
1697#endif
1698 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001699
1700 register_request(osdc, req);
1701
1702 down_read(&osdc->map_sem);
1703 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001704 /*
1705 * a racing kick_requests() may have sent the message for us
1706 * while we dropped request_mutex above, so only send now if
1707 * the request still han't been touched yet.
1708 */
1709 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001710 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001711 if (rc < 0) {
1712 if (nofail) {
1713 dout("osdc_start_request failed map, "
1714 " will retry %lld\n", req->r_tid);
1715 rc = 0;
1716 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001717 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001718 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001719 if (req->r_osd == NULL) {
1720 dout("send_request %p no up osds in pg\n", req);
1721 ceph_monc_request_next_osdmap(&osdc->client->monc);
1722 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001723 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001724 }
Sage Weil56e925b2012-01-03 12:34:34 -08001725 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001726 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001727
1728out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001729 mutex_unlock(&osdc->request_mutex);
1730 up_read(&osdc->map_sem);
1731 return rc;
1732}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001733EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001734
1735/*
1736 * wait for a request to complete
1737 */
1738int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1739 struct ceph_osd_request *req)
1740{
1741 int rc;
1742
1743 rc = wait_for_completion_interruptible(&req->r_completion);
1744 if (rc < 0) {
1745 mutex_lock(&osdc->request_mutex);
1746 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001747 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001748 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001749 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001750 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001751 return rc;
1752 }
1753
1754 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1755 return req->r_result;
1756}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001757EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001758
1759/*
1760 * sync - wait for all in-flight requests to flush. avoid starvation.
1761 */
1762void ceph_osdc_sync(struct ceph_osd_client *osdc)
1763{
1764 struct ceph_osd_request *req;
1765 u64 last_tid, next_tid = 0;
1766
1767 mutex_lock(&osdc->request_mutex);
1768 last_tid = osdc->last_tid;
1769 while (1) {
1770 req = __lookup_request_ge(osdc, next_tid);
1771 if (!req)
1772 break;
1773 if (req->r_tid > last_tid)
1774 break;
1775
1776 next_tid = req->r_tid + 1;
1777 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1778 continue;
1779
1780 ceph_osdc_get_request(req);
1781 mutex_unlock(&osdc->request_mutex);
1782 dout("sync waiting on tid %llu (last is %llu)\n",
1783 req->r_tid, last_tid);
1784 wait_for_completion(&req->r_safe_completion);
1785 mutex_lock(&osdc->request_mutex);
1786 ceph_osdc_put_request(req);
1787 }
1788 mutex_unlock(&osdc->request_mutex);
1789 dout("sync done (thru tid %llu)\n", last_tid);
1790}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001791EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001792
1793/*
1794 * init, shutdown
1795 */
1796int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1797{
1798 int err;
1799
1800 dout("init\n");
1801 osdc->client = client;
1802 osdc->osdmap = NULL;
1803 init_rwsem(&osdc->map_sem);
1804 init_completion(&osdc->map_waiters);
1805 osdc->last_requested_map = 0;
1806 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001807 osdc->last_tid = 0;
1808 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001809 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001810 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001811 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001812 INIT_LIST_HEAD(&osdc->req_unsent);
1813 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001814 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001815 osdc->num_requests = 0;
1816 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001817 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001818 spin_lock_init(&osdc->event_lock);
1819 osdc->event_tree = RB_ROOT;
1820 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001821
1822 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001823 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001824
Sage Weil5f44f142009-11-18 14:52:18 -08001825 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001826 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1827 sizeof(struct ceph_osd_request));
1828 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001829 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001830
Sage Weild50b4092012-07-09 14:22:34 -07001831 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1832 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001833 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001834 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001835 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001836 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001837 OSD_OPREPLY_FRONT_LEN, 10, true,
1838 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001839 if (err < 0)
1840 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001841
1842 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1843 if (IS_ERR(osdc->notify_wq)) {
1844 err = PTR_ERR(osdc->notify_wq);
1845 osdc->notify_wq = NULL;
1846 goto out_msgpool;
1847 }
Sage Weilf24e9982009-10-06 11:31:10 -07001848 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001849
Sage Weilc16e7862010-03-01 13:02:00 -08001850out_msgpool:
1851 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001852out_mempool:
1853 mempool_destroy(osdc->req_mempool);
1854out:
1855 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001856}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001857EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001858
1859void ceph_osdc_stop(struct ceph_osd_client *osdc)
1860{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001861 flush_workqueue(osdc->notify_wq);
1862 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001863 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001864 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001865 if (osdc->osdmap) {
1866 ceph_osdmap_destroy(osdc->osdmap);
1867 osdc->osdmap = NULL;
1868 }
Sage Weilaca420b2011-08-31 14:45:53 -07001869 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001870 mempool_destroy(osdc->req_mempool);
1871 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001872 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001873}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001874EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001875
1876/*
1877 * Read some contiguous pages. If we cross a stripe boundary, shorten
1878 * *plen. Return number of bytes read, or error.
1879 */
1880int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1881 struct ceph_vino vino, struct ceph_file_layout *layout,
1882 u64 off, u64 *plen,
1883 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001884 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001885{
1886 struct ceph_osd_request *req;
1887 int rc = 0;
1888
1889 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1890 vino.snap, off, *plen);
1891 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1892 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1893 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001894 false, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001895 if (IS_ERR(req))
1896 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001897
1898 /* it may be a short read due to an object boundary */
1899 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001900
Sage Weilb7495fc2010-11-09 12:43:12 -08001901 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1902 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001903
1904 rc = ceph_osdc_start_request(osdc, req, false);
1905 if (!rc)
1906 rc = ceph_osdc_wait_request(osdc, req);
1907
1908 ceph_osdc_put_request(req);
1909 dout("readpages result %d\n", rc);
1910 return rc;
1911}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001912EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001913
1914/*
1915 * do a synchronous write on N pages
1916 */
1917int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1918 struct ceph_file_layout *layout,
1919 struct ceph_snap_context *snapc,
1920 u64 off, u64 len,
1921 u32 truncate_seq, u64 truncate_size,
1922 struct timespec *mtime,
1923 struct page **pages, int num_pages,
1924 int flags, int do_sync, bool nofail)
1925{
1926 struct ceph_osd_request *req;
1927 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001928 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001929
1930 BUG_ON(vino.snap != CEPH_NOSNAP);
1931 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1932 CEPH_OSD_OP_WRITE,
1933 flags | CEPH_OSD_FLAG_ONDISK |
1934 CEPH_OSD_FLAG_WRITE,
1935 snapc, do_sync,
1936 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001937 nofail, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001938 if (IS_ERR(req))
1939 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001940
1941 /* it may be a short write due to an object boundary */
1942 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001943 dout("writepages %llu~%llu (%d pages)\n", off, len,
1944 req->r_num_pages);
1945
1946 rc = ceph_osdc_start_request(osdc, req, nofail);
1947 if (!rc)
1948 rc = ceph_osdc_wait_request(osdc, req);
1949
1950 ceph_osdc_put_request(req);
1951 if (rc == 0)
1952 rc = len;
1953 dout("writepages result %d\n", rc);
1954 return rc;
1955}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001956EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001957
1958/*
1959 * handle incoming message
1960 */
1961static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1962{
1963 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001964 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001965 int type = le16_to_cpu(msg->hdr.type);
1966
1967 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001968 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001969 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001970
1971 switch (type) {
1972 case CEPH_MSG_OSD_MAP:
1973 ceph_osdc_handle_map(osdc, msg);
1974 break;
1975 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001976 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001977 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001978 case CEPH_MSG_WATCH_NOTIFY:
1979 handle_watch_notify(osdc, msg);
1980 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001981
1982 default:
1983 pr_err("received unknown message type %d %s\n", type,
1984 ceph_msg_type_name(type));
1985 }
Sage Weil4a32f932010-06-13 10:27:53 -07001986out:
Sage Weilf24e9982009-10-06 11:31:10 -07001987 ceph_msg_put(msg);
1988}
1989
Sage Weil5b3a4db2010-02-19 21:43:23 -08001990/*
Sage Weil21b667f2010-03-04 10:22:59 -08001991 * lookup and return message for incoming reply. set up reply message
1992 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001993 */
1994static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001995 struct ceph_msg_header *hdr,
1996 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001997{
1998 struct ceph_osd *osd = con->private;
1999 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002000 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002001 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002002 int front = le32_to_cpu(hdr->front_len);
2003 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002004 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002005
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002006 tid = le64_to_cpu(hdr->tid);
2007 mutex_lock(&osdc->request_mutex);
2008 req = __lookup_request(osdc, tid);
2009 if (!req) {
2010 *skip = 1;
2011 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002012 dout("get_reply unknown tid %llu from osd%d\n", tid,
2013 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002014 goto out;
2015 }
Sage Weilc16e7862010-03-01 13:02:00 -08002016
2017 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002018 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002019 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002020 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002021 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002022 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002023 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002024
Sage Weilc16e7862010-03-01 13:02:00 -08002025 if (front > req->r_reply->front.iov_len) {
2026 pr_warning("get_reply front %d > preallocated %d\n",
2027 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002028 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002029 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002030 goto out;
2031 ceph_msg_put(req->r_reply);
2032 req->r_reply = m;
2033 }
2034 m = ceph_msg_get(req->r_reply);
2035
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002036 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002037 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002038
2039 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002040 pr_warning("tid %lld reply has %d bytes %d pages, we"
2041 " had only %d pages ready\n", tid, data_len,
2042 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002043 *skip = 1;
2044 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002045 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002046 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002047 }
Sage Weil21b667f2010-03-04 10:22:59 -08002048 m->pages = req->r_pages;
2049 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002050 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002051#ifdef CONFIG_BLOCK
2052 m->bio = req->r_bio;
2053#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002054 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002055 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002056 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002057 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002058
2059out:
2060 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002061 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002062
2063}
2064
2065static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2066 struct ceph_msg_header *hdr,
2067 int *skip)
2068{
2069 struct ceph_osd *osd = con->private;
2070 int type = le16_to_cpu(hdr->type);
2071 int front = le32_to_cpu(hdr->front_len);
2072
Alex Elder1c20f2d2012-06-04 14:43:32 -05002073 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002074 switch (type) {
2075 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002076 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002077 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002078 case CEPH_MSG_OSD_OPREPLY:
2079 return get_reply(con, hdr, skip);
2080 default:
2081 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2082 osd->o_osd);
2083 *skip = 1;
2084 return NULL;
2085 }
Sage Weilf24e9982009-10-06 11:31:10 -07002086}
2087
2088/*
2089 * Wrappers to refcount containing ceph_osd struct
2090 */
2091static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2092{
2093 struct ceph_osd *osd = con->private;
2094 if (get_osd(osd))
2095 return con;
2096 return NULL;
2097}
2098
2099static void put_osd_con(struct ceph_connection *con)
2100{
2101 struct ceph_osd *osd = con->private;
2102 put_osd(osd);
2103}
2104
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002105/*
2106 * authentication
2107 */
Alex Eldera3530df2012-05-16 15:16:39 -05002108/*
2109 * Note: returned pointer is the address of a structure that's
2110 * managed separately. Caller must *not* attempt to free it.
2111 */
2112static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002113 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002114{
2115 struct ceph_osd *o = con->private;
2116 struct ceph_osd_client *osdc = o->o_osdc;
2117 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002118 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002119
Alex Elder74f18692012-05-16 15:16:39 -05002120 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002121 if (ac->ops && ac->ops->destroy_authorizer)
2122 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002123 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002124 }
Alex Eldera2556512012-05-16 15:16:39 -05002125 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002126 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2127 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002128 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002129 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002130 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002131 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002132
Alex Eldera3530df2012-05-16 15:16:39 -05002133 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002134}
2135
2136
2137static int verify_authorizer_reply(struct ceph_connection *con, int len)
2138{
2139 struct ceph_osd *o = con->private;
2140 struct ceph_osd_client *osdc = o->o_osdc;
2141 struct ceph_auth_client *ac = osdc->client->monc.auth;
2142
Alex Eldera2556512012-05-16 15:16:39 -05002143 /*
2144 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2145 * XXX which do we do: succeed or fail?
2146 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002147 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002148}
2149
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002150static int invalidate_authorizer(struct ceph_connection *con)
2151{
2152 struct ceph_osd *o = con->private;
2153 struct ceph_osd_client *osdc = o->o_osdc;
2154 struct ceph_auth_client *ac = osdc->client->monc.auth;
2155
Alex Eldera2556512012-05-16 15:16:39 -05002156 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002157 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2158
2159 return ceph_monc_validate_auth(&osdc->client->monc);
2160}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002161
Tobias Klauser9e327892010-05-20 10:40:19 +02002162static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002163 .get = get_osd_con,
2164 .put = put_osd_con,
2165 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002166 .get_authorizer = get_authorizer,
2167 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002168 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002169 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002170 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002171};