blob: 07920cac31a6b18545a057ccb667ad318d6bdb57 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07004#include <linux/err.h>
5#include <linux/highmem.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/slab.h>
9#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070010#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
Sage Weilf24e9982009-10-06 11:31:10 -070013
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include <linux/ceph/libceph.h>
15#include <linux/ceph/osd_client.h>
16#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/auth.h>
19#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070020
Sage Weilc16e7862010-03-01 13:02:00 -080021#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Tobias Klauser9e327892010-05-20 10:40:19 +020024static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070025
Sage Weil6f6c7002011-01-17 20:34:08 -080026static void send_queued(struct ceph_osd_client *osdc);
27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070028static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080032static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070034
Yehuda Sadeh68b44762010-04-06 15:01:27 -070035static int op_needs_trail(int op)
36{
37 switch (op) {
38 case CEPH_OSD_OP_GETXATTR:
39 case CEPH_OSD_OP_SETXATTR:
40 case CEPH_OSD_OP_CMPXATTR:
41 case CEPH_OSD_OP_CALL:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070042 case CEPH_OSD_OP_NOTIFY:
Yehuda Sadeh68b44762010-04-06 15:01:27 -070043 return 1;
44 default:
45 return 0;
46 }
47}
48
49static int op_has_extent(int op)
50{
51 return (op == CEPH_OSD_OP_READ ||
52 op == CEPH_OSD_OP_WRITE);
53}
54
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070055void ceph_calc_raw_layout(struct ceph_osd_client *osdc,
56 struct ceph_file_layout *layout,
57 u64 snapid,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070058 u64 off, u64 *plen, u64 *bno,
59 struct ceph_osd_request *req,
60 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070061{
62 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Yehuda Sadeh68b44762010-04-06 15:01:27 -070063 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070064 u64 objoff, objlen; /* extent in object */
65
66 reqhead->snapid = cpu_to_le64(snapid);
67
68 /* object extent? */
Yehuda Sadeh68b44762010-04-06 15:01:27 -070069 ceph_calc_file_object_mapping(layout, off, plen, bno,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070070 &objoff, &objlen);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070071 if (*plen < orig_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070072 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070073 orig_len - *plen, off, *plen);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070074
Yehuda Sadeh68b44762010-04-06 15:01:27 -070075 if (op_has_extent(op->op)) {
76 op->extent.offset = objoff;
77 op->extent.length = objlen;
78 }
79 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080080 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070081 if (op->op == CEPH_OSD_OP_WRITE)
82 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070083
84 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
85 *bno, objoff, objlen, req->r_num_pages);
86
87}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070088EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070089
Sage Weilf24e9982009-10-06 11:31:10 -070090/*
91 * Implement client access to distributed object storage cluster.
92 *
93 * All data objects are stored within a cluster/cloud of OSDs, or
94 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
95 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
96 * remote daemons serving up and coordinating consistent and safe
97 * access to storage.
98 *
99 * Cluster membership and the mapping of data objects onto storage devices
100 * are described by the osd map.
101 *
102 * We keep track of pending OSD requests (read, write), resubmit
103 * requests to different OSDs when the cluster topology/data layout
104 * change, or retry the affected requests when the communications
105 * channel with an OSD is reset.
106 */
107
108/*
109 * calculate the mapping of a file extent onto an object, and fill out the
110 * request accordingly. shorten extent as necessary if it crosses an
111 * object boundary.
112 *
113 * fill osd op in request message.
114 */
115static void calc_layout(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700116 struct ceph_vino vino,
117 struct ceph_file_layout *layout,
Sage Weilf24e9982009-10-06 11:31:10 -0700118 u64 off, u64 *plen,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700119 struct ceph_osd_request *req,
120 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700121{
Sage Weilf24e9982009-10-06 11:31:10 -0700122 u64 bno;
123
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700124 ceph_calc_raw_layout(osdc, layout, vino.snap, off,
125 plen, &bno, req, op);
Sage Weilf24e9982009-10-06 11:31:10 -0700126
Sage Weil2dab0362011-05-12 14:29:51 -0700127 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
Sage Weilf24e9982009-10-06 11:31:10 -0700128 req->r_oid_len = strlen(req->r_oid);
Sage Weilf24e9982009-10-06 11:31:10 -0700129}
130
Sage Weilf24e9982009-10-06 11:31:10 -0700131/*
132 * requests
133 */
Sage Weil415e49a2009-12-07 13:37:03 -0800134void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700135{
Sage Weil415e49a2009-12-07 13:37:03 -0800136 struct ceph_osd_request *req = container_of(kref,
137 struct ceph_osd_request,
138 r_kref);
139
140 if (req->r_request)
141 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800142 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -0500143 dout("%s revoking pages %p from con %p\n", __func__,
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800144 req->r_pages, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500145 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700146 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800147 }
Alex Elderab8cb342012-06-04 14:43:32 -0500148 if (req->r_reply)
149 ceph_msg_put(req->r_reply);
Sage Weil415e49a2009-12-07 13:37:03 -0800150 if (req->r_own_pages)
151 ceph_release_page_vector(req->r_pages,
152 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700153#ifdef CONFIG_BLOCK
154 if (req->r_bio)
155 bio_put(req->r_bio);
156#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800157 ceph_put_snap_context(req->r_snapc);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700158 if (req->r_trail) {
159 ceph_pagelist_release(req->r_trail);
160 kfree(req->r_trail);
161 }
Sage Weil415e49a2009-12-07 13:37:03 -0800162 if (req->r_mempool)
163 mempool_free(req, req->r_osdc->req_mempool);
164 else
165 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700166}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700167EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700168
169static int get_num_ops(struct ceph_osd_req_op *ops, int *needs_trail)
170{
171 int i = 0;
172
173 if (needs_trail)
174 *needs_trail = 0;
175 while (ops[i].op) {
176 if (needs_trail && op_needs_trail(ops[i].op))
177 *needs_trail = 1;
178 i++;
179 }
180
181 return i;
182}
183
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700184struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
185 int flags,
186 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700187 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700188 bool use_mempool,
189 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700190 struct page **pages,
191 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700192{
193 struct ceph_osd_request *req;
194 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700195 int needs_trail;
196 int num_op = get_num_ops(ops, &needs_trail);
197 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700198
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700199 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700200
201 if (use_mempool) {
202 req = mempool_alloc(osdc->req_mempool, gfp_flags);
203 memset(req, 0, sizeof(*req));
204 } else {
205 req = kzalloc(sizeof(*req), gfp_flags);
206 }
207 if (req == NULL)
208 return NULL;
209
210 req->r_osdc = osdc;
211 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700212
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700213 kref_init(&req->r_kref);
214 init_completion(&req->r_completion);
215 init_completion(&req->r_safe_completion);
Sage Weilcd430452012-07-09 14:31:41 -0700216 rb_init_node(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700217 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700218 INIT_LIST_HEAD(&req->r_linger_item);
219 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700220 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700221 INIT_LIST_HEAD(&req->r_osd_item);
222
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700223 req->r_flags = flags;
224
225 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
226
227 /* create reply message */
228 if (use_mempool)
229 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
230 else
231 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700232 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700233 if (!msg) {
234 ceph_osdc_put_request(req);
235 return NULL;
236 }
237 req->r_reply = msg;
238
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700239 /* allocate space for the trailing data */
240 if (needs_trail) {
241 req->r_trail = kmalloc(sizeof(struct ceph_pagelist), gfp_flags);
242 if (!req->r_trail) {
243 ceph_osdc_put_request(req);
244 return NULL;
245 }
246 ceph_pagelist_init(req->r_trail);
247 }
Sage Weild50b4092012-07-09 14:22:34 -0700248
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700249 /* create request message; allow space for oid */
Stratos Psomadakis224736d2011-11-10 15:45:37 +0200250 msg_size += MAX_OBJ_NAME_SIZE;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700251 if (snapc)
252 msg_size += sizeof(u64) * snapc->num_snaps;
253 if (use_mempool)
254 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
255 else
Sage Weilb61c2762011-08-09 15:03:46 -0700256 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700257 if (!msg) {
258 ceph_osdc_put_request(req);
259 return NULL;
260 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700261
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700262 memset(msg->front.iov_base, 0, msg->front.iov_len);
263
264 req->r_request = msg;
265 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700266#ifdef CONFIG_BLOCK
267 if (bio) {
268 req->r_bio = bio;
269 bio_get(req->r_bio);
270 }
271#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700272
273 return req;
274}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700275EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700276
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700277static void osd_req_encode_op(struct ceph_osd_request *req,
278 struct ceph_osd_op *dst,
279 struct ceph_osd_req_op *src)
280{
281 dst->op = cpu_to_le16(src->op);
282
Alex Elder065a68f2012-04-20 15:49:43 -0500283 switch (src->op) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700284 case CEPH_OSD_OP_READ:
285 case CEPH_OSD_OP_WRITE:
286 dst->extent.offset =
287 cpu_to_le64(src->extent.offset);
288 dst->extent.length =
289 cpu_to_le64(src->extent.length);
290 dst->extent.truncate_size =
291 cpu_to_le64(src->extent.truncate_size);
292 dst->extent.truncate_seq =
293 cpu_to_le32(src->extent.truncate_seq);
294 break;
295
296 case CEPH_OSD_OP_GETXATTR:
297 case CEPH_OSD_OP_SETXATTR:
298 case CEPH_OSD_OP_CMPXATTR:
299 BUG_ON(!req->r_trail);
300
301 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
302 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
303 dst->xattr.cmp_op = src->xattr.cmp_op;
304 dst->xattr.cmp_mode = src->xattr.cmp_mode;
305 ceph_pagelist_append(req->r_trail, src->xattr.name,
306 src->xattr.name_len);
307 ceph_pagelist_append(req->r_trail, src->xattr.val,
308 src->xattr.value_len);
309 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700310 case CEPH_OSD_OP_CALL:
311 BUG_ON(!req->r_trail);
312
313 dst->cls.class_len = src->cls.class_len;
314 dst->cls.method_len = src->cls.method_len;
315 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
316
317 ceph_pagelist_append(req->r_trail, src->cls.class_name,
318 src->cls.class_len);
319 ceph_pagelist_append(req->r_trail, src->cls.method_name,
320 src->cls.method_len);
321 ceph_pagelist_append(req->r_trail, src->cls.indata,
322 src->cls.indata_len);
323 break;
324 case CEPH_OSD_OP_ROLLBACK:
325 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
326 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700327 case CEPH_OSD_OP_STARTSYNC:
328 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700329 case CEPH_OSD_OP_NOTIFY:
330 {
331 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
332 __le32 timeout = cpu_to_le32(src->watch.timeout);
333
334 BUG_ON(!req->r_trail);
335
336 ceph_pagelist_append(req->r_trail,
337 &prot_ver, sizeof(prot_ver));
338 ceph_pagelist_append(req->r_trail,
339 &timeout, sizeof(timeout));
340 }
341 case CEPH_OSD_OP_NOTIFY_ACK:
342 case CEPH_OSD_OP_WATCH:
343 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
344 dst->watch.ver = cpu_to_le64(src->watch.ver);
345 dst->watch.flag = src->watch.flag;
346 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700347 default:
348 pr_err("unrecognized osd opcode %d\n", dst->op);
349 WARN_ON(1);
350 break;
351 }
352 dst->payload_len = cpu_to_le32(src->payload_len);
353}
354
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355/*
356 * build new request AND message
357 *
358 */
359void ceph_osdc_build_request(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700360 u64 off, u64 *plen,
361 struct ceph_osd_req_op *src_ops,
362 struct ceph_snap_context *snapc,
363 struct timespec *mtime,
364 const char *oid,
365 int oid_len)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700366{
367 struct ceph_msg *msg = req->r_request;
368 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700369 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700370 struct ceph_osd_op *op;
371 void *p;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700372 int num_op = get_num_ops(src_ops, NULL);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700373 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700375 u64 data_len = 0;
376 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700377
378 head = msg->front.iov_base;
379 op = (void *)(head + 1);
380 p = (void *)(op + num_op);
381
382 req->r_snapc = ceph_get_snap_context(snapc);
383
384 head->client_inc = cpu_to_le32(1); /* always, for now. */
385 head->flags = cpu_to_le32(flags);
386 if (flags & CEPH_OSD_FLAG_WRITE)
387 ceph_encode_timespec(&head->mtime, mtime);
388 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700389
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700390
391 /* fill in oid */
392 head->object_len = cpu_to_le32(oid_len);
393 memcpy(p, oid, oid_len);
394 p += oid_len;
395
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700396 src_op = src_ops;
397 while (src_op->op) {
398 osd_req_encode_op(req, op, src_op);
399 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700400 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700402
403 if (req->r_trail)
404 data_len += req->r_trail->length;
405
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700406 if (snapc) {
407 head->snap_seq = cpu_to_le64(snapc->seq);
408 head->num_snaps = cpu_to_le32(snapc->num_snaps);
409 for (i = 0; i < snapc->num_snaps; i++) {
410 put_unaligned_le64(snapc->snaps[i], p);
411 p += sizeof(u64);
412 }
413 }
414
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700415 if (flags & CEPH_OSD_FLAG_WRITE) {
416 req->r_request->hdr.data_off = cpu_to_le16(off);
417 req->r_request->hdr.data_len = cpu_to_le32(*plen + data_len);
418 } else if (data_len) {
419 req->r_request->hdr.data_off = 0;
420 req->r_request->hdr.data_len = cpu_to_le32(data_len);
421 }
422
Sage Weilc5c6b192010-11-09 12:40:00 -0800423 req->r_request->page_alignment = req->r_page_alignment;
424
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700425 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
426 msg_size = p - msg->front.iov_base;
427 msg->front.iov_len = msg_size;
428 msg->hdr.front_len = cpu_to_le32(msg_size);
429 return;
430}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700431EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700432
Sage Weilf24e9982009-10-06 11:31:10 -0700433/*
434 * build new request AND message, calculate layout, and adjust file
435 * extent as needed.
436 *
437 * if the file was recently truncated, we include information about its
438 * old and new size so that the object can be updated appropriately. (we
439 * avoid synchronously deleting truncated objects because it's slow.)
440 *
441 * if @do_sync, include a 'startsync' command so that the osd will flush
442 * data quickly.
443 */
444struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
445 struct ceph_file_layout *layout,
446 struct ceph_vino vino,
447 u64 off, u64 *plen,
448 int opcode, int flags,
449 struct ceph_snap_context *snapc,
450 int do_sync,
451 u32 truncate_seq,
452 u64 truncate_size,
453 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800454 bool use_mempool, int num_reply,
455 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700456{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700457 struct ceph_osd_req_op ops[3];
458 struct ceph_osd_request *req;
459
460 ops[0].op = opcode;
461 ops[0].extent.truncate_seq = truncate_seq;
462 ops[0].extent.truncate_size = truncate_size;
463 ops[0].payload_len = 0;
464
465 if (do_sync) {
466 ops[1].op = CEPH_OSD_OP_STARTSYNC;
467 ops[1].payload_len = 0;
468 ops[2].op = 0;
469 } else
470 ops[1].op = 0;
471
472 req = ceph_osdc_alloc_request(osdc, flags,
473 snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700474 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700475 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700476 if (!req)
477 return NULL;
Sage Weilf24e9982009-10-06 11:31:10 -0700478
479 /* calculate max write size */
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700480 calc_layout(osdc, vino, layout, off, plen, req, ops);
Sage Weilf24e9982009-10-06 11:31:10 -0700481 req->r_file_layout = *layout; /* keep a copy */
482
Sage Weil9bb0ce22011-06-13 16:20:18 -0700483 /* in case it differs from natural (file) alignment that
484 calc_layout filled in for us */
485 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800486 req->r_page_alignment = page_align;
487
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700488 ceph_osdc_build_request(req, off, plen, ops,
489 snapc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700490 mtime,
491 req->r_oid, req->r_oid_len);
Sage Weilf24e9982009-10-06 11:31:10 -0700492
Sage Weilf24e9982009-10-06 11:31:10 -0700493 return req;
494}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700495EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700496
497/*
498 * We keep osd requests in an rbtree, sorted by ->r_tid.
499 */
500static void __insert_request(struct ceph_osd_client *osdc,
501 struct ceph_osd_request *new)
502{
503 struct rb_node **p = &osdc->requests.rb_node;
504 struct rb_node *parent = NULL;
505 struct ceph_osd_request *req = NULL;
506
507 while (*p) {
508 parent = *p;
509 req = rb_entry(parent, struct ceph_osd_request, r_node);
510 if (new->r_tid < req->r_tid)
511 p = &(*p)->rb_left;
512 else if (new->r_tid > req->r_tid)
513 p = &(*p)->rb_right;
514 else
515 BUG();
516 }
517
518 rb_link_node(&new->r_node, parent, p);
519 rb_insert_color(&new->r_node, &osdc->requests);
520}
521
522static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
523 u64 tid)
524{
525 struct ceph_osd_request *req;
526 struct rb_node *n = osdc->requests.rb_node;
527
528 while (n) {
529 req = rb_entry(n, struct ceph_osd_request, r_node);
530 if (tid < req->r_tid)
531 n = n->rb_left;
532 else if (tid > req->r_tid)
533 n = n->rb_right;
534 else
535 return req;
536 }
537 return NULL;
538}
539
540static struct ceph_osd_request *
541__lookup_request_ge(struct ceph_osd_client *osdc,
542 u64 tid)
543{
544 struct ceph_osd_request *req;
545 struct rb_node *n = osdc->requests.rb_node;
546
547 while (n) {
548 req = rb_entry(n, struct ceph_osd_request, r_node);
549 if (tid < req->r_tid) {
550 if (!n->rb_left)
551 return req;
552 n = n->rb_left;
553 } else if (tid > req->r_tid) {
554 n = n->rb_right;
555 } else {
556 return req;
557 }
558 }
559 return NULL;
560}
561
Sage Weil6f6c7002011-01-17 20:34:08 -0800562/*
563 * Resubmit requests pending on the given osd.
564 */
565static void __kick_osd_requests(struct ceph_osd_client *osdc,
566 struct ceph_osd *osd)
567{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700568 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800569 int err;
570
571 dout("__kick_osd_requests osd%d\n", osd->o_osd);
572 err = __reset_osd(osdc, osd);
573 if (err == -EAGAIN)
574 return;
575
576 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
577 list_move(&req->r_req_lru_item, &osdc->req_unsent);
578 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
579 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700580 if (!req->r_linger)
581 req->r_flags |= CEPH_OSD_FLAG_RETRY;
582 }
583
584 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
585 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700586 /*
587 * reregister request prior to unregistering linger so
588 * that r_osd is preserved.
589 */
590 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700591 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700592 list_add(&req->r_req_lru_item, &osdc->req_unsent);
593 list_add(&req->r_osd_item, &req->r_osd->o_requests);
594 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700595 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
596 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800597 }
598}
599
600static void kick_osd_requests(struct ceph_osd_client *osdc,
601 struct ceph_osd *kickosd)
602{
603 mutex_lock(&osdc->request_mutex);
604 __kick_osd_requests(osdc, kickosd);
605 mutex_unlock(&osdc->request_mutex);
606}
Sage Weilf24e9982009-10-06 11:31:10 -0700607
608/*
Sage Weil81b024e2009-10-09 10:29:18 -0700609 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700610 */
611static void osd_reset(struct ceph_connection *con)
612{
613 struct ceph_osd *osd = con->private;
614 struct ceph_osd_client *osdc;
615
616 if (!osd)
617 return;
618 dout("osd_reset osd%d\n", osd->o_osd);
619 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700620 down_read(&osdc->map_sem);
Sage Weil6f6c7002011-01-17 20:34:08 -0800621 kick_osd_requests(osdc, osd);
622 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700623 up_read(&osdc->map_sem);
624}
625
626/*
627 * Track open sessions with osds.
628 */
Alex Eldere10006f2012-05-26 23:26:43 -0500629static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700630{
631 struct ceph_osd *osd;
632
633 osd = kzalloc(sizeof(*osd), GFP_NOFS);
634 if (!osd)
635 return NULL;
636
637 atomic_set(&osd->o_ref, 1);
638 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500639 osd->o_osd = onum;
Sage Weilf24e9982009-10-06 11:31:10 -0700640 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700641 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800642 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700643 osd->o_incarnation = 1;
644
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700645 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800646
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800647 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700648 return osd;
649}
650
651static struct ceph_osd *get_osd(struct ceph_osd *osd)
652{
653 if (atomic_inc_not_zero(&osd->o_ref)) {
654 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
655 atomic_read(&osd->o_ref));
656 return osd;
657 } else {
658 dout("get_osd %p FAIL\n", osd);
659 return NULL;
660 }
661}
662
663static void put_osd(struct ceph_osd *osd)
664{
665 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
666 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500667 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700668 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
669
Alex Eldera2556512012-05-16 15:16:39 -0500670 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500671 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700672 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700673 }
Sage Weilf24e9982009-10-06 11:31:10 -0700674}
675
676/*
677 * remove an osd from our map
678 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800679static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700680{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800681 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700682 BUG_ON(!list_empty(&osd->o_requests));
683 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800684 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700685 ceph_con_close(&osd->o_con);
686 put_osd(osd);
687}
688
Sage Weilaca420b2011-08-31 14:45:53 -0700689static void remove_all_osds(struct ceph_osd_client *osdc)
690{
691 dout("__remove_old_osds %p\n", osdc);
692 mutex_lock(&osdc->request_mutex);
693 while (!RB_EMPTY_ROOT(&osdc->osds)) {
694 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
695 struct ceph_osd, o_node);
696 __remove_osd(osdc, osd);
697 }
698 mutex_unlock(&osdc->request_mutex);
699}
700
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800701static void __move_osd_to_lru(struct ceph_osd_client *osdc,
702 struct ceph_osd *osd)
703{
704 dout("__move_osd_to_lru %p\n", osd);
705 BUG_ON(!list_empty(&osd->o_osd_lru));
706 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700707 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800708}
709
710static void __remove_osd_from_lru(struct ceph_osd *osd)
711{
712 dout("__remove_osd_from_lru %p\n", osd);
713 if (!list_empty(&osd->o_osd_lru))
714 list_del_init(&osd->o_osd_lru);
715}
716
Sage Weilaca420b2011-08-31 14:45:53 -0700717static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800718{
719 struct ceph_osd *osd, *nosd;
720
721 dout("__remove_old_osds %p\n", osdc);
722 mutex_lock(&osdc->request_mutex);
723 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700724 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800725 break;
726 __remove_osd(osdc, osd);
727 }
728 mutex_unlock(&osdc->request_mutex);
729}
730
Sage Weilf24e9982009-10-06 11:31:10 -0700731/*
732 * reset osd connect
733 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800734static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700735{
Sage Weil87b315a2010-03-22 14:51:18 -0700736 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -0700737 int ret = 0;
738
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800739 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700740 if (list_empty(&osd->o_requests) &&
741 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800742 __remove_osd(osdc, osd);
Sage Weil87b315a2010-03-22 14:51:18 -0700743 } else if (memcmp(&osdc->osdmap->osd_addr[osd->o_osd],
744 &osd->o_con.peer_addr,
745 sizeof(osd->o_con.peer_addr)) == 0 &&
746 !ceph_con_opened(&osd->o_con)) {
747 dout(" osd addr hasn't changed and connection never opened,"
748 " letting msgr retry");
749 /* touch each r_stamp for handle_timeout()'s benfit */
750 list_for_each_entry(req, &osd->o_requests, r_osd_item)
751 req->r_stamp = jiffies;
752 ret = -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700753 } else {
754 ceph_con_close(&osd->o_con);
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700755 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd,
756 &osdc->osdmap->osd_addr[osd->o_osd]);
Sage Weilf24e9982009-10-06 11:31:10 -0700757 osd->o_incarnation++;
758 }
759 return ret;
760}
761
762static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
763{
764 struct rb_node **p = &osdc->osds.rb_node;
765 struct rb_node *parent = NULL;
766 struct ceph_osd *osd = NULL;
767
Sage Weilaca420b2011-08-31 14:45:53 -0700768 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700769 while (*p) {
770 parent = *p;
771 osd = rb_entry(parent, struct ceph_osd, o_node);
772 if (new->o_osd < osd->o_osd)
773 p = &(*p)->rb_left;
774 else if (new->o_osd > osd->o_osd)
775 p = &(*p)->rb_right;
776 else
777 BUG();
778 }
779
780 rb_link_node(&new->o_node, parent, p);
781 rb_insert_color(&new->o_node, &osdc->osds);
782}
783
784static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
785{
786 struct ceph_osd *osd;
787 struct rb_node *n = osdc->osds.rb_node;
788
789 while (n) {
790 osd = rb_entry(n, struct ceph_osd, o_node);
791 if (o < osd->o_osd)
792 n = n->rb_left;
793 else if (o > osd->o_osd)
794 n = n->rb_right;
795 else
796 return osd;
797 }
798 return NULL;
799}
800
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800801static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
802{
803 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700804 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800805}
806
807static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
808{
809 cancel_delayed_work(&osdc->timeout_work);
810}
Sage Weilf24e9982009-10-06 11:31:10 -0700811
812/*
813 * Register request, assign tid. If this is the first request, set up
814 * the timeout event.
815 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700816static void __register_request(struct ceph_osd_client *osdc,
817 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700818{
Sage Weilf24e9982009-10-06 11:31:10 -0700819 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800820 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700821 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700822 __insert_request(osdc, req);
823 ceph_osdc_get_request(req);
824 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700825 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800826 dout(" first request, scheduling timeout\n");
827 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700828 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700829}
830
831static void register_request(struct ceph_osd_client *osdc,
832 struct ceph_osd_request *req)
833{
834 mutex_lock(&osdc->request_mutex);
835 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700836 mutex_unlock(&osdc->request_mutex);
837}
838
839/*
840 * called under osdc->request_mutex
841 */
842static void __unregister_request(struct ceph_osd_client *osdc,
843 struct ceph_osd_request *req)
844{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500845 if (RB_EMPTY_NODE(&req->r_node)) {
846 dout("__unregister_request %p tid %lld not registered\n",
847 req, req->r_tid);
848 return;
849 }
850
Sage Weilf24e9982009-10-06 11:31:10 -0700851 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
852 rb_erase(&req->r_node, &osdc->requests);
853 osdc->num_requests--;
854
Sage Weil0ba64782009-10-08 16:57:16 -0700855 if (req->r_osd) {
856 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500857 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700858
859 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700860 if (list_empty(&req->r_osd->o_requests) &&
861 list_empty(&req->r_osd->o_linger_requests)) {
862 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800863 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700864 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700865 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700866 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700867 }
Sage Weilf24e9982009-10-06 11:31:10 -0700868
869 ceph_osdc_put_request(req);
870
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800871 list_del_init(&req->r_req_lru_item);
872 if (osdc->num_requests == 0) {
873 dout(" no requests, canceling timeout\n");
874 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700875 }
876}
877
878/*
879 * Cancel a previously queued request message
880 */
881static void __cancel_request(struct ceph_osd_request *req)
882{
Sage Weil6bc18872010-09-27 10:18:52 -0700883 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500884 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700885 req->r_sent = 0;
886 }
887}
888
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700889static void __register_linger_request(struct ceph_osd_client *osdc,
890 struct ceph_osd_request *req)
891{
892 dout("__register_linger_request %p\n", req);
893 list_add_tail(&req->r_linger_item, &osdc->req_linger);
894 list_add_tail(&req->r_linger_osd, &req->r_osd->o_linger_requests);
895}
896
897static void __unregister_linger_request(struct ceph_osd_client *osdc,
898 struct ceph_osd_request *req)
899{
900 dout("__unregister_linger_request %p\n", req);
901 if (req->r_osd) {
902 list_del_init(&req->r_linger_item);
903 list_del_init(&req->r_linger_osd);
904
905 if (list_empty(&req->r_osd->o_requests) &&
906 list_empty(&req->r_osd->o_linger_requests)) {
907 dout("moving osd to %p lru\n", req->r_osd);
908 __move_osd_to_lru(osdc, req->r_osd);
909 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700910 if (list_empty(&req->r_osd_item))
911 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700912 }
913}
914
915void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
916 struct ceph_osd_request *req)
917{
918 mutex_lock(&osdc->request_mutex);
919 if (req->r_linger) {
920 __unregister_linger_request(osdc, req);
921 ceph_osdc_put_request(req);
922 }
923 mutex_unlock(&osdc->request_mutex);
924}
925EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
926
927void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
928 struct ceph_osd_request *req)
929{
930 if (!req->r_linger) {
931 dout("set_request_linger %p\n", req);
932 req->r_linger = 1;
933 /*
934 * caller is now responsible for calling
935 * unregister_linger_request
936 */
937 ceph_osdc_get_request(req);
938 }
939}
940EXPORT_SYMBOL(ceph_osdc_set_request_linger);
941
Sage Weilf24e9982009-10-06 11:31:10 -0700942/*
943 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
944 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300945 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800946 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700947 *
948 * Return 0 if unchanged, 1 if changed, or negative on error.
949 *
950 * Caller should hold map_sem for read and request_mutex.
951 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800952static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700953 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700954{
955 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800956 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700957 int acting[CEPH_PG_MAX_SIZE];
958 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700959 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700960
Sage Weil6f6c7002011-01-17 20:34:08 -0800961 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700962 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
963 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800964 if (err) {
965 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700966 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800967 }
Sage Weil51042122009-11-04 11:39:12 -0800968 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800969 req->r_pgid = pgid;
970
Sage Weild85b7052010-05-10 10:24:48 -0700971 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
972 if (err > 0) {
973 o = acting[0];
974 num = err;
975 }
Sage Weilf24e9982009-10-06 11:31:10 -0700976
Sage Weil38d64532011-10-14 13:33:55 -0700977 if ((!force_resend &&
978 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700979 req->r_sent >= req->r_osd->o_incarnation &&
980 req->r_num_pg_osds == num &&
981 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700982 (req->r_osd == NULL && o == -1))
983 return 0; /* no change */
984
Sage Weil6f6c7002011-01-17 20:34:08 -0800985 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800986 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700987 req->r_osd ? req->r_osd->o_osd : -1);
988
Sage Weild85b7052010-05-10 10:24:48 -0700989 /* record full pg acting set */
990 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
991 req->r_num_pg_osds = num;
992
Sage Weilf24e9982009-10-06 11:31:10 -0700993 if (req->r_osd) {
994 __cancel_request(req);
995 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700996 req->r_osd = NULL;
997 }
998
999 req->r_osd = __lookup_osd(osdc, o);
1000 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001001 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001002 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001003 if (!req->r_osd) {
1004 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001005 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001006 }
Sage Weilf24e9982009-10-06 11:31:10 -07001007
Sage Weil6f6c7002011-01-17 20:34:08 -08001008 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001009 __insert_osd(osdc, req->r_osd);
1010
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001011 ceph_con_open(&req->r_osd->o_con,
1012 CEPH_ENTITY_TYPE_OSD, o,
1013 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001014 }
1015
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001016 if (req->r_osd) {
1017 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001018 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001019 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1020 } else {
1021 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001022 }
Sage Weild85b7052010-05-10 10:24:48 -07001023 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001024
1025out:
Sage Weilf24e9982009-10-06 11:31:10 -07001026 return err;
1027}
1028
1029/*
1030 * caller should hold map_sem (for read) and request_mutex
1031 */
Sage Weil56e925b2012-01-03 12:34:34 -08001032static void __send_request(struct ceph_osd_client *osdc,
1033 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001034{
1035 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001036
1037 dout("send_request %p tid %llu to osd%d flags %d\n",
1038 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1039
1040 reqhead = req->r_request->front.iov_base;
1041 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1042 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1043 reqhead->reassert_version = req->r_reassert_version;
1044
Sage Weil3dd72fc2010-03-22 14:42:30 -07001045 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001046 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001047
1048 ceph_msg_get(req->r_request); /* send consumes a ref */
1049 ceph_con_send(&req->r_osd->o_con, req->r_request);
1050 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001051}
1052
1053/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001054 * Send any requests in the queue (req_unsent).
1055 */
1056static void send_queued(struct ceph_osd_client *osdc)
1057{
1058 struct ceph_osd_request *req, *tmp;
1059
1060 dout("send_queued\n");
1061 mutex_lock(&osdc->request_mutex);
1062 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1063 __send_request(osdc, req);
1064 }
1065 mutex_unlock(&osdc->request_mutex);
1066}
1067
1068/*
Sage Weilf24e9982009-10-06 11:31:10 -07001069 * Timeout callback, called every N seconds when 1 or more osd
1070 * requests has been active for more than N seconds. When this
1071 * happens, we ping all OSDs with requests who have timed out to
1072 * ensure any communications channel reset is detected. Reset the
1073 * request timeouts another N seconds in the future as we go.
1074 * Reschedule the timeout event another N seconds in future (unless
1075 * there are no open requests).
1076 */
1077static void handle_timeout(struct work_struct *work)
1078{
1079 struct ceph_osd_client *osdc =
1080 container_of(work, struct ceph_osd_client, timeout_work.work);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001081 struct ceph_osd_request *req, *last_req = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001082 struct ceph_osd *osd;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001083 unsigned long timeout = osdc->client->options->osd_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001084 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001085 osdc->client->options->osd_keepalive_timeout * HZ;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001086 unsigned long last_stamp = 0;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001087 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001088 dout("timeout\n");
1089 down_read(&osdc->map_sem);
1090
1091 ceph_monc_request_next_osdmap(&osdc->client->monc);
1092
1093 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001094
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001095 /*
1096 * reset osds that appear to be _really_ unresponsive. this
1097 * is a failsafe measure.. we really shouldn't be getting to
1098 * this point if the system is working properly. the monitors
1099 * should mark the osd as failed and we should find out about
1100 * it from an updated osd map.
1101 */
Sage Weilf26e6812010-04-21 11:09:38 -07001102 while (timeout && !list_empty(&osdc->req_lru)) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001103 req = list_entry(osdc->req_lru.next, struct ceph_osd_request,
1104 r_req_lru_item);
1105
Sage Weil4cf9d542011-07-26 11:27:24 -07001106 /* hasn't been long enough since we sent it? */
Sage Weil3dd72fc2010-03-22 14:42:30 -07001107 if (time_before(jiffies, req->r_stamp + timeout))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001108 break;
1109
Sage Weil4cf9d542011-07-26 11:27:24 -07001110 /* hasn't been long enough since it was acked? */
1111 if (req->r_request->ack_stamp == 0 ||
1112 time_before(jiffies, req->r_request->ack_stamp + timeout))
1113 break;
1114
Sage Weil3dd72fc2010-03-22 14:42:30 -07001115 BUG_ON(req == last_req && req->r_stamp == last_stamp);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001116 last_req = req;
Sage Weil3dd72fc2010-03-22 14:42:30 -07001117 last_stamp = req->r_stamp;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001118
1119 osd = req->r_osd;
1120 BUG_ON(!osd);
1121 pr_warning(" tid %llu timed out on osd%d, will reset osd\n",
1122 req->r_tid, osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -08001123 __kick_osd_requests(osdc, osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001124 }
1125
1126 /*
1127 * ping osds that are a bit slow. this ensures that if there
1128 * is a break in the TCP connection we will notice, and reopen
1129 * a connection with that osd (from the fault callback).
1130 */
1131 INIT_LIST_HEAD(&slow_osds);
1132 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001133 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001134 break;
1135
1136 osd = req->r_osd;
1137 BUG_ON(!osd);
1138 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001139 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001140 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1141 }
1142 while (!list_empty(&slow_osds)) {
1143 osd = list_entry(slow_osds.next, struct ceph_osd,
1144 o_keepalive_item);
1145 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001146 ceph_con_keepalive(&osd->o_con);
1147 }
1148
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001149 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001150 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001151 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001152 up_read(&osdc->map_sem);
1153}
1154
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001155static void handle_osds_timeout(struct work_struct *work)
1156{
1157 struct ceph_osd_client *osdc =
1158 container_of(work, struct ceph_osd_client,
1159 osds_timeout_work.work);
1160 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001161 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001162
1163 dout("osds timeout\n");
1164 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001165 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001166 up_read(&osdc->map_sem);
1167
1168 schedule_delayed_work(&osdc->osds_timeout_work,
1169 round_jiffies_relative(delay));
1170}
1171
Sage Weil25845472011-06-03 09:37:09 -07001172static void complete_request(struct ceph_osd_request *req)
1173{
1174 if (req->r_safe_callback)
1175 req->r_safe_callback(req, NULL);
1176 complete_all(&req->r_safe_completion); /* fsync waiter */
1177}
1178
Sage Weilf24e9982009-10-06 11:31:10 -07001179/*
1180 * handle osd op reply. either call the callback if it is specified,
1181 * or do the completion to wake up the waiting thread.
1182 */
Sage Weil350b1c32009-12-22 10:45:45 -08001183static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1184 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001185{
1186 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1187 struct ceph_osd_request *req;
1188 u64 tid;
1189 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001190 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001191
Sage Weil6df058c2009-12-22 11:24:33 -08001192 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001193 if (msg->front.iov_len < sizeof(*rhead))
1194 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001195 numops = le32_to_cpu(rhead->num_ops);
1196 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001197 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001198 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1199 numops * sizeof(struct ceph_osd_op))
1200 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001201 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001202 /* lookup */
1203 mutex_lock(&osdc->request_mutex);
1204 req = __lookup_request(osdc, tid);
1205 if (req == NULL) {
1206 dout("handle_reply tid %llu dne\n", tid);
1207 mutex_unlock(&osdc->request_mutex);
1208 return;
1209 }
1210 ceph_osdc_get_request(req);
1211 flags = le32_to_cpu(rhead->flags);
1212
Sage Weil350b1c32009-12-22 10:45:45 -08001213 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001214 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001215 * avoid a (safe but slower) revoke later.
1216 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001217 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001218 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001219 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001220 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001221 }
1222
Sage Weilf24e9982009-10-06 11:31:10 -07001223 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001224 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001225
1226 req->r_result = le32_to_cpu(rhead->result);
1227 bytes = le32_to_cpu(msg->hdr.data_len);
1228 dout("handle_reply result %d bytes %d\n", req->r_result,
1229 bytes);
1230 if (req->r_result == 0)
1231 req->r_result = bytes;
1232
1233 /* in case this is a write and we need to replay, */
1234 req->r_reassert_version = rhead->reassert_version;
1235
1236 req->r_got_reply = 1;
1237 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1238 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001239 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001240 goto done;
1241 }
1242
1243 dout("handle_reply tid %llu flags %d\n", tid, flags);
1244
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001245 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1246 __register_linger_request(osdc, req);
1247
Sage Weilf24e9982009-10-06 11:31:10 -07001248 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001249 if (result < 0 ||
1250 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001251 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1252 __unregister_request(osdc, req);
1253
1254 mutex_unlock(&osdc->request_mutex);
1255
1256 if (req->r_callback)
1257 req->r_callback(req, msg);
1258 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001259 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001260
Sage Weil25845472011-06-03 09:37:09 -07001261 if (flags & CEPH_OSD_FLAG_ONDISK)
1262 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001263
1264done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001265 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001266 ceph_osdc_put_request(req);
1267 return;
1268
1269bad:
1270 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1271 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1272 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001273 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001274}
1275
Sage Weil6f6c7002011-01-17 20:34:08 -08001276static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001277{
Sage Weilf24e9982009-10-06 11:31:10 -07001278 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001279
Sage Weil6f6c7002011-01-17 20:34:08 -08001280 for (p = rb_first(&osdc->osds); p; p = n) {
1281 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001282
Sage Weil6f6c7002011-01-17 20:34:08 -08001283 n = rb_next(p);
1284 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1285 memcmp(&osd->o_con.peer_addr,
1286 ceph_osd_addr(osdc->osdmap,
1287 osd->o_osd),
1288 sizeof(struct ceph_entity_addr)) != 0)
1289 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001290 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001291}
1292
1293/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001294 * Requeue requests whose mapping to an OSD has changed. If requests map to
1295 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001296 *
1297 * Caller should hold map_sem for read and request_mutex.
1298 */
Sage Weil38d64532011-10-14 13:33:55 -07001299static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001300{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001301 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001302 struct rb_node *p;
1303 int needmap = 0;
1304 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001305
Sage Weil38d64532011-10-14 13:33:55 -07001306 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001307 mutex_lock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001308 for (p = rb_first(&osdc->requests); p; p = rb_next(p)) {
1309 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil38d64532011-10-14 13:33:55 -07001310 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001311 if (err < 0)
1312 continue; /* error */
1313 if (req->r_osd == NULL) {
1314 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1315 needmap++; /* request a newer map */
1316 } else if (err > 0) {
1317 dout("%p tid %llu requeued on osd%d\n", req, req->r_tid,
1318 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001319 if (!req->r_linger)
1320 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6f6c7002011-01-17 20:34:08 -08001321 }
1322 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001323
1324 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1325 r_linger_item) {
1326 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1327
Sage Weil38d64532011-10-14 13:33:55 -07001328 err = __map_request(osdc, req, force_resend);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001329 if (err == 0)
1330 continue; /* no change and no osd was specified */
1331 if (err < 0)
1332 continue; /* hrm! */
1333 if (req->r_osd == NULL) {
1334 dout("tid %llu maps to no valid osd\n", req->r_tid);
1335 needmap++; /* request a newer map */
1336 continue;
1337 }
1338
1339 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1340 req->r_osd ? req->r_osd->o_osd : -1);
1341 __unregister_linger_request(osdc, req);
1342 __register_request(osdc, req);
1343 }
Sage Weilf24e9982009-10-06 11:31:10 -07001344 mutex_unlock(&osdc->request_mutex);
1345
1346 if (needmap) {
1347 dout("%d requests for down osds, need new map\n", needmap);
1348 ceph_monc_request_next_osdmap(&osdc->client->monc);
1349 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001350}
Sage Weil6f6c7002011-01-17 20:34:08 -08001351
1352
Sage Weilf24e9982009-10-06 11:31:10 -07001353/*
1354 * Process updated osd map.
1355 *
1356 * The message contains any number of incremental and full maps, normally
1357 * indicating some sort of topology change in the cluster. Kick requests
1358 * off to different OSDs as needed.
1359 */
1360void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1361{
1362 void *p, *end, *next;
1363 u32 nr_maps, maplen;
1364 u32 epoch;
1365 struct ceph_osdmap *newmap = NULL, *oldmap;
1366 int err;
1367 struct ceph_fsid fsid;
1368
1369 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1370 p = msg->front.iov_base;
1371 end = p + msg->front.iov_len;
1372
1373 /* verify fsid */
1374 ceph_decode_need(&p, end, sizeof(fsid), bad);
1375 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001376 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1377 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001378
1379 down_write(&osdc->map_sem);
1380
1381 /* incremental maps */
1382 ceph_decode_32_safe(&p, end, nr_maps, bad);
1383 dout(" %d inc maps\n", nr_maps);
1384 while (nr_maps > 0) {
1385 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001386 epoch = ceph_decode_32(&p);
1387 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001388 ceph_decode_need(&p, end, maplen, bad);
1389 next = p + maplen;
1390 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1391 dout("applying incremental map %u len %d\n",
1392 epoch, maplen);
1393 newmap = osdmap_apply_incremental(&p, next,
1394 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001395 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001396 if (IS_ERR(newmap)) {
1397 err = PTR_ERR(newmap);
1398 goto bad;
1399 }
Sage Weil30dc6382009-12-21 14:49:37 -08001400 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001401 if (newmap != osdc->osdmap) {
1402 ceph_osdmap_destroy(osdc->osdmap);
1403 osdc->osdmap = newmap;
1404 }
Sage Weil38d64532011-10-14 13:33:55 -07001405 kick_requests(osdc, 0);
Sage Weil6f6c7002011-01-17 20:34:08 -08001406 reset_changed_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001407 } else {
1408 dout("ignoring incremental map %u len %d\n",
1409 epoch, maplen);
1410 }
1411 p = next;
1412 nr_maps--;
1413 }
1414 if (newmap)
1415 goto done;
1416
1417 /* full maps */
1418 ceph_decode_32_safe(&p, end, nr_maps, bad);
1419 dout(" %d full maps\n", nr_maps);
1420 while (nr_maps) {
1421 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001422 epoch = ceph_decode_32(&p);
1423 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001424 ceph_decode_need(&p, end, maplen, bad);
1425 if (nr_maps > 1) {
1426 dout("skipping non-latest full map %u len %d\n",
1427 epoch, maplen);
1428 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1429 dout("skipping full map %u len %d, "
1430 "older than our %u\n", epoch, maplen,
1431 osdc->osdmap->epoch);
1432 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001433 int skipped_map = 0;
1434
Sage Weilf24e9982009-10-06 11:31:10 -07001435 dout("taking full map %u len %d\n", epoch, maplen);
1436 newmap = osdmap_decode(&p, p+maplen);
1437 if (IS_ERR(newmap)) {
1438 err = PTR_ERR(newmap);
1439 goto bad;
1440 }
Sage Weil30dc6382009-12-21 14:49:37 -08001441 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001442 oldmap = osdc->osdmap;
1443 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001444 if (oldmap) {
1445 if (oldmap->epoch + 1 < newmap->epoch)
1446 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001447 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001448 }
1449 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001450 }
1451 p += maplen;
1452 nr_maps--;
1453 }
1454
1455done:
1456 downgrade_write(&osdc->map_sem);
1457 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001458
1459 /*
1460 * subscribe to subsequent osdmap updates if full to ensure
1461 * we find out when we are no longer full and stop returning
1462 * ENOSPC.
1463 */
1464 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1465 ceph_monc_request_next_osdmap(&osdc->client->monc);
1466
Sage Weil6f6c7002011-01-17 20:34:08 -08001467 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001468 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001469 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001470 return;
1471
1472bad:
1473 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001474 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001475 up_write(&osdc->map_sem);
1476 return;
1477}
1478
Sage Weilf24e9982009-10-06 11:31:10 -07001479/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001480 * watch/notify callback event infrastructure
1481 *
1482 * These callbacks are used both for watch and notify operations.
1483 */
1484static void __release_event(struct kref *kref)
1485{
1486 struct ceph_osd_event *event =
1487 container_of(kref, struct ceph_osd_event, kref);
1488
1489 dout("__release_event %p\n", event);
1490 kfree(event);
1491}
1492
1493static void get_event(struct ceph_osd_event *event)
1494{
1495 kref_get(&event->kref);
1496}
1497
1498void ceph_osdc_put_event(struct ceph_osd_event *event)
1499{
1500 kref_put(&event->kref, __release_event);
1501}
1502EXPORT_SYMBOL(ceph_osdc_put_event);
1503
1504static void __insert_event(struct ceph_osd_client *osdc,
1505 struct ceph_osd_event *new)
1506{
1507 struct rb_node **p = &osdc->event_tree.rb_node;
1508 struct rb_node *parent = NULL;
1509 struct ceph_osd_event *event = NULL;
1510
1511 while (*p) {
1512 parent = *p;
1513 event = rb_entry(parent, struct ceph_osd_event, node);
1514 if (new->cookie < event->cookie)
1515 p = &(*p)->rb_left;
1516 else if (new->cookie > event->cookie)
1517 p = &(*p)->rb_right;
1518 else
1519 BUG();
1520 }
1521
1522 rb_link_node(&new->node, parent, p);
1523 rb_insert_color(&new->node, &osdc->event_tree);
1524}
1525
1526static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1527 u64 cookie)
1528{
1529 struct rb_node **p = &osdc->event_tree.rb_node;
1530 struct rb_node *parent = NULL;
1531 struct ceph_osd_event *event = NULL;
1532
1533 while (*p) {
1534 parent = *p;
1535 event = rb_entry(parent, struct ceph_osd_event, node);
1536 if (cookie < event->cookie)
1537 p = &(*p)->rb_left;
1538 else if (cookie > event->cookie)
1539 p = &(*p)->rb_right;
1540 else
1541 return event;
1542 }
1543 return NULL;
1544}
1545
1546static void __remove_event(struct ceph_osd_event *event)
1547{
1548 struct ceph_osd_client *osdc = event->osdc;
1549
1550 if (!RB_EMPTY_NODE(&event->node)) {
1551 dout("__remove_event removed %p\n", event);
1552 rb_erase(&event->node, &osdc->event_tree);
1553 ceph_osdc_put_event(event);
1554 } else {
1555 dout("__remove_event didn't remove %p\n", event);
1556 }
1557}
1558
1559int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1560 void (*event_cb)(u64, u64, u8, void *),
1561 int one_shot, void *data,
1562 struct ceph_osd_event **pevent)
1563{
1564 struct ceph_osd_event *event;
1565
1566 event = kmalloc(sizeof(*event), GFP_NOIO);
1567 if (!event)
1568 return -ENOMEM;
1569
1570 dout("create_event %p\n", event);
1571 event->cb = event_cb;
1572 event->one_shot = one_shot;
1573 event->data = data;
1574 event->osdc = osdc;
1575 INIT_LIST_HEAD(&event->osd_node);
1576 kref_init(&event->kref); /* one ref for us */
1577 kref_get(&event->kref); /* one ref for the caller */
1578 init_completion(&event->completion);
1579
1580 spin_lock(&osdc->event_lock);
1581 event->cookie = ++osdc->event_count;
1582 __insert_event(osdc, event);
1583 spin_unlock(&osdc->event_lock);
1584
1585 *pevent = event;
1586 return 0;
1587}
1588EXPORT_SYMBOL(ceph_osdc_create_event);
1589
1590void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1591{
1592 struct ceph_osd_client *osdc = event->osdc;
1593
1594 dout("cancel_event %p\n", event);
1595 spin_lock(&osdc->event_lock);
1596 __remove_event(event);
1597 spin_unlock(&osdc->event_lock);
1598 ceph_osdc_put_event(event); /* caller's */
1599}
1600EXPORT_SYMBOL(ceph_osdc_cancel_event);
1601
1602
1603static void do_event_work(struct work_struct *work)
1604{
1605 struct ceph_osd_event_work *event_work =
1606 container_of(work, struct ceph_osd_event_work, work);
1607 struct ceph_osd_event *event = event_work->event;
1608 u64 ver = event_work->ver;
1609 u64 notify_id = event_work->notify_id;
1610 u8 opcode = event_work->opcode;
1611
1612 dout("do_event_work completing %p\n", event);
1613 event->cb(ver, notify_id, opcode, event->data);
1614 complete(&event->completion);
1615 dout("do_event_work completed %p\n", event);
1616 ceph_osdc_put_event(event);
1617 kfree(event_work);
1618}
1619
1620
1621/*
1622 * Process osd watch notifications
1623 */
1624void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1625{
1626 void *p, *end;
1627 u8 proto_ver;
1628 u64 cookie, ver, notify_id;
1629 u8 opcode;
1630 struct ceph_osd_event *event;
1631 struct ceph_osd_event_work *event_work;
1632
1633 p = msg->front.iov_base;
1634 end = p + msg->front.iov_len;
1635
1636 ceph_decode_8_safe(&p, end, proto_ver, bad);
1637 ceph_decode_8_safe(&p, end, opcode, bad);
1638 ceph_decode_64_safe(&p, end, cookie, bad);
1639 ceph_decode_64_safe(&p, end, ver, bad);
1640 ceph_decode_64_safe(&p, end, notify_id, bad);
1641
1642 spin_lock(&osdc->event_lock);
1643 event = __find_event(osdc, cookie);
1644 if (event) {
1645 get_event(event);
1646 if (event->one_shot)
1647 __remove_event(event);
1648 }
1649 spin_unlock(&osdc->event_lock);
1650 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1651 cookie, ver, event);
1652 if (event) {
1653 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001654 if (!event_work) {
1655 dout("ERROR: could not allocate event_work\n");
1656 goto done_err;
1657 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001658 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001659 event_work->event = event;
1660 event_work->ver = ver;
1661 event_work->notify_id = notify_id;
1662 event_work->opcode = opcode;
1663 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1664 dout("WARNING: failed to queue notify event work\n");
1665 goto done_err;
1666 }
1667 }
1668
1669 return;
1670
1671done_err:
1672 complete(&event->completion);
1673 ceph_osdc_put_event(event);
1674 return;
1675
1676bad:
1677 pr_err("osdc handle_watch_notify corrupt msg\n");
1678 return;
1679}
1680
1681int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1682{
1683 int err;
1684
1685 dout("wait_event %p\n", event);
1686 err = wait_for_completion_interruptible_timeout(&event->completion,
1687 timeout * HZ);
1688 ceph_osdc_put_event(event);
1689 if (err > 0)
1690 err = 0;
1691 dout("wait_event %p returns %d\n", event, err);
1692 return err;
1693}
1694EXPORT_SYMBOL(ceph_osdc_wait_event);
1695
1696/*
Sage Weilf24e9982009-10-06 11:31:10 -07001697 * Register request, send initial attempt.
1698 */
1699int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1700 struct ceph_osd_request *req,
1701 bool nofail)
1702{
Sage Weilc1ea8822009-10-08 16:55:47 -07001703 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001704
1705 req->r_request->pages = req->r_pages;
1706 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001707#ifdef CONFIG_BLOCK
1708 req->r_request->bio = req->r_bio;
1709#endif
1710 req->r_request->trail = req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001711
1712 register_request(osdc, req);
1713
1714 down_read(&osdc->map_sem);
1715 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001716 /*
1717 * a racing kick_requests() may have sent the message for us
1718 * while we dropped request_mutex above, so only send now if
1719 * the request still han't been touched yet.
1720 */
1721 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001722 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001723 if (rc < 0) {
1724 if (nofail) {
1725 dout("osdc_start_request failed map, "
1726 " will retry %lld\n", req->r_tid);
1727 rc = 0;
1728 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001729 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001730 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001731 if (req->r_osd == NULL) {
1732 dout("send_request %p no up osds in pg\n", req);
1733 ceph_monc_request_next_osdmap(&osdc->client->monc);
1734 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001735 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001736 }
Sage Weil56e925b2012-01-03 12:34:34 -08001737 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001738 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001739
1740out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001741 mutex_unlock(&osdc->request_mutex);
1742 up_read(&osdc->map_sem);
1743 return rc;
1744}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001745EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001746
1747/*
1748 * wait for a request to complete
1749 */
1750int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1751 struct ceph_osd_request *req)
1752{
1753 int rc;
1754
1755 rc = wait_for_completion_interruptible(&req->r_completion);
1756 if (rc < 0) {
1757 mutex_lock(&osdc->request_mutex);
1758 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001759 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001760 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001761 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001762 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001763 return rc;
1764 }
1765
1766 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1767 return req->r_result;
1768}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001769EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001770
1771/*
1772 * sync - wait for all in-flight requests to flush. avoid starvation.
1773 */
1774void ceph_osdc_sync(struct ceph_osd_client *osdc)
1775{
1776 struct ceph_osd_request *req;
1777 u64 last_tid, next_tid = 0;
1778
1779 mutex_lock(&osdc->request_mutex);
1780 last_tid = osdc->last_tid;
1781 while (1) {
1782 req = __lookup_request_ge(osdc, next_tid);
1783 if (!req)
1784 break;
1785 if (req->r_tid > last_tid)
1786 break;
1787
1788 next_tid = req->r_tid + 1;
1789 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1790 continue;
1791
1792 ceph_osdc_get_request(req);
1793 mutex_unlock(&osdc->request_mutex);
1794 dout("sync waiting on tid %llu (last is %llu)\n",
1795 req->r_tid, last_tid);
1796 wait_for_completion(&req->r_safe_completion);
1797 mutex_lock(&osdc->request_mutex);
1798 ceph_osdc_put_request(req);
1799 }
1800 mutex_unlock(&osdc->request_mutex);
1801 dout("sync done (thru tid %llu)\n", last_tid);
1802}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001803EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001804
1805/*
1806 * init, shutdown
1807 */
1808int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1809{
1810 int err;
1811
1812 dout("init\n");
1813 osdc->client = client;
1814 osdc->osdmap = NULL;
1815 init_rwsem(&osdc->map_sem);
1816 init_completion(&osdc->map_waiters);
1817 osdc->last_requested_map = 0;
1818 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001819 osdc->last_tid = 0;
1820 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001821 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001822 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001823 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001824 INIT_LIST_HEAD(&osdc->req_unsent);
1825 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001826 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001827 osdc->num_requests = 0;
1828 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001829 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001830 spin_lock_init(&osdc->event_lock);
1831 osdc->event_tree = RB_ROOT;
1832 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001833
1834 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001835 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001836
Sage Weil5f44f142009-11-18 14:52:18 -08001837 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001838 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1839 sizeof(struct ceph_osd_request));
1840 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001841 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001842
Sage Weild50b4092012-07-09 14:22:34 -07001843 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1844 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001845 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001846 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001847 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001848 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001849 OSD_OPREPLY_FRONT_LEN, 10, true,
1850 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001851 if (err < 0)
1852 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001853
1854 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1855 if (IS_ERR(osdc->notify_wq)) {
1856 err = PTR_ERR(osdc->notify_wq);
1857 osdc->notify_wq = NULL;
1858 goto out_msgpool;
1859 }
Sage Weilf24e9982009-10-06 11:31:10 -07001860 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001861
Sage Weilc16e7862010-03-01 13:02:00 -08001862out_msgpool:
1863 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001864out_mempool:
1865 mempool_destroy(osdc->req_mempool);
1866out:
1867 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001868}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001869EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001870
1871void ceph_osdc_stop(struct ceph_osd_client *osdc)
1872{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001873 flush_workqueue(osdc->notify_wq);
1874 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001875 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001876 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001877 if (osdc->osdmap) {
1878 ceph_osdmap_destroy(osdc->osdmap);
1879 osdc->osdmap = NULL;
1880 }
Sage Weilaca420b2011-08-31 14:45:53 -07001881 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001882 mempool_destroy(osdc->req_mempool);
1883 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001884 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001885}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001886EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001887
1888/*
1889 * Read some contiguous pages. If we cross a stripe boundary, shorten
1890 * *plen. Return number of bytes read, or error.
1891 */
1892int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1893 struct ceph_vino vino, struct ceph_file_layout *layout,
1894 u64 off, u64 *plen,
1895 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001896 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001897{
1898 struct ceph_osd_request *req;
1899 int rc = 0;
1900
1901 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1902 vino.snap, off, *plen);
1903 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1904 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1905 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001906 false, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001907 if (!req)
1908 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001909
1910 /* it may be a short read due to an object boundary */
1911 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001912
Sage Weilb7495fc2010-11-09 12:43:12 -08001913 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1914 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001915
1916 rc = ceph_osdc_start_request(osdc, req, false);
1917 if (!rc)
1918 rc = ceph_osdc_wait_request(osdc, req);
1919
1920 ceph_osdc_put_request(req);
1921 dout("readpages result %d\n", rc);
1922 return rc;
1923}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001924EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001925
1926/*
1927 * do a synchronous write on N pages
1928 */
1929int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1930 struct ceph_file_layout *layout,
1931 struct ceph_snap_context *snapc,
1932 u64 off, u64 len,
1933 u32 truncate_seq, u64 truncate_size,
1934 struct timespec *mtime,
1935 struct page **pages, int num_pages,
1936 int flags, int do_sync, bool nofail)
1937{
1938 struct ceph_osd_request *req;
1939 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001940 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001941
1942 BUG_ON(vino.snap != CEPH_NOSNAP);
1943 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1944 CEPH_OSD_OP_WRITE,
1945 flags | CEPH_OSD_FLAG_ONDISK |
1946 CEPH_OSD_FLAG_WRITE,
1947 snapc, do_sync,
1948 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001949 nofail, 1, page_align);
Sage Weila79832f2010-04-01 16:06:19 -07001950 if (!req)
1951 return -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001952
1953 /* it may be a short write due to an object boundary */
1954 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001955 dout("writepages %llu~%llu (%d pages)\n", off, len,
1956 req->r_num_pages);
1957
1958 rc = ceph_osdc_start_request(osdc, req, nofail);
1959 if (!rc)
1960 rc = ceph_osdc_wait_request(osdc, req);
1961
1962 ceph_osdc_put_request(req);
1963 if (rc == 0)
1964 rc = len;
1965 dout("writepages result %d\n", rc);
1966 return rc;
1967}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001968EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001969
1970/*
1971 * handle incoming message
1972 */
1973static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1974{
1975 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001976 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001977 int type = le16_to_cpu(msg->hdr.type);
1978
1979 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001980 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001981 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001982
1983 switch (type) {
1984 case CEPH_MSG_OSD_MAP:
1985 ceph_osdc_handle_map(osdc, msg);
1986 break;
1987 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001988 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001989 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001990 case CEPH_MSG_WATCH_NOTIFY:
1991 handle_watch_notify(osdc, msg);
1992 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001993
1994 default:
1995 pr_err("received unknown message type %d %s\n", type,
1996 ceph_msg_type_name(type));
1997 }
Sage Weil4a32f932010-06-13 10:27:53 -07001998out:
Sage Weilf24e9982009-10-06 11:31:10 -07001999 ceph_msg_put(msg);
2000}
2001
Sage Weil5b3a4db2010-02-19 21:43:23 -08002002/*
Sage Weil21b667f2010-03-04 10:22:59 -08002003 * lookup and return message for incoming reply. set up reply message
2004 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002005 */
2006static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002007 struct ceph_msg_header *hdr,
2008 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002009{
2010 struct ceph_osd *osd = con->private;
2011 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002012 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002013 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002014 int front = le32_to_cpu(hdr->front_len);
2015 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002016 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002017
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002018 tid = le64_to_cpu(hdr->tid);
2019 mutex_lock(&osdc->request_mutex);
2020 req = __lookup_request(osdc, tid);
2021 if (!req) {
2022 *skip = 1;
2023 m = NULL;
Sage Weilc16e7862010-03-01 13:02:00 -08002024 pr_info("get_reply unknown tid %llu from osd%d\n", tid,
Sage Weil5b3a4db2010-02-19 21:43:23 -08002025 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002026 goto out;
2027 }
Sage Weilc16e7862010-03-01 13:02:00 -08002028
2029 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002030 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002031 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002032 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002033 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002034 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002035 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002036
Sage Weilc16e7862010-03-01 13:02:00 -08002037 if (front > req->r_reply->front.iov_len) {
2038 pr_warning("get_reply front %d > preallocated %d\n",
2039 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002040 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002041 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002042 goto out;
2043 ceph_msg_put(req->r_reply);
2044 req->r_reply = m;
2045 }
2046 m = ceph_msg_get(req->r_reply);
2047
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002048 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002049 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002050
2051 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002052 pr_warning("tid %lld reply has %d bytes %d pages, we"
2053 " had only %d pages ready\n", tid, data_len,
2054 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002055 *skip = 1;
2056 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002057 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002058 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002059 }
Sage Weil21b667f2010-03-04 10:22:59 -08002060 m->pages = req->r_pages;
2061 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002062 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002063#ifdef CONFIG_BLOCK
2064 m->bio = req->r_bio;
2065#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002066 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002067 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002068 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002069 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002070
2071out:
2072 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002073 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002074
2075}
2076
2077static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2078 struct ceph_msg_header *hdr,
2079 int *skip)
2080{
2081 struct ceph_osd *osd = con->private;
2082 int type = le16_to_cpu(hdr->type);
2083 int front = le32_to_cpu(hdr->front_len);
2084
Alex Elder1c20f2d2012-06-04 14:43:32 -05002085 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002086 switch (type) {
2087 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002088 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002089 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002090 case CEPH_MSG_OSD_OPREPLY:
2091 return get_reply(con, hdr, skip);
2092 default:
2093 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2094 osd->o_osd);
2095 *skip = 1;
2096 return NULL;
2097 }
Sage Weilf24e9982009-10-06 11:31:10 -07002098}
2099
2100/*
2101 * Wrappers to refcount containing ceph_osd struct
2102 */
2103static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2104{
2105 struct ceph_osd *osd = con->private;
2106 if (get_osd(osd))
2107 return con;
2108 return NULL;
2109}
2110
2111static void put_osd_con(struct ceph_connection *con)
2112{
2113 struct ceph_osd *osd = con->private;
2114 put_osd(osd);
2115}
2116
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002117/*
2118 * authentication
2119 */
Alex Eldera3530df2012-05-16 15:16:39 -05002120/*
2121 * Note: returned pointer is the address of a structure that's
2122 * managed separately. Caller must *not* attempt to free it.
2123 */
2124static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002125 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002126{
2127 struct ceph_osd *o = con->private;
2128 struct ceph_osd_client *osdc = o->o_osdc;
2129 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002130 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002131
Alex Elder74f18692012-05-16 15:16:39 -05002132 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002133 if (ac->ops && ac->ops->destroy_authorizer)
2134 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002135 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002136 }
Alex Eldera2556512012-05-16 15:16:39 -05002137 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002138 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2139 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002140 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002141 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002142 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002143 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002144
Alex Eldera3530df2012-05-16 15:16:39 -05002145 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002146}
2147
2148
2149static int verify_authorizer_reply(struct ceph_connection *con, int len)
2150{
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;
2154
Alex Eldera2556512012-05-16 15:16:39 -05002155 /*
2156 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2157 * XXX which do we do: succeed or fail?
2158 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002159 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002160}
2161
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002162static int invalidate_authorizer(struct ceph_connection *con)
2163{
2164 struct ceph_osd *o = con->private;
2165 struct ceph_osd_client *osdc = o->o_osdc;
2166 struct ceph_auth_client *ac = osdc->client->monc.auth;
2167
Alex Eldera2556512012-05-16 15:16:39 -05002168 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002169 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2170
2171 return ceph_monc_validate_auth(&osdc->client->monc);
2172}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002173
Tobias Klauser9e327892010-05-20 10:40:19 +02002174static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002175 .get = get_osd_con,
2176 .put = put_osd_con,
2177 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002178 .get_authorizer = get_authorizer,
2179 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002180 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002181 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002182 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002183};