blob: 5ef24e3e16273dec61d96d70c2e4bf014e80e605 [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
Alex Elderf9d25192013-02-15 11:42:29 -060026static void __send_queued(struct ceph_osd_client *osdc);
Sage Weil6f6c7002011-01-17 20:34:08 -080027static 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
Sage Weilf24e9982009-10-06 11:31:10 -070041/*
42 * Implement client access to distributed object storage cluster.
43 *
44 * All data objects are stored within a cluster/cloud of OSDs, or
45 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
46 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
47 * remote daemons serving up and coordinating consistent and safe
48 * access to storage.
49 *
50 * Cluster membership and the mapping of data objects onto storage devices
51 * are described by the osd map.
52 *
53 * We keep track of pending OSD requests (read, write), resubmit
54 * requests to different OSDs when the cluster topology/data layout
55 * change, or retry the affected requests when the communications
56 * channel with an OSD is reset.
57 */
58
59/*
60 * calculate the mapping of a file extent onto an object, and fill out the
61 * request accordingly. shorten extent as necessary if it crosses an
62 * object boundary.
63 *
64 * fill osd op in request message.
65 */
Alex Elderdbe0fc42013-02-15 22:10:17 -060066static int calc_layout(struct ceph_file_layout *layout, u64 off, u64 *plen,
Alex Elder47a05812013-02-15 22:10:17 -060067 struct ceph_osd_req_op *op, u64 *bno)
Sage Weilf24e9982009-10-06 11:31:10 -070068{
Alex Elder60e56f12013-02-15 11:42:29 -060069 u64 orig_len = *plen;
Alex Elder60e56f12013-02-15 11:42:29 -060070 u64 objoff = 0;
71 u64 objlen = 0;
Sage Weild63b77f2012-09-24 20:59:48 -070072 int r;
Sage Weilf24e9982009-10-06 11:31:10 -070073
Alex Elder60e56f12013-02-15 11:42:29 -060074 /* object extent? */
Alex Elder47a05812013-02-15 22:10:17 -060075 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
Alex Elder60e56f12013-02-15 11:42:29 -060076 &objoff, &objlen);
Sage Weild63b77f2012-09-24 20:59:48 -070077 if (r < 0)
78 return r;
Alex Elder60e56f12013-02-15 11:42:29 -060079 if (objlen < orig_len) {
80 *plen = objlen;
81 dout(" skipping last %llu, final file extent %llu~%llu\n",
82 orig_len - *plen, off, *plen);
83 }
84
85 if (op_has_extent(op->op)) {
86 u32 osize = le32_to_cpu(layout->fl_object_size);
87 op->extent.offset = objoff;
88 op->extent.length = objlen;
89 if (op->extent.truncate_size <= off - objoff) {
90 op->extent.truncate_size = 0;
91 } else {
92 op->extent.truncate_size -= off - objoff;
93 if (op->extent.truncate_size > osize)
94 op->extent.truncate_size = osize;
95 }
96 }
Alex Elder60e56f12013-02-15 11:42:29 -060097 if (op->op == CEPH_OSD_OP_WRITE)
98 op->payload_len = *plen;
99
Alex Elder60cf5992013-02-15 22:10:17 -0600100 dout("calc_layout bno=%llx %llu~%llu\n", *bno, objoff, objlen);
Sage Weilf24e9982009-10-06 11:31:10 -0700101
Alex Elder3ff5f382013-02-15 22:10:17 -0600102 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700103}
104
Sage Weilf24e9982009-10-06 11:31:10 -0700105/*
106 * requests
107 */
Sage Weil415e49a2009-12-07 13:37:03 -0800108void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700109{
Alex Eldere0c59482013-03-07 15:38:25 -0600110 int num_pages;
Sage Weil415e49a2009-12-07 13:37:03 -0800111 struct ceph_osd_request *req = container_of(kref,
112 struct ceph_osd_request,
113 r_kref);
114
115 if (req->r_request)
116 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800117 if (req->r_con_filling_msg) {
Alex Elder9cbb1d72013-01-31 16:02:00 -0600118 dout("%s revoking msg %p from con %p\n", __func__,
119 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500120 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700121 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Alex Elder9cbb1d72013-01-31 16:02:00 -0600122 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800123 }
Alex Elderab8cb342012-06-04 14:43:32 -0500124 if (req->r_reply)
125 ceph_msg_put(req->r_reply);
Alex Elder0fff87e2013-02-14 12:16:43 -0600126
127 if (req->r_data_in.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600128 req->r_data_in.own_pages) {
129 num_pages = calc_pages_for((u64)req->r_data_in.alignment,
130 (u64)req->r_data_in.length);
131 ceph_release_page_vector(req->r_data_in.pages, num_pages);
132 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600133 if (req->r_data_out.type == CEPH_OSD_DATA_TYPE_PAGES &&
Alex Eldere0c59482013-03-07 15:38:25 -0600134 req->r_data_out.own_pages) {
135 num_pages = calc_pages_for((u64)req->r_data_out.alignment,
136 (u64)req->r_data_out.length);
137 ceph_release_page_vector(req->r_data_out.pages, num_pages);
138 }
Alex Elder0fff87e2013-02-14 12:16:43 -0600139
Sage Weil415e49a2009-12-07 13:37:03 -0800140 ceph_put_snap_context(req->r_snapc);
141 if (req->r_mempool)
142 mempool_free(req, req->r_osdc->req_mempool);
143 else
144 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700145}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700146EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700147
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700148struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700149 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800150 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700151 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600152 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700153{
154 struct ceph_osd_request *req;
155 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800156 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700157
Sage Weil1b83bef2013-02-25 16:11:12 -0800158 msg_size = 4 + 4 + 8 + 8 + 4+8;
159 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
160 msg_size += 1 + 8 + 4 + 4; /* pg_t */
161 msg_size += 4 + MAX_OBJ_NAME_SIZE;
162 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
163 msg_size += 8; /* snapid */
164 msg_size += 8; /* snap_seq */
165 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
166 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700167
168 if (use_mempool) {
169 req = mempool_alloc(osdc->req_mempool, gfp_flags);
170 memset(req, 0, sizeof(*req));
171 } else {
172 req = kzalloc(sizeof(*req), gfp_flags);
173 }
174 if (req == NULL)
175 return NULL;
176
177 req->r_osdc = osdc;
178 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700179
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700180 kref_init(&req->r_kref);
181 init_completion(&req->r_completion);
182 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600183 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700184 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700185 INIT_LIST_HEAD(&req->r_linger_item);
186 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700187 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700188 INIT_LIST_HEAD(&req->r_osd_item);
189
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700190 /* create reply message */
191 if (use_mempool)
192 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
193 else
194 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700195 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700196 if (!msg) {
197 ceph_osdc_put_request(req);
198 return NULL;
199 }
200 req->r_reply = msg;
201
Alex Elder0fff87e2013-02-14 12:16:43 -0600202 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
203 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
Sage Weild50b4092012-07-09 14:22:34 -0700204
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700205 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700206 if (use_mempool)
207 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
208 else
Sage Weilb61c2762011-08-09 15:03:46 -0700209 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700210 if (!msg) {
211 ceph_osdc_put_request(req);
212 return NULL;
213 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700214
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700215 memset(msg->front.iov_base, 0, msg->front.iov_len);
216
217 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700218
219 return req;
220}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700221EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700222
Alex Elder175face2013-03-08 13:35:36 -0600223static u64 osd_req_encode_op(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700224 struct ceph_osd_op *dst,
225 struct ceph_osd_req_op *src)
226{
Alex Elder175face2013-03-08 13:35:36 -0600227 u64 out_data_len = 0;
Alex Elder95e072e2013-03-08 13:35:36 -0600228 struct ceph_pagelist *pagelist;
Alex Elder175face2013-03-08 13:35:36 -0600229
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700230 dst->op = cpu_to_le16(src->op);
231
Alex Elder065a68f2012-04-20 15:49:43 -0500232 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600233 case CEPH_OSD_OP_STAT:
234 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700235 case CEPH_OSD_OP_READ:
236 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600237 if (src->op == CEPH_OSD_OP_WRITE)
238 out_data_len = src->extent.length;
239 dst->extent.offset = cpu_to_le64(src->extent.offset);
240 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700241 dst->extent.truncate_size =
242 cpu_to_le64(src->extent.truncate_size);
243 dst->extent.truncate_seq =
244 cpu_to_le32(src->extent.truncate_seq);
245 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700246 case CEPH_OSD_OP_CALL:
Alex Elder95e072e2013-03-08 13:35:36 -0600247 pagelist = kmalloc(sizeof (*pagelist), GFP_NOFS);
248 BUG_ON(!pagelist);
249 ceph_pagelist_init(pagelist);
250
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700251 dst->cls.class_len = src->cls.class_len;
252 dst->cls.method_len = src->cls.method_len;
253 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600254 ceph_pagelist_append(pagelist, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700255 src->cls.class_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600256 ceph_pagelist_append(pagelist, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700257 src->cls.method_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600258 ceph_pagelist_append(pagelist, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700259 src->cls.indata_len);
Alex Elder95e072e2013-03-08 13:35:36 -0600260
261 req->r_data_out.type = CEPH_OSD_DATA_TYPE_PAGELIST;
262 req->r_data_out.pagelist = pagelist;
263 out_data_len = pagelist->length;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700264 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700265 case CEPH_OSD_OP_STARTSYNC:
266 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700267 case CEPH_OSD_OP_NOTIFY_ACK:
268 case CEPH_OSD_OP_WATCH:
269 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
270 dst->watch.ver = cpu_to_le64(src->watch.ver);
271 dst->watch.flag = src->watch.flag;
272 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700273 default:
Alex Elder8f63ca22013-03-04 11:08:29 -0600274 pr_err("unrecognized osd opcode %d\n", src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700275 WARN_ON(1);
276 break;
Alex Elder4c464592013-02-15 11:42:30 -0600277 case CEPH_OSD_OP_MAPEXT:
278 case CEPH_OSD_OP_MASKTRUNC:
279 case CEPH_OSD_OP_SPARSE_READ:
Alex Eldera9f36c32013-02-15 11:42:30 -0600280 case CEPH_OSD_OP_NOTIFY:
Alex Elder4c464592013-02-15 11:42:30 -0600281 case CEPH_OSD_OP_ASSERT_VER:
282 case CEPH_OSD_OP_WRITEFULL:
283 case CEPH_OSD_OP_TRUNCATE:
284 case CEPH_OSD_OP_ZERO:
285 case CEPH_OSD_OP_DELETE:
286 case CEPH_OSD_OP_APPEND:
287 case CEPH_OSD_OP_SETTRUNC:
288 case CEPH_OSD_OP_TRIMTRUNC:
289 case CEPH_OSD_OP_TMAPUP:
290 case CEPH_OSD_OP_TMAPPUT:
291 case CEPH_OSD_OP_TMAPGET:
292 case CEPH_OSD_OP_CREATE:
Alex Eldera9f36c32013-02-15 11:42:30 -0600293 case CEPH_OSD_OP_ROLLBACK:
Alex Elder4c464592013-02-15 11:42:30 -0600294 case CEPH_OSD_OP_OMAPGETKEYS:
295 case CEPH_OSD_OP_OMAPGETVALS:
296 case CEPH_OSD_OP_OMAPGETHEADER:
297 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
298 case CEPH_OSD_OP_MODE_RD:
299 case CEPH_OSD_OP_OMAPSETVALS:
300 case CEPH_OSD_OP_OMAPSETHEADER:
301 case CEPH_OSD_OP_OMAPCLEAR:
302 case CEPH_OSD_OP_OMAPRMKEYS:
303 case CEPH_OSD_OP_OMAP_CMP:
304 case CEPH_OSD_OP_CLONERANGE:
305 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
306 case CEPH_OSD_OP_SRC_CMPXATTR:
Alex Eldera9f36c32013-02-15 11:42:30 -0600307 case CEPH_OSD_OP_GETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600308 case CEPH_OSD_OP_GETXATTRS:
Alex Eldera9f36c32013-02-15 11:42:30 -0600309 case CEPH_OSD_OP_CMPXATTR:
310 case CEPH_OSD_OP_SETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600311 case CEPH_OSD_OP_SETXATTRS:
312 case CEPH_OSD_OP_RESETXATTRS:
313 case CEPH_OSD_OP_RMXATTR:
314 case CEPH_OSD_OP_PULL:
315 case CEPH_OSD_OP_PUSH:
316 case CEPH_OSD_OP_BALANCEREADS:
317 case CEPH_OSD_OP_UNBALANCEREADS:
318 case CEPH_OSD_OP_SCRUB:
319 case CEPH_OSD_OP_SCRUB_RESERVE:
320 case CEPH_OSD_OP_SCRUB_UNRESERVE:
321 case CEPH_OSD_OP_SCRUB_STOP:
322 case CEPH_OSD_OP_SCRUB_MAP:
323 case CEPH_OSD_OP_WRLOCK:
324 case CEPH_OSD_OP_WRUNLOCK:
325 case CEPH_OSD_OP_RDLOCK:
326 case CEPH_OSD_OP_RDUNLOCK:
327 case CEPH_OSD_OP_UPLOCK:
328 case CEPH_OSD_OP_DNLOCK:
329 case CEPH_OSD_OP_PGLS:
330 case CEPH_OSD_OP_PGLS_FILTER:
331 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600332 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600333 WARN_ON(1);
334 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700335 }
336 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600337
338 return out_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700339}
340
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700341/*
342 * build new request AND message
343 *
344 */
345void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder175face2013-03-08 13:35:36 -0600346 u64 off, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700347 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600348 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600349 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700350{
351 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700352 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700353 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800354 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600356 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700357 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700358
Sage Weil1b83bef2013-02-25 16:11:12 -0800359 req->r_num_ops = num_ops;
360 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700361 req->r_snapc = ceph_get_snap_context(snapc);
362
Sage Weil1b83bef2013-02-25 16:11:12 -0800363 /* encode request */
364 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700365
Sage Weil1b83bef2013-02-25 16:11:12 -0800366 p = msg->front.iov_base;
367 ceph_encode_32(&p, 1); /* client_inc is always 1 */
368 req->r_request_osdmap_epoch = p;
369 p += 4;
370 req->r_request_flags = p;
371 p += 4;
372 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
373 ceph_encode_timespec(p, mtime);
374 p += sizeof(struct ceph_timespec);
375 req->r_request_reassert_version = p;
376 p += sizeof(struct ceph_eversion); /* will get filled in */
377
378 /* oloc */
379 ceph_encode_8(&p, 4);
380 ceph_encode_8(&p, 4);
381 ceph_encode_32(&p, 8 + 4 + 4);
382 req->r_request_pool = p;
383 p += 8;
384 ceph_encode_32(&p, -1); /* preferred */
385 ceph_encode_32(&p, 0); /* key len */
386
387 ceph_encode_8(&p, 1);
388 req->r_request_pgid = p;
389 p += 8 + 4;
390 ceph_encode_32(&p, -1); /* preferred */
391
392 /* oid */
393 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600394 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800395 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600396 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700397
Alex Elder175face2013-03-08 13:35:36 -0600398 /* ops--can imply data */
Sage Weil1b83bef2013-02-25 16:11:12 -0800399 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700400 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800401 req->r_request_ops = p;
Alex Elder175face2013-03-08 13:35:36 -0600402 data_len = 0;
Sage Weil1b83bef2013-02-25 16:11:12 -0800403 for (i = 0; i < num_ops; i++, src_op++) {
Alex Elder175face2013-03-08 13:35:36 -0600404 data_len += osd_req_encode_op(req, p, src_op);
Sage Weil1b83bef2013-02-25 16:11:12 -0800405 p += sizeof(struct ceph_osd_op);
406 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700407
Sage Weil1b83bef2013-02-25 16:11:12 -0800408 /* snaps */
409 ceph_encode_64(&p, req->r_snapid);
410 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
411 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
412 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700413 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800414 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700415 }
416 }
417
Sage Weil1b83bef2013-02-25 16:11:12 -0800418 req->r_request_attempts = p;
419 p += 4;
420
Alex Elder175face2013-03-08 13:35:36 -0600421 /* data */
422 if (flags & CEPH_OSD_FLAG_WRITE)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700423 req->r_request->hdr.data_off = cpu_to_le16(off);
Alex Elderf44246e2013-02-14 12:16:43 -0600424 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800425
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700426 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
427 msg_size = p - msg->front.iov_base;
428 msg->front.iov_len = msg_size;
429 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800430
431 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
432 num_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700433 return;
434}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700435EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700436
Sage Weilf24e9982009-10-06 11:31:10 -0700437/*
438 * build new request AND message, calculate layout, and adjust file
439 * extent as needed.
440 *
441 * if the file was recently truncated, we include information about its
442 * old and new size so that the object can be updated appropriately. (we
443 * avoid synchronously deleting truncated objects because it's slow.)
444 *
445 * if @do_sync, include a 'startsync' command so that the osd will flush
446 * data quickly.
447 */
448struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
449 struct ceph_file_layout *layout,
450 struct ceph_vino vino,
451 u64 off, u64 *plen,
452 int opcode, int flags,
453 struct ceph_snap_context *snapc,
454 int do_sync,
455 u32 truncate_seq,
456 u64 truncate_size,
457 struct timespec *mtime,
Alex Elder153e5162013-03-01 18:00:15 -0600458 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700459{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600460 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700461 struct ceph_osd_request *req;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600462 unsigned int num_op = 1;
Alex Elder47a05812013-02-15 22:10:17 -0600463 u64 bno = 0;
Sage Weil68162822012-09-24 21:01:02 -0700464 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700465
Alex Elderae7ca4a32012-11-13 21:11:15 -0600466 memset(&ops, 0, sizeof ops);
467
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700468 ops[0].op = opcode;
469 ops[0].extent.truncate_seq = truncate_seq;
470 ops[0].extent.truncate_size = truncate_size;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700471
472 if (do_sync) {
473 ops[1].op = CEPH_OSD_OP_STARTSYNC;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600474 num_op++;
475 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700476
Alex Elderae7ca4a32012-11-13 21:11:15 -0600477 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
478 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700479 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700480 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600481 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700482
483 /* calculate max write size */
Alex Elder60cf5992013-02-15 22:10:17 -0600484 r = calc_layout(layout, off, plen, ops, &bno);
Alex Elder3ff5f382013-02-15 22:10:17 -0600485 if (r < 0) {
486 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700487 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600488 }
Sage Weilf24e9982009-10-06 11:31:10 -0700489 req->r_file_layout = *layout; /* keep a copy */
490
Alex Elderdbe0fc42013-02-15 22:10:17 -0600491 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
492 req->r_oid_len = strlen(req->r_oid);
493
Alex Elder175face2013-03-08 13:35:36 -0600494 ceph_osdc_build_request(req, off, num_op, ops,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600495 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700496
Sage Weilf24e9982009-10-06 11:31:10 -0700497 return req;
498}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700499EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700500
501/*
502 * We keep osd requests in an rbtree, sorted by ->r_tid.
503 */
504static void __insert_request(struct ceph_osd_client *osdc,
505 struct ceph_osd_request *new)
506{
507 struct rb_node **p = &osdc->requests.rb_node;
508 struct rb_node *parent = NULL;
509 struct ceph_osd_request *req = NULL;
510
511 while (*p) {
512 parent = *p;
513 req = rb_entry(parent, struct ceph_osd_request, r_node);
514 if (new->r_tid < req->r_tid)
515 p = &(*p)->rb_left;
516 else if (new->r_tid > req->r_tid)
517 p = &(*p)->rb_right;
518 else
519 BUG();
520 }
521
522 rb_link_node(&new->r_node, parent, p);
523 rb_insert_color(&new->r_node, &osdc->requests);
524}
525
526static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
527 u64 tid)
528{
529 struct ceph_osd_request *req;
530 struct rb_node *n = osdc->requests.rb_node;
531
532 while (n) {
533 req = rb_entry(n, struct ceph_osd_request, r_node);
534 if (tid < req->r_tid)
535 n = n->rb_left;
536 else if (tid > req->r_tid)
537 n = n->rb_right;
538 else
539 return req;
540 }
541 return NULL;
542}
543
544static struct ceph_osd_request *
545__lookup_request_ge(struct ceph_osd_client *osdc,
546 u64 tid)
547{
548 struct ceph_osd_request *req;
549 struct rb_node *n = osdc->requests.rb_node;
550
551 while (n) {
552 req = rb_entry(n, struct ceph_osd_request, r_node);
553 if (tid < req->r_tid) {
554 if (!n->rb_left)
555 return req;
556 n = n->rb_left;
557 } else if (tid > req->r_tid) {
558 n = n->rb_right;
559 } else {
560 return req;
561 }
562 }
563 return NULL;
564}
565
Sage Weil6f6c7002011-01-17 20:34:08 -0800566/*
567 * Resubmit requests pending on the given osd.
568 */
569static void __kick_osd_requests(struct ceph_osd_client *osdc,
570 struct ceph_osd *osd)
571{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700572 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800573 int err;
574
575 dout("__kick_osd_requests osd%d\n", osd->o_osd);
576 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600577 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800578 return;
579
580 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
581 list_move(&req->r_req_lru_item, &osdc->req_unsent);
582 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
583 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700584 if (!req->r_linger)
585 req->r_flags |= CEPH_OSD_FLAG_RETRY;
586 }
587
588 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
589 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700590 /*
591 * reregister request prior to unregistering linger so
592 * that r_osd is preserved.
593 */
594 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700595 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700596 list_add(&req->r_req_lru_item, &osdc->req_unsent);
597 list_add(&req->r_osd_item, &req->r_osd->o_requests);
598 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700599 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
600 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800601 }
602}
603
Sage Weilf24e9982009-10-06 11:31:10 -0700604/*
Sage Weil81b024e2009-10-09 10:29:18 -0700605 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700606 */
607static void osd_reset(struct ceph_connection *con)
608{
609 struct ceph_osd *osd = con->private;
610 struct ceph_osd_client *osdc;
611
612 if (!osd)
613 return;
614 dout("osd_reset osd%d\n", osd->o_osd);
615 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700616 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800617 mutex_lock(&osdc->request_mutex);
618 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600619 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800620 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700621 up_read(&osdc->map_sem);
622}
623
624/*
625 * Track open sessions with osds.
626 */
Alex Eldere10006f2012-05-26 23:26:43 -0500627static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700628{
629 struct ceph_osd *osd;
630
631 osd = kzalloc(sizeof(*osd), GFP_NOFS);
632 if (!osd)
633 return NULL;
634
635 atomic_set(&osd->o_ref, 1);
636 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500637 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600638 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700639 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700640 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800641 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700642 osd->o_incarnation = 1;
643
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700644 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800645
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800646 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700647 return osd;
648}
649
650static struct ceph_osd *get_osd(struct ceph_osd *osd)
651{
652 if (atomic_inc_not_zero(&osd->o_ref)) {
653 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
654 atomic_read(&osd->o_ref));
655 return osd;
656 } else {
657 dout("get_osd %p FAIL\n", osd);
658 return NULL;
659 }
660}
661
662static void put_osd(struct ceph_osd *osd)
663{
664 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
665 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500666 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700667 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
668
Alex Eldera2556512012-05-16 15:16:39 -0500669 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500670 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700671 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700672 }
Sage Weilf24e9982009-10-06 11:31:10 -0700673}
674
675/*
676 * remove an osd from our map
677 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800678static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700679{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800680 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700681 BUG_ON(!list_empty(&osd->o_requests));
682 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800683 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700684 ceph_con_close(&osd->o_con);
685 put_osd(osd);
686}
687
Sage Weilaca420b2011-08-31 14:45:53 -0700688static void remove_all_osds(struct ceph_osd_client *osdc)
689{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500690 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700691 mutex_lock(&osdc->request_mutex);
692 while (!RB_EMPTY_ROOT(&osdc->osds)) {
693 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
694 struct ceph_osd, o_node);
695 __remove_osd(osdc, osd);
696 }
697 mutex_unlock(&osdc->request_mutex);
698}
699
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800700static void __move_osd_to_lru(struct ceph_osd_client *osdc,
701 struct ceph_osd *osd)
702{
703 dout("__move_osd_to_lru %p\n", osd);
704 BUG_ON(!list_empty(&osd->o_osd_lru));
705 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700706 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800707}
708
709static void __remove_osd_from_lru(struct ceph_osd *osd)
710{
711 dout("__remove_osd_from_lru %p\n", osd);
712 if (!list_empty(&osd->o_osd_lru))
713 list_del_init(&osd->o_osd_lru);
714}
715
Sage Weilaca420b2011-08-31 14:45:53 -0700716static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800717{
718 struct ceph_osd *osd, *nosd;
719
720 dout("__remove_old_osds %p\n", osdc);
721 mutex_lock(&osdc->request_mutex);
722 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700723 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800724 break;
725 __remove_osd(osdc, osd);
726 }
727 mutex_unlock(&osdc->request_mutex);
728}
729
Sage Weilf24e9982009-10-06 11:31:10 -0700730/*
731 * reset osd connect
732 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800733static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700734{
Alex Elderc3acb182012-12-07 09:57:58 -0600735 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700736
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800737 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700738 if (list_empty(&osd->o_requests) &&
739 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800740 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600741
742 return -ENODEV;
743 }
744
745 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
746 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
747 !ceph_con_opened(&osd->o_con)) {
748 struct ceph_osd_request *req;
749
Sage Weil87b315a2010-03-22 14:51:18 -0700750 dout(" osd addr hasn't changed and connection never opened,"
751 " letting msgr retry");
752 /* touch each r_stamp for handle_timeout()'s benfit */
753 list_for_each_entry(req, &osd->o_requests, r_osd_item)
754 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600755
756 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700757 }
Alex Elderc3acb182012-12-07 09:57:58 -0600758
759 ceph_con_close(&osd->o_con);
760 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
761 osd->o_incarnation++;
762
763 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700764}
765
766static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
767{
768 struct rb_node **p = &osdc->osds.rb_node;
769 struct rb_node *parent = NULL;
770 struct ceph_osd *osd = NULL;
771
Sage Weilaca420b2011-08-31 14:45:53 -0700772 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700773 while (*p) {
774 parent = *p;
775 osd = rb_entry(parent, struct ceph_osd, o_node);
776 if (new->o_osd < osd->o_osd)
777 p = &(*p)->rb_left;
778 else if (new->o_osd > osd->o_osd)
779 p = &(*p)->rb_right;
780 else
781 BUG();
782 }
783
784 rb_link_node(&new->o_node, parent, p);
785 rb_insert_color(&new->o_node, &osdc->osds);
786}
787
788static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
789{
790 struct ceph_osd *osd;
791 struct rb_node *n = osdc->osds.rb_node;
792
793 while (n) {
794 osd = rb_entry(n, struct ceph_osd, o_node);
795 if (o < osd->o_osd)
796 n = n->rb_left;
797 else if (o > osd->o_osd)
798 n = n->rb_right;
799 else
800 return osd;
801 }
802 return NULL;
803}
804
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800805static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
806{
807 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700808 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800809}
810
811static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
812{
813 cancel_delayed_work(&osdc->timeout_work);
814}
Sage Weilf24e9982009-10-06 11:31:10 -0700815
816/*
817 * Register request, assign tid. If this is the first request, set up
818 * the timeout event.
819 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700820static void __register_request(struct ceph_osd_client *osdc,
821 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700822{
Sage Weilf24e9982009-10-06 11:31:10 -0700823 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800824 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700825 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700826 __insert_request(osdc, req);
827 ceph_osdc_get_request(req);
828 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700829 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800830 dout(" first request, scheduling timeout\n");
831 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700832 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700833}
834
835static void register_request(struct ceph_osd_client *osdc,
836 struct ceph_osd_request *req)
837{
838 mutex_lock(&osdc->request_mutex);
839 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700840 mutex_unlock(&osdc->request_mutex);
841}
842
843/*
844 * called under osdc->request_mutex
845 */
846static void __unregister_request(struct ceph_osd_client *osdc,
847 struct ceph_osd_request *req)
848{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500849 if (RB_EMPTY_NODE(&req->r_node)) {
850 dout("__unregister_request %p tid %lld not registered\n",
851 req, req->r_tid);
852 return;
853 }
854
Sage Weilf24e9982009-10-06 11:31:10 -0700855 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
856 rb_erase(&req->r_node, &osdc->requests);
857 osdc->num_requests--;
858
Sage Weil0ba64782009-10-08 16:57:16 -0700859 if (req->r_osd) {
860 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500861 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700862
863 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700864 if (list_empty(&req->r_osd->o_requests) &&
865 list_empty(&req->r_osd->o_linger_requests)) {
866 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800867 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700868 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700869 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700870 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700871 }
Sage Weilf24e9982009-10-06 11:31:10 -0700872
Alex Elder7d5f2482012-11-29 08:37:03 -0600873 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700874 ceph_osdc_put_request(req);
875
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800876 if (osdc->num_requests == 0) {
877 dout(" no requests, canceling timeout\n");
878 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700879 }
880}
881
882/*
883 * Cancel a previously queued request message
884 */
885static void __cancel_request(struct ceph_osd_request *req)
886{
Sage Weil6bc18872010-09-27 10:18:52 -0700887 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500888 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700889 req->r_sent = 0;
890 }
891}
892
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700893static void __register_linger_request(struct ceph_osd_client *osdc,
894 struct ceph_osd_request *req)
895{
896 dout("__register_linger_request %p\n", req);
897 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700898 if (req->r_osd)
899 list_add_tail(&req->r_linger_osd,
900 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700901}
902
903static void __unregister_linger_request(struct ceph_osd_client *osdc,
904 struct ceph_osd_request *req)
905{
906 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -0600907 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700908 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700909 list_del_init(&req->r_linger_osd);
910
911 if (list_empty(&req->r_osd->o_requests) &&
912 list_empty(&req->r_osd->o_linger_requests)) {
913 dout("moving osd to %p lru\n", req->r_osd);
914 __move_osd_to_lru(osdc, req->r_osd);
915 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700916 if (list_empty(&req->r_osd_item))
917 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700918 }
919}
920
921void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
922 struct ceph_osd_request *req)
923{
924 mutex_lock(&osdc->request_mutex);
925 if (req->r_linger) {
926 __unregister_linger_request(osdc, req);
927 ceph_osdc_put_request(req);
928 }
929 mutex_unlock(&osdc->request_mutex);
930}
931EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
932
933void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
934 struct ceph_osd_request *req)
935{
936 if (!req->r_linger) {
937 dout("set_request_linger %p\n", req);
938 req->r_linger = 1;
939 /*
940 * caller is now responsible for calling
941 * unregister_linger_request
942 */
943 ceph_osdc_get_request(req);
944 }
945}
946EXPORT_SYMBOL(ceph_osdc_set_request_linger);
947
Sage Weilf24e9982009-10-06 11:31:10 -0700948/*
949 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
950 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300951 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800952 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700953 *
954 * Return 0 if unchanged, 1 if changed, or negative on error.
955 *
956 * Caller should hold map_sem for read and request_mutex.
957 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800958static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700959 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700960{
Sage Weil5b191d92013-02-23 10:38:16 -0800961 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700962 int acting[CEPH_PG_MAX_SIZE];
963 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700964 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700965
Sage Weil6f6c7002011-01-17 20:34:08 -0800966 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -0600967 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
968 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -0800969 if (err) {
970 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700971 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800972 }
Sage Weil7740a422010-01-08 15:58:25 -0800973 req->r_pgid = pgid;
974
Sage Weild85b7052010-05-10 10:24:48 -0700975 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
976 if (err > 0) {
977 o = acting[0];
978 num = err;
979 }
Sage Weilf24e9982009-10-06 11:31:10 -0700980
Sage Weil38d64532011-10-14 13:33:55 -0700981 if ((!force_resend &&
982 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700983 req->r_sent >= req->r_osd->o_incarnation &&
984 req->r_num_pg_osds == num &&
985 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700986 (req->r_osd == NULL && o == -1))
987 return 0; /* no change */
988
Sage Weil5b191d92013-02-23 10:38:16 -0800989 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
990 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -0700991 req->r_osd ? req->r_osd->o_osd : -1);
992
Sage Weild85b7052010-05-10 10:24:48 -0700993 /* record full pg acting set */
994 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
995 req->r_num_pg_osds = num;
996
Sage Weilf24e9982009-10-06 11:31:10 -0700997 if (req->r_osd) {
998 __cancel_request(req);
999 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001000 req->r_osd = NULL;
1001 }
1002
1003 req->r_osd = __lookup_osd(osdc, o);
1004 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001005 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001006 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001007 if (!req->r_osd) {
1008 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001009 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001010 }
Sage Weilf24e9982009-10-06 11:31:10 -07001011
Sage Weil6f6c7002011-01-17 20:34:08 -08001012 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001013 __insert_osd(osdc, req->r_osd);
1014
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001015 ceph_con_open(&req->r_osd->o_con,
1016 CEPH_ENTITY_TYPE_OSD, o,
1017 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001018 }
1019
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001020 if (req->r_osd) {
1021 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001022 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001023 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1024 } else {
1025 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001026 }
Sage Weild85b7052010-05-10 10:24:48 -07001027 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001028
1029out:
Sage Weilf24e9982009-10-06 11:31:10 -07001030 return err;
1031}
1032
1033/*
1034 * caller should hold map_sem (for read) and request_mutex
1035 */
Sage Weil56e925b2012-01-03 12:34:34 -08001036static void __send_request(struct ceph_osd_client *osdc,
1037 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001038{
Sage Weil1b83bef2013-02-25 16:11:12 -08001039 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001040
Sage Weil1b83bef2013-02-25 16:11:12 -08001041 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1042 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1043 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001044
Sage Weil1b83bef2013-02-25 16:11:12 -08001045 /* fill in message content that changes each time we send it */
1046 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1047 put_unaligned_le32(req->r_flags, req->r_request_flags);
1048 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1049 p = req->r_request_pgid;
1050 ceph_encode_64(&p, req->r_pgid.pool);
1051 ceph_encode_32(&p, req->r_pgid.seed);
1052 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1053 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1054 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001055
Sage Weil3dd72fc2010-03-22 14:42:30 -07001056 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001057 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001058
1059 ceph_msg_get(req->r_request); /* send consumes a ref */
1060 ceph_con_send(&req->r_osd->o_con, req->r_request);
1061 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001062}
1063
1064/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001065 * Send any requests in the queue (req_unsent).
1066 */
Alex Elderf9d25192013-02-15 11:42:29 -06001067static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001068{
1069 struct ceph_osd_request *req, *tmp;
1070
Alex Elderf9d25192013-02-15 11:42:29 -06001071 dout("__send_queued\n");
1072 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001073 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001074}
1075
1076/*
Sage Weilf24e9982009-10-06 11:31:10 -07001077 * Timeout callback, called every N seconds when 1 or more osd
1078 * requests has been active for more than N seconds. When this
1079 * happens, we ping all OSDs with requests who have timed out to
1080 * ensure any communications channel reset is detected. Reset the
1081 * request timeouts another N seconds in the future as we go.
1082 * Reschedule the timeout event another N seconds in future (unless
1083 * there are no open requests).
1084 */
1085static void handle_timeout(struct work_struct *work)
1086{
1087 struct ceph_osd_client *osdc =
1088 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001089 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001090 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001091 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001092 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001093 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001094 dout("timeout\n");
1095 down_read(&osdc->map_sem);
1096
1097 ceph_monc_request_next_osdmap(&osdc->client->monc);
1098
1099 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001100
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001101 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001102 * ping osds that are a bit slow. this ensures that if there
1103 * is a break in the TCP connection we will notice, and reopen
1104 * a connection with that osd (from the fault callback).
1105 */
1106 INIT_LIST_HEAD(&slow_osds);
1107 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001108 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001109 break;
1110
1111 osd = req->r_osd;
1112 BUG_ON(!osd);
1113 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001114 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001115 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1116 }
1117 while (!list_empty(&slow_osds)) {
1118 osd = list_entry(slow_osds.next, struct ceph_osd,
1119 o_keepalive_item);
1120 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001121 ceph_con_keepalive(&osd->o_con);
1122 }
1123
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001124 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001125 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001126 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001127 up_read(&osdc->map_sem);
1128}
1129
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001130static void handle_osds_timeout(struct work_struct *work)
1131{
1132 struct ceph_osd_client *osdc =
1133 container_of(work, struct ceph_osd_client,
1134 osds_timeout_work.work);
1135 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001136 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001137
1138 dout("osds timeout\n");
1139 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001140 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001141 up_read(&osdc->map_sem);
1142
1143 schedule_delayed_work(&osdc->osds_timeout_work,
1144 round_jiffies_relative(delay));
1145}
1146
Sage Weil25845472011-06-03 09:37:09 -07001147static void complete_request(struct ceph_osd_request *req)
1148{
1149 if (req->r_safe_callback)
1150 req->r_safe_callback(req, NULL);
1151 complete_all(&req->r_safe_completion); /* fsync waiter */
1152}
1153
Sage Weil1b83bef2013-02-25 16:11:12 -08001154static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1155{
1156 __u8 v;
1157
1158 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1159 v = ceph_decode_8(p);
1160 if (v > 1) {
1161 pr_warning("do not understand pg encoding %d > 1", v);
1162 return -EINVAL;
1163 }
1164 pgid->pool = ceph_decode_64(p);
1165 pgid->seed = ceph_decode_32(p);
1166 *p += 4;
1167 return 0;
1168
1169bad:
1170 pr_warning("incomplete pg encoding");
1171 return -EINVAL;
1172}
1173
Sage Weilf24e9982009-10-06 11:31:10 -07001174/*
1175 * handle osd op reply. either call the callback if it is specified,
1176 * or do the completion to wake up the waiting thread.
1177 */
Sage Weil350b1c32009-12-22 10:45:45 -08001178static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1179 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001180{
Sage Weil1b83bef2013-02-25 16:11:12 -08001181 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001182 struct ceph_osd_request *req;
1183 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001184 int object_len;
1185 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001186 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001187 s32 retry_attempt;
1188 struct ceph_pg pg;
1189 int err;
1190 u32 reassert_epoch;
1191 u64 reassert_version;
1192 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001193 int already_completed;
Sage Weil1b83bef2013-02-25 16:11:12 -08001194 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001195
Sage Weil6df058c2009-12-22 11:24:33 -08001196 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001197 dout("handle_reply %p tid %llu\n", msg, tid);
1198
1199 p = msg->front.iov_base;
1200 end = p + msg->front.iov_len;
1201
1202 ceph_decode_need(&p, end, 4, bad);
1203 object_len = ceph_decode_32(&p);
1204 ceph_decode_need(&p, end, object_len, bad);
1205 p += object_len;
1206
1207 err = __decode_pgid(&p, end, &pg);
1208 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001209 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001210
1211 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1212 flags = ceph_decode_64(&p);
1213 result = ceph_decode_32(&p);
1214 reassert_epoch = ceph_decode_32(&p);
1215 reassert_version = ceph_decode_64(&p);
1216 osdmap_epoch = ceph_decode_32(&p);
1217
Sage Weilf24e9982009-10-06 11:31:10 -07001218 /* lookup */
1219 mutex_lock(&osdc->request_mutex);
1220 req = __lookup_request(osdc, tid);
1221 if (req == NULL) {
1222 dout("handle_reply tid %llu dne\n", tid);
1223 mutex_unlock(&osdc->request_mutex);
1224 return;
1225 }
1226 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001227
1228 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1229 req, result);
1230
1231 ceph_decode_need(&p, end, 4, bad);
1232 numops = ceph_decode_32(&p);
1233 if (numops > CEPH_OSD_MAX_OP)
1234 goto bad_put;
1235 if (numops != req->r_num_ops)
1236 goto bad_put;
1237 payload_len = 0;
1238 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1239 for (i = 0; i < numops; i++) {
1240 struct ceph_osd_op *op = p;
1241 int len;
1242
1243 len = le32_to_cpu(op->payload_len);
1244 req->r_reply_op_len[i] = len;
1245 dout(" op %d has %d bytes\n", i, len);
1246 payload_len += len;
1247 p += sizeof(*op);
1248 }
1249 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1250 pr_warning("sum of op payload lens %d != data_len %d",
1251 payload_len, le32_to_cpu(msg->hdr.data_len));
1252 goto bad_put;
1253 }
1254
1255 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1256 retry_attempt = ceph_decode_32(&p);
1257 for (i = 0; i < numops; i++)
1258 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001259
Sage Weil350b1c32009-12-22 10:45:45 -08001260 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001261 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001262 * avoid a (safe but slower) revoke later.
1263 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001264 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001265 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001266 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001267 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001268 }
1269
Sage Weilf24e9982009-10-06 11:31:10 -07001270 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001271 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001272
Sage Weil1b83bef2013-02-25 16:11:12 -08001273 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001274 bytes = le32_to_cpu(msg->hdr.data_len);
1275 dout("handle_reply result %d bytes %d\n", req->r_result,
1276 bytes);
1277 if (req->r_result == 0)
1278 req->r_result = bytes;
1279
1280 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001281 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1282 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001283
1284 req->r_got_reply = 1;
1285 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1286 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001287 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001288 goto done;
1289 }
1290
1291 dout("handle_reply tid %llu flags %d\n", tid, flags);
1292
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001293 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1294 __register_linger_request(osdc, req);
1295
Sage Weilf24e9982009-10-06 11:31:10 -07001296 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001297 if (result < 0 ||
1298 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001299 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1300 __unregister_request(osdc, req);
1301
Alex Elder0d5af162013-02-27 10:26:25 -06001302 already_completed = req->r_completed;
1303 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001304 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001305 if (already_completed)
1306 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001307
1308 if (req->r_callback)
1309 req->r_callback(req, msg);
1310 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001311 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001312
Sage Weil25845472011-06-03 09:37:09 -07001313 if (flags & CEPH_OSD_FLAG_ONDISK)
1314 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001315
1316done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001317 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001318 ceph_osdc_put_request(req);
1319 return;
1320
Sage Weil1b83bef2013-02-25 16:11:12 -08001321bad_put:
1322 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001323bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001324 pr_err("corrupt osd_op_reply got %d %d\n",
1325 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001326 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001327}
1328
Sage Weil6f6c7002011-01-17 20:34:08 -08001329static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001330{
Sage Weilf24e9982009-10-06 11:31:10 -07001331 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001332
Sage Weil6f6c7002011-01-17 20:34:08 -08001333 for (p = rb_first(&osdc->osds); p; p = n) {
1334 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001335
Sage Weil6f6c7002011-01-17 20:34:08 -08001336 n = rb_next(p);
1337 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1338 memcmp(&osd->o_con.peer_addr,
1339 ceph_osd_addr(osdc->osdmap,
1340 osd->o_osd),
1341 sizeof(struct ceph_entity_addr)) != 0)
1342 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001343 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001344}
1345
1346/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001347 * Requeue requests whose mapping to an OSD has changed. If requests map to
1348 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001349 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001350 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001351 */
Sage Weil38d64532011-10-14 13:33:55 -07001352static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001353{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001354 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001355 struct rb_node *p;
1356 int needmap = 0;
1357 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001358
Sage Weil38d64532011-10-14 13:33:55 -07001359 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001360 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001361 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001362 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001363 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001364
1365 /*
1366 * For linger requests that have not yet been
1367 * registered, move them to the linger list; they'll
1368 * be sent to the osd in the loop below. Unregister
1369 * the request before re-registering it as a linger
1370 * request to ensure the __map_request() below
1371 * will decide it needs to be sent.
1372 */
1373 if (req->r_linger && list_empty(&req->r_linger_item)) {
1374 dout("%p tid %llu restart on osd%d\n",
1375 req, req->r_tid,
1376 req->r_osd ? req->r_osd->o_osd : -1);
1377 __unregister_request(osdc, req);
1378 __register_linger_request(osdc, req);
1379 continue;
1380 }
1381
Sage Weil38d64532011-10-14 13:33:55 -07001382 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001383 if (err < 0)
1384 continue; /* error */
1385 if (req->r_osd == NULL) {
1386 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1387 needmap++; /* request a newer map */
1388 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001389 if (!req->r_linger) {
1390 dout("%p tid %llu requeued on osd%d\n", req,
1391 req->r_tid,
1392 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001393 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001394 }
1395 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001396 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001397
1398 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1399 r_linger_item) {
1400 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1401
Sage Weil38d64532011-10-14 13:33:55 -07001402 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001403 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001404 if (err == 0)
1405 continue; /* no change and no osd was specified */
1406 if (err < 0)
1407 continue; /* hrm! */
1408 if (req->r_osd == NULL) {
1409 dout("tid %llu maps to no valid osd\n", req->r_tid);
1410 needmap++; /* request a newer map */
1411 continue;
1412 }
1413
1414 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1415 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001416 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001417 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001418 }
Sage Weilf24e9982009-10-06 11:31:10 -07001419 mutex_unlock(&osdc->request_mutex);
1420
1421 if (needmap) {
1422 dout("%d requests for down osds, need new map\n", needmap);
1423 ceph_monc_request_next_osdmap(&osdc->client->monc);
1424 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001425 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001426}
Sage Weil6f6c7002011-01-17 20:34:08 -08001427
1428
Sage Weilf24e9982009-10-06 11:31:10 -07001429/*
1430 * Process updated osd map.
1431 *
1432 * The message contains any number of incremental and full maps, normally
1433 * indicating some sort of topology change in the cluster. Kick requests
1434 * off to different OSDs as needed.
1435 */
1436void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1437{
1438 void *p, *end, *next;
1439 u32 nr_maps, maplen;
1440 u32 epoch;
1441 struct ceph_osdmap *newmap = NULL, *oldmap;
1442 int err;
1443 struct ceph_fsid fsid;
1444
1445 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1446 p = msg->front.iov_base;
1447 end = p + msg->front.iov_len;
1448
1449 /* verify fsid */
1450 ceph_decode_need(&p, end, sizeof(fsid), bad);
1451 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001452 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1453 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001454
1455 down_write(&osdc->map_sem);
1456
1457 /* incremental maps */
1458 ceph_decode_32_safe(&p, end, nr_maps, bad);
1459 dout(" %d inc maps\n", nr_maps);
1460 while (nr_maps > 0) {
1461 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001462 epoch = ceph_decode_32(&p);
1463 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001464 ceph_decode_need(&p, end, maplen, bad);
1465 next = p + maplen;
1466 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1467 dout("applying incremental map %u len %d\n",
1468 epoch, maplen);
1469 newmap = osdmap_apply_incremental(&p, next,
1470 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001471 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001472 if (IS_ERR(newmap)) {
1473 err = PTR_ERR(newmap);
1474 goto bad;
1475 }
Sage Weil30dc6382009-12-21 14:49:37 -08001476 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001477 if (newmap != osdc->osdmap) {
1478 ceph_osdmap_destroy(osdc->osdmap);
1479 osdc->osdmap = newmap;
1480 }
Sage Weil38d64532011-10-14 13:33:55 -07001481 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001482 } else {
1483 dout("ignoring incremental map %u len %d\n",
1484 epoch, maplen);
1485 }
1486 p = next;
1487 nr_maps--;
1488 }
1489 if (newmap)
1490 goto done;
1491
1492 /* full maps */
1493 ceph_decode_32_safe(&p, end, nr_maps, bad);
1494 dout(" %d full maps\n", nr_maps);
1495 while (nr_maps) {
1496 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001497 epoch = ceph_decode_32(&p);
1498 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001499 ceph_decode_need(&p, end, maplen, bad);
1500 if (nr_maps > 1) {
1501 dout("skipping non-latest full map %u len %d\n",
1502 epoch, maplen);
1503 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1504 dout("skipping full map %u len %d, "
1505 "older than our %u\n", epoch, maplen,
1506 osdc->osdmap->epoch);
1507 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001508 int skipped_map = 0;
1509
Sage Weilf24e9982009-10-06 11:31:10 -07001510 dout("taking full map %u len %d\n", epoch, maplen);
1511 newmap = osdmap_decode(&p, p+maplen);
1512 if (IS_ERR(newmap)) {
1513 err = PTR_ERR(newmap);
1514 goto bad;
1515 }
Sage Weil30dc6382009-12-21 14:49:37 -08001516 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001517 oldmap = osdc->osdmap;
1518 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001519 if (oldmap) {
1520 if (oldmap->epoch + 1 < newmap->epoch)
1521 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001522 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001523 }
1524 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001525 }
1526 p += maplen;
1527 nr_maps--;
1528 }
1529
1530done:
1531 downgrade_write(&osdc->map_sem);
1532 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001533
1534 /*
1535 * subscribe to subsequent osdmap updates if full to ensure
1536 * we find out when we are no longer full and stop returning
1537 * ENOSPC.
1538 */
1539 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1540 ceph_monc_request_next_osdmap(&osdc->client->monc);
1541
Alex Elderf9d25192013-02-15 11:42:29 -06001542 mutex_lock(&osdc->request_mutex);
1543 __send_queued(osdc);
1544 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001545 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001546 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001547 return;
1548
1549bad:
1550 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001551 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001552 up_write(&osdc->map_sem);
1553 return;
1554}
1555
Sage Weilf24e9982009-10-06 11:31:10 -07001556/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001557 * watch/notify callback event infrastructure
1558 *
1559 * These callbacks are used both for watch and notify operations.
1560 */
1561static void __release_event(struct kref *kref)
1562{
1563 struct ceph_osd_event *event =
1564 container_of(kref, struct ceph_osd_event, kref);
1565
1566 dout("__release_event %p\n", event);
1567 kfree(event);
1568}
1569
1570static void get_event(struct ceph_osd_event *event)
1571{
1572 kref_get(&event->kref);
1573}
1574
1575void ceph_osdc_put_event(struct ceph_osd_event *event)
1576{
1577 kref_put(&event->kref, __release_event);
1578}
1579EXPORT_SYMBOL(ceph_osdc_put_event);
1580
1581static void __insert_event(struct ceph_osd_client *osdc,
1582 struct ceph_osd_event *new)
1583{
1584 struct rb_node **p = &osdc->event_tree.rb_node;
1585 struct rb_node *parent = NULL;
1586 struct ceph_osd_event *event = NULL;
1587
1588 while (*p) {
1589 parent = *p;
1590 event = rb_entry(parent, struct ceph_osd_event, node);
1591 if (new->cookie < event->cookie)
1592 p = &(*p)->rb_left;
1593 else if (new->cookie > event->cookie)
1594 p = &(*p)->rb_right;
1595 else
1596 BUG();
1597 }
1598
1599 rb_link_node(&new->node, parent, p);
1600 rb_insert_color(&new->node, &osdc->event_tree);
1601}
1602
1603static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1604 u64 cookie)
1605{
1606 struct rb_node **p = &osdc->event_tree.rb_node;
1607 struct rb_node *parent = NULL;
1608 struct ceph_osd_event *event = NULL;
1609
1610 while (*p) {
1611 parent = *p;
1612 event = rb_entry(parent, struct ceph_osd_event, node);
1613 if (cookie < event->cookie)
1614 p = &(*p)->rb_left;
1615 else if (cookie > event->cookie)
1616 p = &(*p)->rb_right;
1617 else
1618 return event;
1619 }
1620 return NULL;
1621}
1622
1623static void __remove_event(struct ceph_osd_event *event)
1624{
1625 struct ceph_osd_client *osdc = event->osdc;
1626
1627 if (!RB_EMPTY_NODE(&event->node)) {
1628 dout("__remove_event removed %p\n", event);
1629 rb_erase(&event->node, &osdc->event_tree);
1630 ceph_osdc_put_event(event);
1631 } else {
1632 dout("__remove_event didn't remove %p\n", event);
1633 }
1634}
1635
1636int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1637 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001638 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001639{
1640 struct ceph_osd_event *event;
1641
1642 event = kmalloc(sizeof(*event), GFP_NOIO);
1643 if (!event)
1644 return -ENOMEM;
1645
1646 dout("create_event %p\n", event);
1647 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001648 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001649 event->data = data;
1650 event->osdc = osdc;
1651 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001652 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001653 kref_init(&event->kref); /* one ref for us */
1654 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001655
1656 spin_lock(&osdc->event_lock);
1657 event->cookie = ++osdc->event_count;
1658 __insert_event(osdc, event);
1659 spin_unlock(&osdc->event_lock);
1660
1661 *pevent = event;
1662 return 0;
1663}
1664EXPORT_SYMBOL(ceph_osdc_create_event);
1665
1666void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1667{
1668 struct ceph_osd_client *osdc = event->osdc;
1669
1670 dout("cancel_event %p\n", event);
1671 spin_lock(&osdc->event_lock);
1672 __remove_event(event);
1673 spin_unlock(&osdc->event_lock);
1674 ceph_osdc_put_event(event); /* caller's */
1675}
1676EXPORT_SYMBOL(ceph_osdc_cancel_event);
1677
1678
1679static void do_event_work(struct work_struct *work)
1680{
1681 struct ceph_osd_event_work *event_work =
1682 container_of(work, struct ceph_osd_event_work, work);
1683 struct ceph_osd_event *event = event_work->event;
1684 u64 ver = event_work->ver;
1685 u64 notify_id = event_work->notify_id;
1686 u8 opcode = event_work->opcode;
1687
1688 dout("do_event_work completing %p\n", event);
1689 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001690 dout("do_event_work completed %p\n", event);
1691 ceph_osdc_put_event(event);
1692 kfree(event_work);
1693}
1694
1695
1696/*
1697 * Process osd watch notifications
1698 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001699static void handle_watch_notify(struct ceph_osd_client *osdc,
1700 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001701{
1702 void *p, *end;
1703 u8 proto_ver;
1704 u64 cookie, ver, notify_id;
1705 u8 opcode;
1706 struct ceph_osd_event *event;
1707 struct ceph_osd_event_work *event_work;
1708
1709 p = msg->front.iov_base;
1710 end = p + msg->front.iov_len;
1711
1712 ceph_decode_8_safe(&p, end, proto_ver, bad);
1713 ceph_decode_8_safe(&p, end, opcode, bad);
1714 ceph_decode_64_safe(&p, end, cookie, bad);
1715 ceph_decode_64_safe(&p, end, ver, bad);
1716 ceph_decode_64_safe(&p, end, notify_id, bad);
1717
1718 spin_lock(&osdc->event_lock);
1719 event = __find_event(osdc, cookie);
1720 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001721 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001722 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001723 }
1724 spin_unlock(&osdc->event_lock);
1725 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1726 cookie, ver, event);
1727 if (event) {
1728 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001729 if (!event_work) {
1730 dout("ERROR: could not allocate event_work\n");
1731 goto done_err;
1732 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001733 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001734 event_work->event = event;
1735 event_work->ver = ver;
1736 event_work->notify_id = notify_id;
1737 event_work->opcode = opcode;
1738 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1739 dout("WARNING: failed to queue notify event work\n");
1740 goto done_err;
1741 }
1742 }
1743
1744 return;
1745
1746done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001747 ceph_osdc_put_event(event);
1748 return;
1749
1750bad:
1751 pr_err("osdc handle_watch_notify corrupt msg\n");
1752 return;
1753}
1754
Alex Elder70636772013-03-04 18:29:06 -06001755static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1756 struct ceph_osd_data *osd_data)
1757{
1758 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1759 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1760 if (osd_data->length)
1761 ceph_msg_data_set_pages(msg, osd_data->pages,
1762 osd_data->length, osd_data->alignment);
Alex Elder9a5e6d02013-03-08 13:35:36 -06001763 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGELIST) {
1764 BUG_ON(!osd_data->pagelist->length);
1765 ceph_msg_data_set_pagelist(msg, osd_data->pagelist);
Alex Elder70636772013-03-04 18:29:06 -06001766#ifdef CONFIG_BLOCK
1767 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1768 ceph_msg_data_set_bio(msg, osd_data->bio);
1769#endif
1770 } else {
1771 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1772 }
1773}
1774
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001775/*
Sage Weilf24e9982009-10-06 11:31:10 -07001776 * Register request, send initial attempt.
1777 */
1778int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1779 struct ceph_osd_request *req,
1780 bool nofail)
1781{
Sage Weilc1ea8822009-10-08 16:55:47 -07001782 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001783
Alex Elder70636772013-03-04 18:29:06 -06001784 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001785
Alex Elder70636772013-03-04 18:29:06 -06001786 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1787 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Sage Weilf24e9982009-10-06 11:31:10 -07001788
1789 register_request(osdc, req);
1790
1791 down_read(&osdc->map_sem);
1792 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001793 /*
1794 * a racing kick_requests() may have sent the message for us
1795 * while we dropped request_mutex above, so only send now if
1796 * the request still han't been touched yet.
1797 */
1798 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001799 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001800 if (rc < 0) {
1801 if (nofail) {
1802 dout("osdc_start_request failed map, "
1803 " will retry %lld\n", req->r_tid);
1804 rc = 0;
1805 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001806 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001807 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001808 if (req->r_osd == NULL) {
1809 dout("send_request %p no up osds in pg\n", req);
1810 ceph_monc_request_next_osdmap(&osdc->client->monc);
1811 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001812 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001813 }
Sage Weil56e925b2012-01-03 12:34:34 -08001814 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001815 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001816
1817out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001818 mutex_unlock(&osdc->request_mutex);
1819 up_read(&osdc->map_sem);
1820 return rc;
1821}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001822EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001823
1824/*
1825 * wait for a request to complete
1826 */
1827int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1828 struct ceph_osd_request *req)
1829{
1830 int rc;
1831
1832 rc = wait_for_completion_interruptible(&req->r_completion);
1833 if (rc < 0) {
1834 mutex_lock(&osdc->request_mutex);
1835 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001836 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001837 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001838 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001839 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001840 return rc;
1841 }
1842
1843 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1844 return req->r_result;
1845}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001846EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001847
1848/*
1849 * sync - wait for all in-flight requests to flush. avoid starvation.
1850 */
1851void ceph_osdc_sync(struct ceph_osd_client *osdc)
1852{
1853 struct ceph_osd_request *req;
1854 u64 last_tid, next_tid = 0;
1855
1856 mutex_lock(&osdc->request_mutex);
1857 last_tid = osdc->last_tid;
1858 while (1) {
1859 req = __lookup_request_ge(osdc, next_tid);
1860 if (!req)
1861 break;
1862 if (req->r_tid > last_tid)
1863 break;
1864
1865 next_tid = req->r_tid + 1;
1866 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1867 continue;
1868
1869 ceph_osdc_get_request(req);
1870 mutex_unlock(&osdc->request_mutex);
1871 dout("sync waiting on tid %llu (last is %llu)\n",
1872 req->r_tid, last_tid);
1873 wait_for_completion(&req->r_safe_completion);
1874 mutex_lock(&osdc->request_mutex);
1875 ceph_osdc_put_request(req);
1876 }
1877 mutex_unlock(&osdc->request_mutex);
1878 dout("sync done (thru tid %llu)\n", last_tid);
1879}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001880EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001881
1882/*
1883 * init, shutdown
1884 */
1885int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1886{
1887 int err;
1888
1889 dout("init\n");
1890 osdc->client = client;
1891 osdc->osdmap = NULL;
1892 init_rwsem(&osdc->map_sem);
1893 init_completion(&osdc->map_waiters);
1894 osdc->last_requested_map = 0;
1895 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001896 osdc->last_tid = 0;
1897 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001898 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001899 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001900 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001901 INIT_LIST_HEAD(&osdc->req_unsent);
1902 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001903 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001904 osdc->num_requests = 0;
1905 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001906 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001907 spin_lock_init(&osdc->event_lock);
1908 osdc->event_tree = RB_ROOT;
1909 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001910
1911 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001912 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001913
Sage Weil5f44f142009-11-18 14:52:18 -08001914 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001915 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1916 sizeof(struct ceph_osd_request));
1917 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001918 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001919
Sage Weild50b4092012-07-09 14:22:34 -07001920 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1921 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001922 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001923 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001924 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001925 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001926 OSD_OPREPLY_FRONT_LEN, 10, true,
1927 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001928 if (err < 0)
1929 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001930
1931 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1932 if (IS_ERR(osdc->notify_wq)) {
1933 err = PTR_ERR(osdc->notify_wq);
1934 osdc->notify_wq = NULL;
1935 goto out_msgpool;
1936 }
Sage Weilf24e9982009-10-06 11:31:10 -07001937 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001938
Sage Weilc16e7862010-03-01 13:02:00 -08001939out_msgpool:
1940 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001941out_mempool:
1942 mempool_destroy(osdc->req_mempool);
1943out:
1944 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001945}
1946
1947void ceph_osdc_stop(struct ceph_osd_client *osdc)
1948{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001949 flush_workqueue(osdc->notify_wq);
1950 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001951 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001952 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001953 if (osdc->osdmap) {
1954 ceph_osdmap_destroy(osdc->osdmap);
1955 osdc->osdmap = NULL;
1956 }
Sage Weilaca420b2011-08-31 14:45:53 -07001957 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001958 mempool_destroy(osdc->req_mempool);
1959 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001960 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001961}
1962
1963/*
1964 * Read some contiguous pages. If we cross a stripe boundary, shorten
1965 * *plen. Return number of bytes read, or error.
1966 */
1967int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1968 struct ceph_vino vino, struct ceph_file_layout *layout,
1969 u64 off, u64 *plen,
1970 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001971 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001972{
1973 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06001974 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07001975 int rc = 0;
1976
1977 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1978 vino.snap, off, *plen);
1979 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1980 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1981 NULL, 0, truncate_seq, truncate_size, NULL,
Alex Elder153e5162013-03-01 18:00:15 -06001982 false);
Sage Weil68162822012-09-24 21:01:02 -07001983 if (IS_ERR(req))
1984 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001985
1986 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06001987
1988 osd_data = &req->r_data_in;
1989 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
1990 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06001991 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06001992 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07001993
Alex Eldere0c59482013-03-07 15:38:25 -06001994 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
1995 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001996
1997 rc = ceph_osdc_start_request(osdc, req, false);
1998 if (!rc)
1999 rc = ceph_osdc_wait_request(osdc, req);
2000
2001 ceph_osdc_put_request(req);
2002 dout("readpages result %d\n", rc);
2003 return rc;
2004}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002005EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002006
2007/*
2008 * do a synchronous write on N pages
2009 */
2010int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2011 struct ceph_file_layout *layout,
2012 struct ceph_snap_context *snapc,
2013 u64 off, u64 len,
2014 u32 truncate_seq, u64 truncate_size,
2015 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002016 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002017{
2018 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002019 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002020 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002021 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002022
2023 BUG_ON(vino.snap != CEPH_NOSNAP);
2024 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2025 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002026 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06002027 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07002028 truncate_seq, truncate_size, mtime,
Alex Elder153e5162013-03-01 18:00:15 -06002029 true);
Sage Weil68162822012-09-24 21:01:02 -07002030 if (IS_ERR(req))
2031 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002032
2033 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002034 osd_data = &req->r_data_out;
2035 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2036 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002037 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002038 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002039 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002040
Alex Elder87f979d2013-02-15 11:42:29 -06002041 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002042 if (!rc)
2043 rc = ceph_osdc_wait_request(osdc, req);
2044
2045 ceph_osdc_put_request(req);
2046 if (rc == 0)
2047 rc = len;
2048 dout("writepages result %d\n", rc);
2049 return rc;
2050}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002051EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002052
2053/*
2054 * handle incoming message
2055 */
2056static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2057{
2058 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002059 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002060 int type = le16_to_cpu(msg->hdr.type);
2061
2062 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002063 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002064 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002065
2066 switch (type) {
2067 case CEPH_MSG_OSD_MAP:
2068 ceph_osdc_handle_map(osdc, msg);
2069 break;
2070 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002071 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002072 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002073 case CEPH_MSG_WATCH_NOTIFY:
2074 handle_watch_notify(osdc, msg);
2075 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002076
2077 default:
2078 pr_err("received unknown message type %d %s\n", type,
2079 ceph_msg_type_name(type));
2080 }
Sage Weil4a32f932010-06-13 10:27:53 -07002081out:
Sage Weilf24e9982009-10-06 11:31:10 -07002082 ceph_msg_put(msg);
2083}
2084
Sage Weil5b3a4db2010-02-19 21:43:23 -08002085/*
Sage Weil21b667f2010-03-04 10:22:59 -08002086 * lookup and return message for incoming reply. set up reply message
2087 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002088 */
2089static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002090 struct ceph_msg_header *hdr,
2091 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002092{
2093 struct ceph_osd *osd = con->private;
2094 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002095 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002096 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002097 int front = le32_to_cpu(hdr->front_len);
2098 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002099 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002100
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002101 tid = le64_to_cpu(hdr->tid);
2102 mutex_lock(&osdc->request_mutex);
2103 req = __lookup_request(osdc, tid);
2104 if (!req) {
2105 *skip = 1;
2106 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002107 dout("get_reply unknown tid %llu from osd%d\n", tid,
2108 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002109 goto out;
2110 }
Sage Weilc16e7862010-03-01 13:02:00 -08002111
2112 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002113 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002114 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002115 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002116 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002117 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002118 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002119
Sage Weilc16e7862010-03-01 13:02:00 -08002120 if (front > req->r_reply->front.iov_len) {
2121 pr_warning("get_reply front %d > preallocated %d\n",
2122 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002123 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002124 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002125 goto out;
2126 ceph_msg_put(req->r_reply);
2127 req->r_reply = m;
2128 }
2129 m = ceph_msg_get(req->r_reply);
2130
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002131 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002132 struct ceph_osd_data *osd_data = &req->r_data_in;
2133
2134 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002135 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002136 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002137
Alex Eldere0c59482013-03-07 15:38:25 -06002138 pr_warning("tid %lld reply has %d bytes "
2139 "we had only %llu bytes ready\n",
2140 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002141 *skip = 1;
2142 ceph_msg_put(m);
2143 m = NULL;
2144 goto out;
2145 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002146 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002147 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002148 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002149 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002150 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002151
2152out:
2153 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002154 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002155
2156}
2157
2158static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2159 struct ceph_msg_header *hdr,
2160 int *skip)
2161{
2162 struct ceph_osd *osd = con->private;
2163 int type = le16_to_cpu(hdr->type);
2164 int front = le32_to_cpu(hdr->front_len);
2165
Alex Elder1c20f2d2012-06-04 14:43:32 -05002166 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002167 switch (type) {
2168 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002169 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002170 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002171 case CEPH_MSG_OSD_OPREPLY:
2172 return get_reply(con, hdr, skip);
2173 default:
2174 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2175 osd->o_osd);
2176 *skip = 1;
2177 return NULL;
2178 }
Sage Weilf24e9982009-10-06 11:31:10 -07002179}
2180
2181/*
2182 * Wrappers to refcount containing ceph_osd struct
2183 */
2184static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2185{
2186 struct ceph_osd *osd = con->private;
2187 if (get_osd(osd))
2188 return con;
2189 return NULL;
2190}
2191
2192static void put_osd_con(struct ceph_connection *con)
2193{
2194 struct ceph_osd *osd = con->private;
2195 put_osd(osd);
2196}
2197
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002198/*
2199 * authentication
2200 */
Alex Eldera3530df2012-05-16 15:16:39 -05002201/*
2202 * Note: returned pointer is the address of a structure that's
2203 * managed separately. Caller must *not* attempt to free it.
2204 */
2205static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002206 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002207{
2208 struct ceph_osd *o = con->private;
2209 struct ceph_osd_client *osdc = o->o_osdc;
2210 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002211 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002212
Alex Elder74f18692012-05-16 15:16:39 -05002213 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002214 if (ac->ops && ac->ops->destroy_authorizer)
2215 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002216 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002217 }
Alex Eldera2556512012-05-16 15:16:39 -05002218 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002219 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2220 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002221 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002222 return ERR_PTR(ret);
Sage Weil0bed9b52013-03-25 10:26:01 -07002223 } else if (ac->ops && ac->ops->update_authorizer) {
2224 int ret = ac->ops->update_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2225 auth);
2226 if (ret)
2227 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002228 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002229 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002230
Alex Eldera3530df2012-05-16 15:16:39 -05002231 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002232}
2233
2234
2235static int verify_authorizer_reply(struct ceph_connection *con, int len)
2236{
2237 struct ceph_osd *o = con->private;
2238 struct ceph_osd_client *osdc = o->o_osdc;
2239 struct ceph_auth_client *ac = osdc->client->monc.auth;
2240
Alex Eldera2556512012-05-16 15:16:39 -05002241 /*
2242 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2243 * XXX which do we do: succeed or fail?
2244 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002245 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002246}
2247
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002248static int invalidate_authorizer(struct ceph_connection *con)
2249{
2250 struct ceph_osd *o = con->private;
2251 struct ceph_osd_client *osdc = o->o_osdc;
2252 struct ceph_auth_client *ac = osdc->client->monc.auth;
2253
Alex Eldera2556512012-05-16 15:16:39 -05002254 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002255 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2256
2257 return ceph_monc_validate_auth(&osdc->client->monc);
2258}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002259
Tobias Klauser9e327892010-05-20 10:40:19 +02002260static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002261 .get = get_osd_con,
2262 .put = put_osd_con,
2263 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002264 .get_authorizer = get_authorizer,
2265 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002266 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002267 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002268 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002269};