blob: 77ce1edaa07db88800415e83337d956ee955032c [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_has_extent(int op)
36{
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
39}
40
Alex Eldere75b45c2012-11-13 21:11:14 -060041int ceph_calc_raw_layout(struct ceph_file_layout *layout,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070042 u64 off, u64 *plen, u64 *bno,
43 struct ceph_osd_request *req,
44 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070045{
Yehuda Sadeh68b44762010-04-06 15:01:27 -070046 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070047 u64 objoff, objlen; /* extent in object */
Sage Weild63b77f2012-09-24 20:59:48 -070048 int r;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070049
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070050 /* object extent? */
Alex Eldere8afad62012-11-14 09:38:19 -060051 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
Sage Weild63b77f2012-09-24 20:59:48 -070052 &objoff, &objlen);
53 if (r < 0)
54 return r;
Alex Eldere8afad62012-11-14 09:38:19 -060055 if (objlen < orig_len) {
56 *plen = objlen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070057 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070058 orig_len - *plen, off, *plen);
Alex Eldere8afad62012-11-14 09:38:19 -060059 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070060
Yehuda Sadeh68b44762010-04-06 15:01:27 -070061 if (op_has_extent(op->op)) {
Yan, Zhenga41bad12012-11-30 13:49:51 +080062 u32 osize = le32_to_cpu(layout->fl_object_size);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070063 op->extent.offset = objoff;
64 op->extent.length = objlen;
Yan, Zhenga41bad12012-11-30 13:49:51 +080065 if (op->extent.truncate_size <= off - objoff) {
66 op->extent.truncate_size = 0;
67 } else {
68 op->extent.truncate_size -= off - objoff;
69 if (op->extent.truncate_size > osize)
70 op->extent.truncate_size = osize;
71 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -070072 }
73 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080074 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070075 if (op->op == CEPH_OSD_OP_WRITE)
76 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070077
78 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
79 *bno, objoff, objlen, req->r_num_pages);
Sage Weild63b77f2012-09-24 20:59:48 -070080 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070081}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070082EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070083
Sage Weilf24e9982009-10-06 11:31:10 -070084/*
85 * Implement client access to distributed object storage cluster.
86 *
87 * All data objects are stored within a cluster/cloud of OSDs, or
88 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
89 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
90 * remote daemons serving up and coordinating consistent and safe
91 * access to storage.
92 *
93 * Cluster membership and the mapping of data objects onto storage devices
94 * are described by the osd map.
95 *
96 * We keep track of pending OSD requests (read, write), resubmit
97 * requests to different OSDs when the cluster topology/data layout
98 * change, or retry the affected requests when the communications
99 * channel with an OSD is reset.
100 */
101
102/*
103 * calculate the mapping of a file extent onto an object, and fill out the
104 * request accordingly. shorten extent as necessary if it crosses an
105 * object boundary.
106 *
107 * fill osd op in request message.
108 */
Alex Eldere75b45c2012-11-13 21:11:14 -0600109static int calc_layout(struct ceph_vino vino,
Sage Weild63b77f2012-09-24 20:59:48 -0700110 struct ceph_file_layout *layout,
111 u64 off, u64 *plen,
112 struct ceph_osd_request *req,
113 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700114{
Sage Weilf24e9982009-10-06 11:31:10 -0700115 u64 bno;
Sage Weild63b77f2012-09-24 20:59:48 -0700116 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700117
Alex Eldere75b45c2012-11-13 21:11:14 -0600118 r = ceph_calc_raw_layout(layout, off, plen, &bno, req, op);
Sage Weild63b77f2012-09-24 20:59:48 -0700119 if (r < 0)
120 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700121
Sage Weil2dab0362011-05-12 14:29:51 -0700122 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
Sage Weilf24e9982009-10-06 11:31:10 -0700123 req->r_oid_len = strlen(req->r_oid);
Sage Weild63b77f2012-09-24 20:59:48 -0700124
125 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700126}
127
Sage Weilf24e9982009-10-06 11:31:10 -0700128/*
129 * requests
130 */
Sage Weil415e49a2009-12-07 13:37:03 -0800131void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700132{
Sage Weil415e49a2009-12-07 13:37:03 -0800133 struct ceph_osd_request *req = container_of(kref,
134 struct ceph_osd_request,
135 r_kref);
136
137 if (req->r_request)
138 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800139 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -0500140 dout("%s revoking pages %p from con %p\n", __func__,
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800141 req->r_pages, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500142 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700143 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil350b1c32009-12-22 10:45:45 -0800144 }
Alex Elderab8cb342012-06-04 14:43:32 -0500145 if (req->r_reply)
146 ceph_msg_put(req->r_reply);
Sage Weil415e49a2009-12-07 13:37:03 -0800147 if (req->r_own_pages)
148 ceph_release_page_vector(req->r_pages,
149 req->r_num_pages);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700150#ifdef CONFIG_BLOCK
151 if (req->r_bio)
152 bio_put(req->r_bio);
153#endif
Sage Weil415e49a2009-12-07 13:37:03 -0800154 ceph_put_snap_context(req->r_snapc);
Alex Elderc8858372012-11-13 21:11:15 -0600155 ceph_pagelist_release(&req->r_trail);
Sage Weil415e49a2009-12-07 13:37:03 -0800156 if (req->r_mempool)
157 mempool_free(req, req->r_osdc->req_mempool);
158 else
159 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700160}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700161EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700162
Alex Elder5b9d1b12012-11-13 21:11:15 -0600163static int get_num_ops(struct ceph_osd_req_op *ops)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700164{
165 int i = 0;
166
Alex Elder5b9d1b12012-11-13 21:11:15 -0600167 while (ops[i].op)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700168 i++;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700169
170 return i;
171}
172
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700173struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700174 struct ceph_snap_context *snapc,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700175 struct ceph_osd_req_op *ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700176 bool use_mempool,
177 gfp_t gfp_flags,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700178 struct page **pages,
179 struct bio *bio)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700180{
181 struct ceph_osd_request *req;
182 struct ceph_msg *msg;
Alex Elder5b9d1b12012-11-13 21:11:15 -0600183 int num_op = get_num_ops(ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700184 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700185
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700186 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700187
188 if (use_mempool) {
189 req = mempool_alloc(osdc->req_mempool, gfp_flags);
190 memset(req, 0, sizeof(*req));
191 } else {
192 req = kzalloc(sizeof(*req), gfp_flags);
193 }
194 if (req == NULL)
195 return NULL;
196
197 req->r_osdc = osdc;
198 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700199
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700200 kref_init(&req->r_kref);
201 init_completion(&req->r_completion);
202 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600203 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700204 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700205 INIT_LIST_HEAD(&req->r_linger_item);
206 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700207 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700208 INIT_LIST_HEAD(&req->r_osd_item);
209
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700210 /* create reply message */
211 if (use_mempool)
212 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
213 else
214 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700215 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700216 if (!msg) {
217 ceph_osdc_put_request(req);
218 return NULL;
219 }
220 req->r_reply = msg;
221
Alex Elderc8858372012-11-13 21:11:15 -0600222 ceph_pagelist_init(&req->r_trail);
Sage Weild50b4092012-07-09 14:22:34 -0700223
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700224 /* create request message; allow space for oid */
Stratos Psomadakis224736d2011-11-10 15:45:37 +0200225 msg_size += MAX_OBJ_NAME_SIZE;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700226 if (snapc)
227 msg_size += sizeof(u64) * snapc->num_snaps;
228 if (use_mempool)
229 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
230 else
Sage Weilb61c2762011-08-09 15:03:46 -0700231 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700232 if (!msg) {
233 ceph_osdc_put_request(req);
234 return NULL;
235 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700236
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700237 memset(msg->front.iov_base, 0, msg->front.iov_len);
238
239 req->r_request = msg;
240 req->r_pages = pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700241#ifdef CONFIG_BLOCK
242 if (bio) {
243 req->r_bio = bio;
244 bio_get(req->r_bio);
245 }
246#endif
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700247
248 return req;
249}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700250EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700251
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700252static void osd_req_encode_op(struct ceph_osd_request *req,
253 struct ceph_osd_op *dst,
254 struct ceph_osd_req_op *src)
255{
256 dst->op = cpu_to_le16(src->op);
257
Alex Elder065a68f2012-04-20 15:49:43 -0500258 switch (src->op) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700259 case CEPH_OSD_OP_READ:
260 case CEPH_OSD_OP_WRITE:
261 dst->extent.offset =
262 cpu_to_le64(src->extent.offset);
263 dst->extent.length =
264 cpu_to_le64(src->extent.length);
265 dst->extent.truncate_size =
266 cpu_to_le64(src->extent.truncate_size);
267 dst->extent.truncate_seq =
268 cpu_to_le32(src->extent.truncate_seq);
269 break;
270
271 case CEPH_OSD_OP_GETXATTR:
272 case CEPH_OSD_OP_SETXATTR:
273 case CEPH_OSD_OP_CMPXATTR:
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700274 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
275 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
276 dst->xattr.cmp_op = src->xattr.cmp_op;
277 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Alex Elderc8858372012-11-13 21:11:15 -0600278 ceph_pagelist_append(&req->r_trail, src->xattr.name,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700279 src->xattr.name_len);
Alex Elderc8858372012-11-13 21:11:15 -0600280 ceph_pagelist_append(&req->r_trail, src->xattr.val,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700281 src->xattr.value_len);
282 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700283 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700284 dst->cls.class_len = src->cls.class_len;
285 dst->cls.method_len = src->cls.method_len;
286 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
287
Alex Elderc8858372012-11-13 21:11:15 -0600288 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700289 src->cls.class_len);
Alex Elderc8858372012-11-13 21:11:15 -0600290 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700291 src->cls.method_len);
Alex Elderc8858372012-11-13 21:11:15 -0600292 ceph_pagelist_append(&req->r_trail, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700293 src->cls.indata_len);
294 break;
295 case CEPH_OSD_OP_ROLLBACK:
296 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
297 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700298 case CEPH_OSD_OP_STARTSYNC:
299 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700300 case CEPH_OSD_OP_NOTIFY:
301 {
302 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
303 __le32 timeout = cpu_to_le32(src->watch.timeout);
304
Alex Elderc8858372012-11-13 21:11:15 -0600305 ceph_pagelist_append(&req->r_trail,
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700306 &prot_ver, sizeof(prot_ver));
Alex Elderc8858372012-11-13 21:11:15 -0600307 ceph_pagelist_append(&req->r_trail,
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700308 &timeout, sizeof(timeout));
309 }
310 case CEPH_OSD_OP_NOTIFY_ACK:
311 case CEPH_OSD_OP_WATCH:
312 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
313 dst->watch.ver = cpu_to_le64(src->watch.ver);
314 dst->watch.flag = src->watch.flag;
315 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700316 default:
317 pr_err("unrecognized osd opcode %d\n", dst->op);
318 WARN_ON(1);
319 break;
320 }
321 dst->payload_len = cpu_to_le32(src->payload_len);
322}
323
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700324/*
325 * build new request AND message
326 *
327 */
328void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder0120be32012-11-14 09:38:19 -0600329 u64 off, u64 len,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700330 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600331 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600332 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700333{
334 struct ceph_msg *msg = req->r_request;
335 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700336 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700337 struct ceph_osd_op *op;
338 void *p;
Alex Elder5b9d1b12012-11-13 21:11:15 -0600339 int num_op = get_num_ops(src_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700340 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700341 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700342 u64 data_len = 0;
343 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700344
Alex Elderd178a9e2012-11-13 21:11:15 -0600345 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
346
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700347 head = msg->front.iov_base;
Alex Elder4d6b2502012-11-13 21:11:15 -0600348 head->snapid = cpu_to_le64(snap_id);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700349 op = (void *)(head + 1);
350 p = (void *)(op + num_op);
351
352 req->r_snapc = ceph_get_snap_context(snapc);
353
354 head->client_inc = cpu_to_le32(1); /* always, for now. */
355 head->flags = cpu_to_le32(flags);
356 if (flags & CEPH_OSD_FLAG_WRITE)
357 ceph_encode_timespec(&head->mtime, mtime);
358 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700359
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700360
361 /* fill in oid */
Alex Elderaf77f262012-11-09 08:43:15 -0600362 head->object_len = cpu_to_le32(req->r_oid_len);
363 memcpy(p, req->r_oid, req->r_oid_len);
364 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700366 src_op = src_ops;
367 while (src_op->op) {
368 osd_req_encode_op(req, op, src_op);
369 src_op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700370 op++;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700371 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700372
Alex Elderc8858372012-11-13 21:11:15 -0600373 data_len += req->r_trail.length;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700374
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700375 if (snapc) {
376 head->snap_seq = cpu_to_le64(snapc->seq);
377 head->num_snaps = cpu_to_le32(snapc->num_snaps);
378 for (i = 0; i < snapc->num_snaps; i++) {
379 put_unaligned_le64(snapc->snaps[i], p);
380 p += sizeof(u64);
381 }
382 }
383
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700384 if (flags & CEPH_OSD_FLAG_WRITE) {
385 req->r_request->hdr.data_off = cpu_to_le16(off);
Alex Elder0120be32012-11-14 09:38:19 -0600386 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700387 } else if (data_len) {
388 req->r_request->hdr.data_off = 0;
389 req->r_request->hdr.data_len = cpu_to_le32(data_len);
390 }
391
Sage Weilc5c6b192010-11-09 12:40:00 -0800392 req->r_request->page_alignment = req->r_page_alignment;
393
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700394 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
395 msg_size = p - msg->front.iov_base;
396 msg->front.iov_len = msg_size;
397 msg->hdr.front_len = cpu_to_le32(msg_size);
398 return;
399}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700400EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700401
Sage Weilf24e9982009-10-06 11:31:10 -0700402/*
403 * build new request AND message, calculate layout, and adjust file
404 * extent as needed.
405 *
406 * if the file was recently truncated, we include information about its
407 * old and new size so that the object can be updated appropriately. (we
408 * avoid synchronously deleting truncated objects because it's slow.)
409 *
410 * if @do_sync, include a 'startsync' command so that the osd will flush
411 * data quickly.
412 */
413struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
414 struct ceph_file_layout *layout,
415 struct ceph_vino vino,
416 u64 off, u64 *plen,
417 int opcode, int flags,
418 struct ceph_snap_context *snapc,
419 int do_sync,
420 u32 truncate_seq,
421 u64 truncate_size,
422 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800423 bool use_mempool, int num_reply,
424 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700425{
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700426 struct ceph_osd_req_op ops[3];
427 struct ceph_osd_request *req;
Sage Weil68162822012-09-24 21:01:02 -0700428 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700429
430 ops[0].op = opcode;
431 ops[0].extent.truncate_seq = truncate_seq;
432 ops[0].extent.truncate_size = truncate_size;
433 ops[0].payload_len = 0;
434
435 if (do_sync) {
436 ops[1].op = CEPH_OSD_OP_STARTSYNC;
437 ops[1].payload_len = 0;
438 ops[2].op = 0;
439 } else
440 ops[1].op = 0;
441
Alex Elderd178a9e2012-11-13 21:11:15 -0600442 req = ceph_osdc_alloc_request(osdc, snapc, ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700443 use_mempool,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700444 GFP_NOFS, NULL, NULL);
Sage Weil4ad12622011-05-03 09:23:36 -0700445 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700446 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600447 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700448
449 /* calculate max write size */
Alex Eldere75b45c2012-11-13 21:11:14 -0600450 r = calc_layout(vino, layout, off, plen, req, ops);
Sage Weil68162822012-09-24 21:01:02 -0700451 if (r < 0)
452 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700453 req->r_file_layout = *layout; /* keep a copy */
454
Sage Weil9bb0ce22011-06-13 16:20:18 -0700455 /* in case it differs from natural (file) alignment that
456 calc_layout filled in for us */
457 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800458 req->r_page_alignment = page_align;
459
Alex Elder4d6b2502012-11-13 21:11:15 -0600460 ceph_osdc_build_request(req, off, *plen, ops, snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700461
Sage Weilf24e9982009-10-06 11:31:10 -0700462 return req;
463}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700464EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700465
466/*
467 * We keep osd requests in an rbtree, sorted by ->r_tid.
468 */
469static void __insert_request(struct ceph_osd_client *osdc,
470 struct ceph_osd_request *new)
471{
472 struct rb_node **p = &osdc->requests.rb_node;
473 struct rb_node *parent = NULL;
474 struct ceph_osd_request *req = NULL;
475
476 while (*p) {
477 parent = *p;
478 req = rb_entry(parent, struct ceph_osd_request, r_node);
479 if (new->r_tid < req->r_tid)
480 p = &(*p)->rb_left;
481 else if (new->r_tid > req->r_tid)
482 p = &(*p)->rb_right;
483 else
484 BUG();
485 }
486
487 rb_link_node(&new->r_node, parent, p);
488 rb_insert_color(&new->r_node, &osdc->requests);
489}
490
491static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
492 u64 tid)
493{
494 struct ceph_osd_request *req;
495 struct rb_node *n = osdc->requests.rb_node;
496
497 while (n) {
498 req = rb_entry(n, struct ceph_osd_request, r_node);
499 if (tid < req->r_tid)
500 n = n->rb_left;
501 else if (tid > req->r_tid)
502 n = n->rb_right;
503 else
504 return req;
505 }
506 return NULL;
507}
508
509static struct ceph_osd_request *
510__lookup_request_ge(struct ceph_osd_client *osdc,
511 u64 tid)
512{
513 struct ceph_osd_request *req;
514 struct rb_node *n = osdc->requests.rb_node;
515
516 while (n) {
517 req = rb_entry(n, struct ceph_osd_request, r_node);
518 if (tid < req->r_tid) {
519 if (!n->rb_left)
520 return req;
521 n = n->rb_left;
522 } else if (tid > req->r_tid) {
523 n = n->rb_right;
524 } else {
525 return req;
526 }
527 }
528 return NULL;
529}
530
Sage Weil6f6c7002011-01-17 20:34:08 -0800531/*
532 * Resubmit requests pending on the given osd.
533 */
534static void __kick_osd_requests(struct ceph_osd_client *osdc,
535 struct ceph_osd *osd)
536{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700537 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800538 int err;
539
540 dout("__kick_osd_requests osd%d\n", osd->o_osd);
541 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600542 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800543 return;
544
545 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
546 list_move(&req->r_req_lru_item, &osdc->req_unsent);
547 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
548 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700549 if (!req->r_linger)
550 req->r_flags |= CEPH_OSD_FLAG_RETRY;
551 }
552
553 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
554 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700555 /*
556 * reregister request prior to unregistering linger so
557 * that r_osd is preserved.
558 */
559 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700560 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700561 list_add(&req->r_req_lru_item, &osdc->req_unsent);
562 list_add(&req->r_osd_item, &req->r_osd->o_requests);
563 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700564 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
565 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800566 }
567}
568
Sage Weilf24e9982009-10-06 11:31:10 -0700569/*
Sage Weil81b024e2009-10-09 10:29:18 -0700570 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700571 */
572static void osd_reset(struct ceph_connection *con)
573{
574 struct ceph_osd *osd = con->private;
575 struct ceph_osd_client *osdc;
576
577 if (!osd)
578 return;
579 dout("osd_reset osd%d\n", osd->o_osd);
580 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700581 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800582 mutex_lock(&osdc->request_mutex);
583 __kick_osd_requests(osdc, osd);
584 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -0800585 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700586 up_read(&osdc->map_sem);
587}
588
589/*
590 * Track open sessions with osds.
591 */
Alex Eldere10006f2012-05-26 23:26:43 -0500592static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700593{
594 struct ceph_osd *osd;
595
596 osd = kzalloc(sizeof(*osd), GFP_NOFS);
597 if (!osd)
598 return NULL;
599
600 atomic_set(&osd->o_ref, 1);
601 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500602 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600603 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700604 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700605 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800606 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700607 osd->o_incarnation = 1;
608
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700609 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800610
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800611 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700612 return osd;
613}
614
615static struct ceph_osd *get_osd(struct ceph_osd *osd)
616{
617 if (atomic_inc_not_zero(&osd->o_ref)) {
618 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
619 atomic_read(&osd->o_ref));
620 return osd;
621 } else {
622 dout("get_osd %p FAIL\n", osd);
623 return NULL;
624 }
625}
626
627static void put_osd(struct ceph_osd *osd)
628{
629 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
630 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500631 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700632 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
633
Alex Eldera2556512012-05-16 15:16:39 -0500634 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500635 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700636 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700637 }
Sage Weilf24e9982009-10-06 11:31:10 -0700638}
639
640/*
641 * remove an osd from our map
642 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800643static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700644{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800645 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700646 BUG_ON(!list_empty(&osd->o_requests));
647 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800648 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700649 ceph_con_close(&osd->o_con);
650 put_osd(osd);
651}
652
Sage Weilaca420b2011-08-31 14:45:53 -0700653static void remove_all_osds(struct ceph_osd_client *osdc)
654{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500655 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700656 mutex_lock(&osdc->request_mutex);
657 while (!RB_EMPTY_ROOT(&osdc->osds)) {
658 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
659 struct ceph_osd, o_node);
660 __remove_osd(osdc, osd);
661 }
662 mutex_unlock(&osdc->request_mutex);
663}
664
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800665static void __move_osd_to_lru(struct ceph_osd_client *osdc,
666 struct ceph_osd *osd)
667{
668 dout("__move_osd_to_lru %p\n", osd);
669 BUG_ON(!list_empty(&osd->o_osd_lru));
670 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700671 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800672}
673
674static void __remove_osd_from_lru(struct ceph_osd *osd)
675{
676 dout("__remove_osd_from_lru %p\n", osd);
677 if (!list_empty(&osd->o_osd_lru))
678 list_del_init(&osd->o_osd_lru);
679}
680
Sage Weilaca420b2011-08-31 14:45:53 -0700681static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800682{
683 struct ceph_osd *osd, *nosd;
684
685 dout("__remove_old_osds %p\n", osdc);
686 mutex_lock(&osdc->request_mutex);
687 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700688 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800689 break;
690 __remove_osd(osdc, osd);
691 }
692 mutex_unlock(&osdc->request_mutex);
693}
694
Sage Weilf24e9982009-10-06 11:31:10 -0700695/*
696 * reset osd connect
697 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800698static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700699{
Alex Elderc3acb182012-12-07 09:57:58 -0600700 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700701
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800702 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700703 if (list_empty(&osd->o_requests) &&
704 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800705 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600706
707 return -ENODEV;
708 }
709
710 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
711 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
712 !ceph_con_opened(&osd->o_con)) {
713 struct ceph_osd_request *req;
714
Sage Weil87b315a2010-03-22 14:51:18 -0700715 dout(" osd addr hasn't changed and connection never opened,"
716 " letting msgr retry");
717 /* touch each r_stamp for handle_timeout()'s benfit */
718 list_for_each_entry(req, &osd->o_requests, r_osd_item)
719 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600720
721 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700722 }
Alex Elderc3acb182012-12-07 09:57:58 -0600723
724 ceph_con_close(&osd->o_con);
725 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
726 osd->o_incarnation++;
727
728 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700729}
730
731static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
732{
733 struct rb_node **p = &osdc->osds.rb_node;
734 struct rb_node *parent = NULL;
735 struct ceph_osd *osd = NULL;
736
Sage Weilaca420b2011-08-31 14:45:53 -0700737 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700738 while (*p) {
739 parent = *p;
740 osd = rb_entry(parent, struct ceph_osd, o_node);
741 if (new->o_osd < osd->o_osd)
742 p = &(*p)->rb_left;
743 else if (new->o_osd > osd->o_osd)
744 p = &(*p)->rb_right;
745 else
746 BUG();
747 }
748
749 rb_link_node(&new->o_node, parent, p);
750 rb_insert_color(&new->o_node, &osdc->osds);
751}
752
753static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
754{
755 struct ceph_osd *osd;
756 struct rb_node *n = osdc->osds.rb_node;
757
758 while (n) {
759 osd = rb_entry(n, struct ceph_osd, o_node);
760 if (o < osd->o_osd)
761 n = n->rb_left;
762 else if (o > osd->o_osd)
763 n = n->rb_right;
764 else
765 return osd;
766 }
767 return NULL;
768}
769
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800770static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
771{
772 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700773 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800774}
775
776static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
777{
778 cancel_delayed_work(&osdc->timeout_work);
779}
Sage Weilf24e9982009-10-06 11:31:10 -0700780
781/*
782 * Register request, assign tid. If this is the first request, set up
783 * the timeout event.
784 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700785static void __register_request(struct ceph_osd_client *osdc,
786 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700787{
Sage Weilf24e9982009-10-06 11:31:10 -0700788 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800789 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700790 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700791 __insert_request(osdc, req);
792 ceph_osdc_get_request(req);
793 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700794 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800795 dout(" first request, scheduling timeout\n");
796 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700797 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700798}
799
800static void register_request(struct ceph_osd_client *osdc,
801 struct ceph_osd_request *req)
802{
803 mutex_lock(&osdc->request_mutex);
804 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700805 mutex_unlock(&osdc->request_mutex);
806}
807
808/*
809 * called under osdc->request_mutex
810 */
811static void __unregister_request(struct ceph_osd_client *osdc,
812 struct ceph_osd_request *req)
813{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500814 if (RB_EMPTY_NODE(&req->r_node)) {
815 dout("__unregister_request %p tid %lld not registered\n",
816 req, req->r_tid);
817 return;
818 }
819
Sage Weilf24e9982009-10-06 11:31:10 -0700820 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
821 rb_erase(&req->r_node, &osdc->requests);
822 osdc->num_requests--;
823
Sage Weil0ba64782009-10-08 16:57:16 -0700824 if (req->r_osd) {
825 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500826 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700827
828 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700829 if (list_empty(&req->r_osd->o_requests) &&
830 list_empty(&req->r_osd->o_linger_requests)) {
831 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800832 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700833 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700834 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700835 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700836 }
Sage Weilf24e9982009-10-06 11:31:10 -0700837
Alex Elder7d5f2482012-11-29 08:37:03 -0600838 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700839 ceph_osdc_put_request(req);
840
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800841 if (osdc->num_requests == 0) {
842 dout(" no requests, canceling timeout\n");
843 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700844 }
845}
846
847/*
848 * Cancel a previously queued request message
849 */
850static void __cancel_request(struct ceph_osd_request *req)
851{
Sage Weil6bc18872010-09-27 10:18:52 -0700852 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500853 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700854 req->r_sent = 0;
855 }
856}
857
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700858static void __register_linger_request(struct ceph_osd_client *osdc,
859 struct ceph_osd_request *req)
860{
861 dout("__register_linger_request %p\n", req);
862 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700863 if (req->r_osd)
864 list_add_tail(&req->r_linger_osd,
865 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700866}
867
868static void __unregister_linger_request(struct ceph_osd_client *osdc,
869 struct ceph_osd_request *req)
870{
871 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -0600872 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700873 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700874 list_del_init(&req->r_linger_osd);
875
876 if (list_empty(&req->r_osd->o_requests) &&
877 list_empty(&req->r_osd->o_linger_requests)) {
878 dout("moving osd to %p lru\n", req->r_osd);
879 __move_osd_to_lru(osdc, req->r_osd);
880 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700881 if (list_empty(&req->r_osd_item))
882 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700883 }
884}
885
886void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
887 struct ceph_osd_request *req)
888{
889 mutex_lock(&osdc->request_mutex);
890 if (req->r_linger) {
891 __unregister_linger_request(osdc, req);
892 ceph_osdc_put_request(req);
893 }
894 mutex_unlock(&osdc->request_mutex);
895}
896EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
897
898void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
899 struct ceph_osd_request *req)
900{
901 if (!req->r_linger) {
902 dout("set_request_linger %p\n", req);
903 req->r_linger = 1;
904 /*
905 * caller is now responsible for calling
906 * unregister_linger_request
907 */
908 ceph_osdc_get_request(req);
909 }
910}
911EXPORT_SYMBOL(ceph_osdc_set_request_linger);
912
Sage Weilf24e9982009-10-06 11:31:10 -0700913/*
914 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
915 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300916 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800917 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700918 *
919 * Return 0 if unchanged, 1 if changed, or negative on error.
920 *
921 * Caller should hold map_sem for read and request_mutex.
922 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800923static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700924 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700925{
926 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800927 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700928 int acting[CEPH_PG_MAX_SIZE];
929 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700930 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700931
Sage Weil6f6c7002011-01-17 20:34:08 -0800932 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700933 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
934 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800935 if (err) {
936 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700937 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800938 }
Sage Weil51042122009-11-04 11:39:12 -0800939 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800940 req->r_pgid = pgid;
941
Sage Weild85b7052010-05-10 10:24:48 -0700942 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
943 if (err > 0) {
944 o = acting[0];
945 num = err;
946 }
Sage Weilf24e9982009-10-06 11:31:10 -0700947
Sage Weil38d64532011-10-14 13:33:55 -0700948 if ((!force_resend &&
949 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700950 req->r_sent >= req->r_osd->o_incarnation &&
951 req->r_num_pg_osds == num &&
952 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700953 (req->r_osd == NULL && o == -1))
954 return 0; /* no change */
955
Sage Weil6f6c7002011-01-17 20:34:08 -0800956 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800957 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700958 req->r_osd ? req->r_osd->o_osd : -1);
959
Sage Weild85b7052010-05-10 10:24:48 -0700960 /* record full pg acting set */
961 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
962 req->r_num_pg_osds = num;
963
Sage Weilf24e9982009-10-06 11:31:10 -0700964 if (req->r_osd) {
965 __cancel_request(req);
966 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700967 req->r_osd = NULL;
968 }
969
970 req->r_osd = __lookup_osd(osdc, o);
971 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800972 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -0500973 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -0800974 if (!req->r_osd) {
975 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800976 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -0800977 }
Sage Weilf24e9982009-10-06 11:31:10 -0700978
Sage Weil6f6c7002011-01-17 20:34:08 -0800979 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -0700980 __insert_osd(osdc, req->r_osd);
981
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700982 ceph_con_open(&req->r_osd->o_con,
983 CEPH_ENTITY_TYPE_OSD, o,
984 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -0700985 }
986
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800987 if (req->r_osd) {
988 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700989 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -0800990 list_move(&req->r_req_lru_item, &osdc->req_unsent);
991 } else {
992 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800993 }
Sage Weild85b7052010-05-10 10:24:48 -0700994 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -0700995
996out:
Sage Weilf24e9982009-10-06 11:31:10 -0700997 return err;
998}
999
1000/*
1001 * caller should hold map_sem (for read) and request_mutex
1002 */
Sage Weil56e925b2012-01-03 12:34:34 -08001003static void __send_request(struct ceph_osd_client *osdc,
1004 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001005{
1006 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -07001007
1008 dout("send_request %p tid %llu to osd%d flags %d\n",
1009 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
1010
1011 reqhead = req->r_request->front.iov_base;
1012 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
1013 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
1014 reqhead->reassert_version = req->r_reassert_version;
1015
Sage Weil3dd72fc2010-03-22 14:42:30 -07001016 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001017 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001018
1019 ceph_msg_get(req->r_request); /* send consumes a ref */
1020 ceph_con_send(&req->r_osd->o_con, req->r_request);
1021 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001022}
1023
1024/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001025 * Send any requests in the queue (req_unsent).
1026 */
1027static void send_queued(struct ceph_osd_client *osdc)
1028{
1029 struct ceph_osd_request *req, *tmp;
1030
1031 dout("send_queued\n");
1032 mutex_lock(&osdc->request_mutex);
1033 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1034 __send_request(osdc, req);
1035 }
1036 mutex_unlock(&osdc->request_mutex);
1037}
1038
1039/*
Sage Weilf24e9982009-10-06 11:31:10 -07001040 * Timeout callback, called every N seconds when 1 or more osd
1041 * requests has been active for more than N seconds. When this
1042 * happens, we ping all OSDs with requests who have timed out to
1043 * ensure any communications channel reset is detected. Reset the
1044 * request timeouts another N seconds in the future as we go.
1045 * Reschedule the timeout event another N seconds in future (unless
1046 * there are no open requests).
1047 */
1048static void handle_timeout(struct work_struct *work)
1049{
1050 struct ceph_osd_client *osdc =
1051 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001052 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001053 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001054 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001055 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001056 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001057 dout("timeout\n");
1058 down_read(&osdc->map_sem);
1059
1060 ceph_monc_request_next_osdmap(&osdc->client->monc);
1061
1062 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001063
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001064 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001065 * ping osds that are a bit slow. this ensures that if there
1066 * is a break in the TCP connection we will notice, and reopen
1067 * a connection with that osd (from the fault callback).
1068 */
1069 INIT_LIST_HEAD(&slow_osds);
1070 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001071 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001072 break;
1073
1074 osd = req->r_osd;
1075 BUG_ON(!osd);
1076 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001077 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001078 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1079 }
1080 while (!list_empty(&slow_osds)) {
1081 osd = list_entry(slow_osds.next, struct ceph_osd,
1082 o_keepalive_item);
1083 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001084 ceph_con_keepalive(&osd->o_con);
1085 }
1086
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001087 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001088 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001089 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001090 up_read(&osdc->map_sem);
1091}
1092
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001093static void handle_osds_timeout(struct work_struct *work)
1094{
1095 struct ceph_osd_client *osdc =
1096 container_of(work, struct ceph_osd_client,
1097 osds_timeout_work.work);
1098 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001099 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001100
1101 dout("osds timeout\n");
1102 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001103 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001104 up_read(&osdc->map_sem);
1105
1106 schedule_delayed_work(&osdc->osds_timeout_work,
1107 round_jiffies_relative(delay));
1108}
1109
Sage Weil25845472011-06-03 09:37:09 -07001110static void complete_request(struct ceph_osd_request *req)
1111{
1112 if (req->r_safe_callback)
1113 req->r_safe_callback(req, NULL);
1114 complete_all(&req->r_safe_completion); /* fsync waiter */
1115}
1116
Sage Weilf24e9982009-10-06 11:31:10 -07001117/*
1118 * handle osd op reply. either call the callback if it is specified,
1119 * or do the completion to wake up the waiting thread.
1120 */
Sage Weil350b1c32009-12-22 10:45:45 -08001121static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1122 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001123{
1124 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1125 struct ceph_osd_request *req;
1126 u64 tid;
1127 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001128 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001129
Sage Weil6df058c2009-12-22 11:24:33 -08001130 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001131 if (msg->front.iov_len < sizeof(*rhead))
1132 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001133 numops = le32_to_cpu(rhead->num_ops);
1134 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001135 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001136 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1137 numops * sizeof(struct ceph_osd_op))
1138 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001139 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001140 /* lookup */
1141 mutex_lock(&osdc->request_mutex);
1142 req = __lookup_request(osdc, tid);
1143 if (req == NULL) {
1144 dout("handle_reply tid %llu dne\n", tid);
1145 mutex_unlock(&osdc->request_mutex);
1146 return;
1147 }
1148 ceph_osdc_get_request(req);
1149 flags = le32_to_cpu(rhead->flags);
1150
Sage Weil350b1c32009-12-22 10:45:45 -08001151 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001152 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001153 * avoid a (safe but slower) revoke later.
1154 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001155 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001156 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001157 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001158 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001159 }
1160
Sage Weilf24e9982009-10-06 11:31:10 -07001161 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001162 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001163
1164 req->r_result = le32_to_cpu(rhead->result);
1165 bytes = le32_to_cpu(msg->hdr.data_len);
1166 dout("handle_reply result %d bytes %d\n", req->r_result,
1167 bytes);
1168 if (req->r_result == 0)
1169 req->r_result = bytes;
1170
1171 /* in case this is a write and we need to replay, */
1172 req->r_reassert_version = rhead->reassert_version;
1173
1174 req->r_got_reply = 1;
1175 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1176 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001177 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001178 goto done;
1179 }
1180
1181 dout("handle_reply tid %llu flags %d\n", tid, flags);
1182
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001183 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1184 __register_linger_request(osdc, req);
1185
Sage Weilf24e9982009-10-06 11:31:10 -07001186 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001187 if (result < 0 ||
1188 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001189 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1190 __unregister_request(osdc, req);
1191
1192 mutex_unlock(&osdc->request_mutex);
1193
1194 if (req->r_callback)
1195 req->r_callback(req, msg);
1196 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001197 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001198
Sage Weil25845472011-06-03 09:37:09 -07001199 if (flags & CEPH_OSD_FLAG_ONDISK)
1200 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001201
1202done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001203 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001204 ceph_osdc_put_request(req);
1205 return;
1206
1207bad:
1208 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1209 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1210 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001211 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001212}
1213
Sage Weil6f6c7002011-01-17 20:34:08 -08001214static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001215{
Sage Weilf24e9982009-10-06 11:31:10 -07001216 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001217
Sage Weil6f6c7002011-01-17 20:34:08 -08001218 for (p = rb_first(&osdc->osds); p; p = n) {
1219 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001220
Sage Weil6f6c7002011-01-17 20:34:08 -08001221 n = rb_next(p);
1222 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1223 memcmp(&osd->o_con.peer_addr,
1224 ceph_osd_addr(osdc->osdmap,
1225 osd->o_osd),
1226 sizeof(struct ceph_entity_addr)) != 0)
1227 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001228 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001229}
1230
1231/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001232 * Requeue requests whose mapping to an OSD has changed. If requests map to
1233 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001234 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001235 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001236 */
Sage Weil38d64532011-10-14 13:33:55 -07001237static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001238{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001239 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001240 struct rb_node *p;
1241 int needmap = 0;
1242 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001243
Sage Weil38d64532011-10-14 13:33:55 -07001244 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001245 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001246 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001247 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001248 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001249
1250 /*
1251 * For linger requests that have not yet been
1252 * registered, move them to the linger list; they'll
1253 * be sent to the osd in the loop below. Unregister
1254 * the request before re-registering it as a linger
1255 * request to ensure the __map_request() below
1256 * will decide it needs to be sent.
1257 */
1258 if (req->r_linger && list_empty(&req->r_linger_item)) {
1259 dout("%p tid %llu restart on osd%d\n",
1260 req, req->r_tid,
1261 req->r_osd ? req->r_osd->o_osd : -1);
1262 __unregister_request(osdc, req);
1263 __register_linger_request(osdc, req);
1264 continue;
1265 }
1266
Sage Weil38d64532011-10-14 13:33:55 -07001267 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001268 if (err < 0)
1269 continue; /* error */
1270 if (req->r_osd == NULL) {
1271 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1272 needmap++; /* request a newer map */
1273 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001274 if (!req->r_linger) {
1275 dout("%p tid %llu requeued on osd%d\n", req,
1276 req->r_tid,
1277 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001278 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001279 }
1280 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001281 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001282
1283 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1284 r_linger_item) {
1285 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1286
Sage Weil38d64532011-10-14 13:33:55 -07001287 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001288 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001289 if (err == 0)
1290 continue; /* no change and no osd was specified */
1291 if (err < 0)
1292 continue; /* hrm! */
1293 if (req->r_osd == NULL) {
1294 dout("tid %llu maps to no valid osd\n", req->r_tid);
1295 needmap++; /* request a newer map */
1296 continue;
1297 }
1298
1299 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1300 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001301 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001302 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001303 }
Sage Weilf24e9982009-10-06 11:31:10 -07001304 mutex_unlock(&osdc->request_mutex);
1305
1306 if (needmap) {
1307 dout("%d requests for down osds, need new map\n", needmap);
1308 ceph_monc_request_next_osdmap(&osdc->client->monc);
1309 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001310 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001311}
Sage Weil6f6c7002011-01-17 20:34:08 -08001312
1313
Sage Weilf24e9982009-10-06 11:31:10 -07001314/*
1315 * Process updated osd map.
1316 *
1317 * The message contains any number of incremental and full maps, normally
1318 * indicating some sort of topology change in the cluster. Kick requests
1319 * off to different OSDs as needed.
1320 */
1321void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1322{
1323 void *p, *end, *next;
1324 u32 nr_maps, maplen;
1325 u32 epoch;
1326 struct ceph_osdmap *newmap = NULL, *oldmap;
1327 int err;
1328 struct ceph_fsid fsid;
1329
1330 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1331 p = msg->front.iov_base;
1332 end = p + msg->front.iov_len;
1333
1334 /* verify fsid */
1335 ceph_decode_need(&p, end, sizeof(fsid), bad);
1336 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001337 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1338 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001339
1340 down_write(&osdc->map_sem);
1341
1342 /* incremental maps */
1343 ceph_decode_32_safe(&p, end, nr_maps, bad);
1344 dout(" %d inc maps\n", nr_maps);
1345 while (nr_maps > 0) {
1346 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001347 epoch = ceph_decode_32(&p);
1348 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001349 ceph_decode_need(&p, end, maplen, bad);
1350 next = p + maplen;
1351 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1352 dout("applying incremental map %u len %d\n",
1353 epoch, maplen);
1354 newmap = osdmap_apply_incremental(&p, next,
1355 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001356 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001357 if (IS_ERR(newmap)) {
1358 err = PTR_ERR(newmap);
1359 goto bad;
1360 }
Sage Weil30dc6382009-12-21 14:49:37 -08001361 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001362 if (newmap != osdc->osdmap) {
1363 ceph_osdmap_destroy(osdc->osdmap);
1364 osdc->osdmap = newmap;
1365 }
Sage Weil38d64532011-10-14 13:33:55 -07001366 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001367 } else {
1368 dout("ignoring incremental map %u len %d\n",
1369 epoch, maplen);
1370 }
1371 p = next;
1372 nr_maps--;
1373 }
1374 if (newmap)
1375 goto done;
1376
1377 /* full maps */
1378 ceph_decode_32_safe(&p, end, nr_maps, bad);
1379 dout(" %d full maps\n", nr_maps);
1380 while (nr_maps) {
1381 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001382 epoch = ceph_decode_32(&p);
1383 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001384 ceph_decode_need(&p, end, maplen, bad);
1385 if (nr_maps > 1) {
1386 dout("skipping non-latest full map %u len %d\n",
1387 epoch, maplen);
1388 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1389 dout("skipping full map %u len %d, "
1390 "older than our %u\n", epoch, maplen,
1391 osdc->osdmap->epoch);
1392 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001393 int skipped_map = 0;
1394
Sage Weilf24e9982009-10-06 11:31:10 -07001395 dout("taking full map %u len %d\n", epoch, maplen);
1396 newmap = osdmap_decode(&p, p+maplen);
1397 if (IS_ERR(newmap)) {
1398 err = PTR_ERR(newmap);
1399 goto bad;
1400 }
Sage Weil30dc6382009-12-21 14:49:37 -08001401 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001402 oldmap = osdc->osdmap;
1403 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001404 if (oldmap) {
1405 if (oldmap->epoch + 1 < newmap->epoch)
1406 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001407 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001408 }
1409 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001410 }
1411 p += maplen;
1412 nr_maps--;
1413 }
1414
1415done:
1416 downgrade_write(&osdc->map_sem);
1417 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001418
1419 /*
1420 * subscribe to subsequent osdmap updates if full to ensure
1421 * we find out when we are no longer full and stop returning
1422 * ENOSPC.
1423 */
1424 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1425 ceph_monc_request_next_osdmap(&osdc->client->monc);
1426
Sage Weil6f6c7002011-01-17 20:34:08 -08001427 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001428 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001429 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001430 return;
1431
1432bad:
1433 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001434 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001435 up_write(&osdc->map_sem);
1436 return;
1437}
1438
Sage Weilf24e9982009-10-06 11:31:10 -07001439/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001440 * watch/notify callback event infrastructure
1441 *
1442 * These callbacks are used both for watch and notify operations.
1443 */
1444static void __release_event(struct kref *kref)
1445{
1446 struct ceph_osd_event *event =
1447 container_of(kref, struct ceph_osd_event, kref);
1448
1449 dout("__release_event %p\n", event);
1450 kfree(event);
1451}
1452
1453static void get_event(struct ceph_osd_event *event)
1454{
1455 kref_get(&event->kref);
1456}
1457
1458void ceph_osdc_put_event(struct ceph_osd_event *event)
1459{
1460 kref_put(&event->kref, __release_event);
1461}
1462EXPORT_SYMBOL(ceph_osdc_put_event);
1463
1464static void __insert_event(struct ceph_osd_client *osdc,
1465 struct ceph_osd_event *new)
1466{
1467 struct rb_node **p = &osdc->event_tree.rb_node;
1468 struct rb_node *parent = NULL;
1469 struct ceph_osd_event *event = NULL;
1470
1471 while (*p) {
1472 parent = *p;
1473 event = rb_entry(parent, struct ceph_osd_event, node);
1474 if (new->cookie < event->cookie)
1475 p = &(*p)->rb_left;
1476 else if (new->cookie > event->cookie)
1477 p = &(*p)->rb_right;
1478 else
1479 BUG();
1480 }
1481
1482 rb_link_node(&new->node, parent, p);
1483 rb_insert_color(&new->node, &osdc->event_tree);
1484}
1485
1486static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1487 u64 cookie)
1488{
1489 struct rb_node **p = &osdc->event_tree.rb_node;
1490 struct rb_node *parent = NULL;
1491 struct ceph_osd_event *event = NULL;
1492
1493 while (*p) {
1494 parent = *p;
1495 event = rb_entry(parent, struct ceph_osd_event, node);
1496 if (cookie < event->cookie)
1497 p = &(*p)->rb_left;
1498 else if (cookie > event->cookie)
1499 p = &(*p)->rb_right;
1500 else
1501 return event;
1502 }
1503 return NULL;
1504}
1505
1506static void __remove_event(struct ceph_osd_event *event)
1507{
1508 struct ceph_osd_client *osdc = event->osdc;
1509
1510 if (!RB_EMPTY_NODE(&event->node)) {
1511 dout("__remove_event removed %p\n", event);
1512 rb_erase(&event->node, &osdc->event_tree);
1513 ceph_osdc_put_event(event);
1514 } else {
1515 dout("__remove_event didn't remove %p\n", event);
1516 }
1517}
1518
1519int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1520 void (*event_cb)(u64, u64, u8, void *),
1521 int one_shot, void *data,
1522 struct ceph_osd_event **pevent)
1523{
1524 struct ceph_osd_event *event;
1525
1526 event = kmalloc(sizeof(*event), GFP_NOIO);
1527 if (!event)
1528 return -ENOMEM;
1529
1530 dout("create_event %p\n", event);
1531 event->cb = event_cb;
1532 event->one_shot = one_shot;
1533 event->data = data;
1534 event->osdc = osdc;
1535 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001536 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001537 kref_init(&event->kref); /* one ref for us */
1538 kref_get(&event->kref); /* one ref for the caller */
1539 init_completion(&event->completion);
1540
1541 spin_lock(&osdc->event_lock);
1542 event->cookie = ++osdc->event_count;
1543 __insert_event(osdc, event);
1544 spin_unlock(&osdc->event_lock);
1545
1546 *pevent = event;
1547 return 0;
1548}
1549EXPORT_SYMBOL(ceph_osdc_create_event);
1550
1551void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1552{
1553 struct ceph_osd_client *osdc = event->osdc;
1554
1555 dout("cancel_event %p\n", event);
1556 spin_lock(&osdc->event_lock);
1557 __remove_event(event);
1558 spin_unlock(&osdc->event_lock);
1559 ceph_osdc_put_event(event); /* caller's */
1560}
1561EXPORT_SYMBOL(ceph_osdc_cancel_event);
1562
1563
1564static void do_event_work(struct work_struct *work)
1565{
1566 struct ceph_osd_event_work *event_work =
1567 container_of(work, struct ceph_osd_event_work, work);
1568 struct ceph_osd_event *event = event_work->event;
1569 u64 ver = event_work->ver;
1570 u64 notify_id = event_work->notify_id;
1571 u8 opcode = event_work->opcode;
1572
1573 dout("do_event_work completing %p\n", event);
1574 event->cb(ver, notify_id, opcode, event->data);
1575 complete(&event->completion);
1576 dout("do_event_work completed %p\n", event);
1577 ceph_osdc_put_event(event);
1578 kfree(event_work);
1579}
1580
1581
1582/*
1583 * Process osd watch notifications
1584 */
1585void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1586{
1587 void *p, *end;
1588 u8 proto_ver;
1589 u64 cookie, ver, notify_id;
1590 u8 opcode;
1591 struct ceph_osd_event *event;
1592 struct ceph_osd_event_work *event_work;
1593
1594 p = msg->front.iov_base;
1595 end = p + msg->front.iov_len;
1596
1597 ceph_decode_8_safe(&p, end, proto_ver, bad);
1598 ceph_decode_8_safe(&p, end, opcode, bad);
1599 ceph_decode_64_safe(&p, end, cookie, bad);
1600 ceph_decode_64_safe(&p, end, ver, bad);
1601 ceph_decode_64_safe(&p, end, notify_id, bad);
1602
1603 spin_lock(&osdc->event_lock);
1604 event = __find_event(osdc, cookie);
1605 if (event) {
1606 get_event(event);
1607 if (event->one_shot)
1608 __remove_event(event);
1609 }
1610 spin_unlock(&osdc->event_lock);
1611 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1612 cookie, ver, event);
1613 if (event) {
1614 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001615 if (!event_work) {
1616 dout("ERROR: could not allocate event_work\n");
1617 goto done_err;
1618 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001619 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001620 event_work->event = event;
1621 event_work->ver = ver;
1622 event_work->notify_id = notify_id;
1623 event_work->opcode = opcode;
1624 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1625 dout("WARNING: failed to queue notify event work\n");
1626 goto done_err;
1627 }
1628 }
1629
1630 return;
1631
1632done_err:
1633 complete(&event->completion);
1634 ceph_osdc_put_event(event);
1635 return;
1636
1637bad:
1638 pr_err("osdc handle_watch_notify corrupt msg\n");
1639 return;
1640}
1641
1642int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1643{
1644 int err;
1645
1646 dout("wait_event %p\n", event);
1647 err = wait_for_completion_interruptible_timeout(&event->completion,
1648 timeout * HZ);
1649 ceph_osdc_put_event(event);
1650 if (err > 0)
1651 err = 0;
1652 dout("wait_event %p returns %d\n", event, err);
1653 return err;
1654}
1655EXPORT_SYMBOL(ceph_osdc_wait_event);
1656
1657/*
Sage Weilf24e9982009-10-06 11:31:10 -07001658 * Register request, send initial attempt.
1659 */
1660int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1661 struct ceph_osd_request *req,
1662 bool nofail)
1663{
Sage Weilc1ea8822009-10-08 16:55:47 -07001664 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001665
1666 req->r_request->pages = req->r_pages;
1667 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001668#ifdef CONFIG_BLOCK
1669 req->r_request->bio = req->r_bio;
1670#endif
Alex Elderc8858372012-11-13 21:11:15 -06001671 req->r_request->trail = &req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001672
1673 register_request(osdc, req);
1674
1675 down_read(&osdc->map_sem);
1676 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001677 /*
1678 * a racing kick_requests() may have sent the message for us
1679 * while we dropped request_mutex above, so only send now if
1680 * the request still han't been touched yet.
1681 */
1682 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001683 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001684 if (rc < 0) {
1685 if (nofail) {
1686 dout("osdc_start_request failed map, "
1687 " will retry %lld\n", req->r_tid);
1688 rc = 0;
1689 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001690 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001691 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001692 if (req->r_osd == NULL) {
1693 dout("send_request %p no up osds in pg\n", req);
1694 ceph_monc_request_next_osdmap(&osdc->client->monc);
1695 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001696 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001697 }
Sage Weil56e925b2012-01-03 12:34:34 -08001698 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001699 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001700
1701out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001702 mutex_unlock(&osdc->request_mutex);
1703 up_read(&osdc->map_sem);
1704 return rc;
1705}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001706EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001707
1708/*
1709 * wait for a request to complete
1710 */
1711int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1712 struct ceph_osd_request *req)
1713{
1714 int rc;
1715
1716 rc = wait_for_completion_interruptible(&req->r_completion);
1717 if (rc < 0) {
1718 mutex_lock(&osdc->request_mutex);
1719 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001720 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001721 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001722 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001723 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001724 return rc;
1725 }
1726
1727 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1728 return req->r_result;
1729}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001730EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001731
1732/*
1733 * sync - wait for all in-flight requests to flush. avoid starvation.
1734 */
1735void ceph_osdc_sync(struct ceph_osd_client *osdc)
1736{
1737 struct ceph_osd_request *req;
1738 u64 last_tid, next_tid = 0;
1739
1740 mutex_lock(&osdc->request_mutex);
1741 last_tid = osdc->last_tid;
1742 while (1) {
1743 req = __lookup_request_ge(osdc, next_tid);
1744 if (!req)
1745 break;
1746 if (req->r_tid > last_tid)
1747 break;
1748
1749 next_tid = req->r_tid + 1;
1750 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1751 continue;
1752
1753 ceph_osdc_get_request(req);
1754 mutex_unlock(&osdc->request_mutex);
1755 dout("sync waiting on tid %llu (last is %llu)\n",
1756 req->r_tid, last_tid);
1757 wait_for_completion(&req->r_safe_completion);
1758 mutex_lock(&osdc->request_mutex);
1759 ceph_osdc_put_request(req);
1760 }
1761 mutex_unlock(&osdc->request_mutex);
1762 dout("sync done (thru tid %llu)\n", last_tid);
1763}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001764EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001765
1766/*
1767 * init, shutdown
1768 */
1769int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1770{
1771 int err;
1772
1773 dout("init\n");
1774 osdc->client = client;
1775 osdc->osdmap = NULL;
1776 init_rwsem(&osdc->map_sem);
1777 init_completion(&osdc->map_waiters);
1778 osdc->last_requested_map = 0;
1779 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001780 osdc->last_tid = 0;
1781 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001782 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001783 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001784 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001785 INIT_LIST_HEAD(&osdc->req_unsent);
1786 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001787 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001788 osdc->num_requests = 0;
1789 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001790 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001791 spin_lock_init(&osdc->event_lock);
1792 osdc->event_tree = RB_ROOT;
1793 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001794
1795 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001796 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001797
Sage Weil5f44f142009-11-18 14:52:18 -08001798 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001799 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1800 sizeof(struct ceph_osd_request));
1801 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001802 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001803
Sage Weild50b4092012-07-09 14:22:34 -07001804 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1805 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001806 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001807 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001808 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001809 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001810 OSD_OPREPLY_FRONT_LEN, 10, true,
1811 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001812 if (err < 0)
1813 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001814
1815 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1816 if (IS_ERR(osdc->notify_wq)) {
1817 err = PTR_ERR(osdc->notify_wq);
1818 osdc->notify_wq = NULL;
1819 goto out_msgpool;
1820 }
Sage Weilf24e9982009-10-06 11:31:10 -07001821 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001822
Sage Weilc16e7862010-03-01 13:02:00 -08001823out_msgpool:
1824 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001825out_mempool:
1826 mempool_destroy(osdc->req_mempool);
1827out:
1828 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001829}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001830EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001831
1832void ceph_osdc_stop(struct ceph_osd_client *osdc)
1833{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001834 flush_workqueue(osdc->notify_wq);
1835 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001836 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001837 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001838 if (osdc->osdmap) {
1839 ceph_osdmap_destroy(osdc->osdmap);
1840 osdc->osdmap = NULL;
1841 }
Sage Weilaca420b2011-08-31 14:45:53 -07001842 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001843 mempool_destroy(osdc->req_mempool);
1844 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001845 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001846}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001847EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001848
1849/*
1850 * Read some contiguous pages. If we cross a stripe boundary, shorten
1851 * *plen. Return number of bytes read, or error.
1852 */
1853int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1854 struct ceph_vino vino, struct ceph_file_layout *layout,
1855 u64 off, u64 *plen,
1856 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001857 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001858{
1859 struct ceph_osd_request *req;
1860 int rc = 0;
1861
1862 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1863 vino.snap, off, *plen);
1864 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1865 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1866 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001867 false, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001868 if (IS_ERR(req))
1869 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001870
1871 /* it may be a short read due to an object boundary */
1872 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001873
Sage Weilb7495fc2010-11-09 12:43:12 -08001874 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1875 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001876
1877 rc = ceph_osdc_start_request(osdc, req, false);
1878 if (!rc)
1879 rc = ceph_osdc_wait_request(osdc, req);
1880
1881 ceph_osdc_put_request(req);
1882 dout("readpages result %d\n", rc);
1883 return rc;
1884}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001885EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001886
1887/*
1888 * do a synchronous write on N pages
1889 */
1890int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1891 struct ceph_file_layout *layout,
1892 struct ceph_snap_context *snapc,
1893 u64 off, u64 len,
1894 u32 truncate_seq, u64 truncate_size,
1895 struct timespec *mtime,
1896 struct page **pages, int num_pages,
1897 int flags, int do_sync, bool nofail)
1898{
1899 struct ceph_osd_request *req;
1900 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001901 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001902
1903 BUG_ON(vino.snap != CEPH_NOSNAP);
1904 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1905 CEPH_OSD_OP_WRITE,
1906 flags | CEPH_OSD_FLAG_ONDISK |
1907 CEPH_OSD_FLAG_WRITE,
1908 snapc, do_sync,
1909 truncate_seq, truncate_size, mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -08001910 nofail, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001911 if (IS_ERR(req))
1912 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001913
1914 /* it may be a short write due to an object boundary */
1915 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001916 dout("writepages %llu~%llu (%d pages)\n", off, len,
1917 req->r_num_pages);
1918
1919 rc = ceph_osdc_start_request(osdc, req, nofail);
1920 if (!rc)
1921 rc = ceph_osdc_wait_request(osdc, req);
1922
1923 ceph_osdc_put_request(req);
1924 if (rc == 0)
1925 rc = len;
1926 dout("writepages result %d\n", rc);
1927 return rc;
1928}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001929EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001930
1931/*
1932 * handle incoming message
1933 */
1934static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1935{
1936 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001937 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001938 int type = le16_to_cpu(msg->hdr.type);
1939
1940 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001941 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001942 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001943
1944 switch (type) {
1945 case CEPH_MSG_OSD_MAP:
1946 ceph_osdc_handle_map(osdc, msg);
1947 break;
1948 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001949 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001950 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001951 case CEPH_MSG_WATCH_NOTIFY:
1952 handle_watch_notify(osdc, msg);
1953 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001954
1955 default:
1956 pr_err("received unknown message type %d %s\n", type,
1957 ceph_msg_type_name(type));
1958 }
Sage Weil4a32f932010-06-13 10:27:53 -07001959out:
Sage Weilf24e9982009-10-06 11:31:10 -07001960 ceph_msg_put(msg);
1961}
1962
Sage Weil5b3a4db2010-02-19 21:43:23 -08001963/*
Sage Weil21b667f2010-03-04 10:22:59 -08001964 * lookup and return message for incoming reply. set up reply message
1965 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001966 */
1967static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001968 struct ceph_msg_header *hdr,
1969 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001970{
1971 struct ceph_osd *osd = con->private;
1972 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001973 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001974 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001975 int front = le32_to_cpu(hdr->front_len);
1976 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001977 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07001978
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001979 tid = le64_to_cpu(hdr->tid);
1980 mutex_lock(&osdc->request_mutex);
1981 req = __lookup_request(osdc, tid);
1982 if (!req) {
1983 *skip = 1;
1984 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07001985 dout("get_reply unknown tid %llu from osd%d\n", tid,
1986 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001987 goto out;
1988 }
Sage Weilc16e7862010-03-01 13:02:00 -08001989
1990 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05001991 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08001992 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05001993 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07001994 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07001995 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001996 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001997
Sage Weilc16e7862010-03-01 13:02:00 -08001998 if (front > req->r_reply->front.iov_len) {
1999 pr_warning("get_reply front %d > preallocated %d\n",
2000 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002001 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002002 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002003 goto out;
2004 ceph_msg_put(req->r_reply);
2005 req->r_reply = m;
2006 }
2007 m = ceph_msg_get(req->r_reply);
2008
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002009 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08002010 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08002011
2012 if (unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07002013 pr_warning("tid %lld reply has %d bytes %d pages, we"
2014 " had only %d pages ready\n", tid, data_len,
2015 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002016 *skip = 1;
2017 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07002018 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08002019 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002020 }
Sage Weil21b667f2010-03-04 10:22:59 -08002021 m->pages = req->r_pages;
2022 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08002023 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002024#ifdef CONFIG_BLOCK
2025 m->bio = req->r_bio;
2026#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002027 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002028 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002029 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002030 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002031
2032out:
2033 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002034 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002035
2036}
2037
2038static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2039 struct ceph_msg_header *hdr,
2040 int *skip)
2041{
2042 struct ceph_osd *osd = con->private;
2043 int type = le16_to_cpu(hdr->type);
2044 int front = le32_to_cpu(hdr->front_len);
2045
Alex Elder1c20f2d2012-06-04 14:43:32 -05002046 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002047 switch (type) {
2048 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002049 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002050 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002051 case CEPH_MSG_OSD_OPREPLY:
2052 return get_reply(con, hdr, skip);
2053 default:
2054 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2055 osd->o_osd);
2056 *skip = 1;
2057 return NULL;
2058 }
Sage Weilf24e9982009-10-06 11:31:10 -07002059}
2060
2061/*
2062 * Wrappers to refcount containing ceph_osd struct
2063 */
2064static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2065{
2066 struct ceph_osd *osd = con->private;
2067 if (get_osd(osd))
2068 return con;
2069 return NULL;
2070}
2071
2072static void put_osd_con(struct ceph_connection *con)
2073{
2074 struct ceph_osd *osd = con->private;
2075 put_osd(osd);
2076}
2077
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002078/*
2079 * authentication
2080 */
Alex Eldera3530df2012-05-16 15:16:39 -05002081/*
2082 * Note: returned pointer is the address of a structure that's
2083 * managed separately. Caller must *not* attempt to free it.
2084 */
2085static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002086 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002087{
2088 struct ceph_osd *o = con->private;
2089 struct ceph_osd_client *osdc = o->o_osdc;
2090 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002091 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002092
Alex Elder74f18692012-05-16 15:16:39 -05002093 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002094 if (ac->ops && ac->ops->destroy_authorizer)
2095 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002096 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002097 }
Alex Eldera2556512012-05-16 15:16:39 -05002098 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002099 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2100 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002101 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002102 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002103 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002104 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002105
Alex Eldera3530df2012-05-16 15:16:39 -05002106 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002107}
2108
2109
2110static int verify_authorizer_reply(struct ceph_connection *con, int len)
2111{
2112 struct ceph_osd *o = con->private;
2113 struct ceph_osd_client *osdc = o->o_osdc;
2114 struct ceph_auth_client *ac = osdc->client->monc.auth;
2115
Alex Eldera2556512012-05-16 15:16:39 -05002116 /*
2117 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2118 * XXX which do we do: succeed or fail?
2119 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002120 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002121}
2122
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002123static int invalidate_authorizer(struct ceph_connection *con)
2124{
2125 struct ceph_osd *o = con->private;
2126 struct ceph_osd_client *osdc = o->o_osdc;
2127 struct ceph_auth_client *ac = osdc->client->monc.auth;
2128
Alex Eldera2556512012-05-16 15:16:39 -05002129 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002130 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2131
2132 return ceph_monc_validate_auth(&osdc->client->monc);
2133}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002134
Tobias Klauser9e327892010-05-20 10:40:19 +02002135static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002136 .get = get_osd_con,
2137 .put = put_osd_con,
2138 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002139 .get_authorizer = get_authorizer,
2140 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002141 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002142 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002143 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002144};