blob: ce34faaa453f14faa03b49cbcab5bbd7c8cba80d [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);
Alex Elderc8858372012-11-13 21:11:15 -0600141 ceph_pagelist_release(&req->r_trail);
Sage Weil415e49a2009-12-07 13:37:03 -0800142 if (req->r_mempool)
143 mempool_free(req, req->r_osdc->req_mempool);
144 else
145 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700146}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700147EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700148
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700149struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700150 struct ceph_snap_context *snapc,
Sage Weil1b83bef2013-02-25 16:11:12 -0800151 unsigned int num_ops,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700152 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600153 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700154{
155 struct ceph_osd_request *req;
156 struct ceph_msg *msg;
Sage Weil1b83bef2013-02-25 16:11:12 -0800157 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700158
Sage Weil1b83bef2013-02-25 16:11:12 -0800159 msg_size = 4 + 4 + 8 + 8 + 4+8;
160 msg_size += 2 + 4 + 8 + 4 + 4; /* oloc */
161 msg_size += 1 + 8 + 4 + 4; /* pg_t */
162 msg_size += 4 + MAX_OBJ_NAME_SIZE;
163 msg_size += 2 + num_ops*sizeof(struct ceph_osd_op);
164 msg_size += 8; /* snapid */
165 msg_size += 8; /* snap_seq */
166 msg_size += 8 * (snapc ? snapc->num_snaps : 0); /* snaps */
167 msg_size += 4;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700168
169 if (use_mempool) {
170 req = mempool_alloc(osdc->req_mempool, gfp_flags);
171 memset(req, 0, sizeof(*req));
172 } else {
173 req = kzalloc(sizeof(*req), gfp_flags);
174 }
175 if (req == NULL)
176 return NULL;
177
178 req->r_osdc = osdc;
179 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700180
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700181 kref_init(&req->r_kref);
182 init_completion(&req->r_completion);
183 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600184 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700185 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700186 INIT_LIST_HEAD(&req->r_linger_item);
187 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700188 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700189 INIT_LIST_HEAD(&req->r_osd_item);
190
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700191 /* create reply message */
192 if (use_mempool)
193 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
194 else
195 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700196 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700197 if (!msg) {
198 ceph_osdc_put_request(req);
199 return NULL;
200 }
201 req->r_reply = msg;
202
Alex Elder0fff87e2013-02-14 12:16:43 -0600203 req->r_data_in.type = CEPH_OSD_DATA_TYPE_NONE;
204 req->r_data_out.type = CEPH_OSD_DATA_TYPE_NONE;
Alex Elderc8858372012-11-13 21:11:15 -0600205 ceph_pagelist_init(&req->r_trail);
Sage Weild50b4092012-07-09 14:22:34 -0700206
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700207 /* create request message; allow space for oid */
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700208 if (use_mempool)
209 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
210 else
Sage Weilb61c2762011-08-09 15:03:46 -0700211 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700212 if (!msg) {
213 ceph_osdc_put_request(req);
214 return NULL;
215 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700216
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700217 memset(msg->front.iov_base, 0, msg->front.iov_len);
218
219 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700220
221 return req;
222}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700223EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700224
Alex Elder175face2013-03-08 13:35:36 -0600225static u64 osd_req_encode_op(struct ceph_osd_request *req,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700226 struct ceph_osd_op *dst,
227 struct ceph_osd_req_op *src)
228{
Alex Elder175face2013-03-08 13:35:36 -0600229 u64 out_data_len = 0;
230 u64 tmp;
231
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700232 dst->op = cpu_to_le16(src->op);
233
Alex Elder065a68f2012-04-20 15:49:43 -0500234 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600235 case CEPH_OSD_OP_STAT:
236 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700237 case CEPH_OSD_OP_READ:
238 case CEPH_OSD_OP_WRITE:
Alex Elder175face2013-03-08 13:35:36 -0600239 if (src->op == CEPH_OSD_OP_WRITE)
240 out_data_len = src->extent.length;
241 dst->extent.offset = cpu_to_le64(src->extent.offset);
242 dst->extent.length = cpu_to_le64(src->extent.length);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700243 dst->extent.truncate_size =
244 cpu_to_le64(src->extent.truncate_size);
245 dst->extent.truncate_seq =
246 cpu_to_le32(src->extent.truncate_seq);
247 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700248 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700249 dst->cls.class_len = src->cls.class_len;
250 dst->cls.method_len = src->cls.method_len;
251 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
252
Alex Elder175face2013-03-08 13:35:36 -0600253 tmp = req->r_trail.length;
Alex Elderc8858372012-11-13 21:11:15 -0600254 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700255 src->cls.class_len);
Alex Elderc8858372012-11-13 21:11:15 -0600256 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700257 src->cls.method_len);
Alex Elderc8858372012-11-13 21:11:15 -0600258 ceph_pagelist_append(&req->r_trail, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700259 src->cls.indata_len);
Alex Elder175face2013-03-08 13:35:36 -0600260 out_data_len = req->r_trail.length - tmp;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700261 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700262 case CEPH_OSD_OP_STARTSYNC:
263 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700264 case CEPH_OSD_OP_NOTIFY_ACK:
265 case CEPH_OSD_OP_WATCH:
266 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
267 dst->watch.ver = cpu_to_le64(src->watch.ver);
268 dst->watch.flag = src->watch.flag;
269 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700270 default:
Alex Elder8f63ca22013-03-04 11:08:29 -0600271 pr_err("unrecognized osd opcode %d\n", src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700272 WARN_ON(1);
273 break;
Alex Elder4c464592013-02-15 11:42:30 -0600274 case CEPH_OSD_OP_MAPEXT:
275 case CEPH_OSD_OP_MASKTRUNC:
276 case CEPH_OSD_OP_SPARSE_READ:
Alex Eldera9f36c32013-02-15 11:42:30 -0600277 case CEPH_OSD_OP_NOTIFY:
Alex Elder4c464592013-02-15 11:42:30 -0600278 case CEPH_OSD_OP_ASSERT_VER:
279 case CEPH_OSD_OP_WRITEFULL:
280 case CEPH_OSD_OP_TRUNCATE:
281 case CEPH_OSD_OP_ZERO:
282 case CEPH_OSD_OP_DELETE:
283 case CEPH_OSD_OP_APPEND:
284 case CEPH_OSD_OP_SETTRUNC:
285 case CEPH_OSD_OP_TRIMTRUNC:
286 case CEPH_OSD_OP_TMAPUP:
287 case CEPH_OSD_OP_TMAPPUT:
288 case CEPH_OSD_OP_TMAPGET:
289 case CEPH_OSD_OP_CREATE:
Alex Eldera9f36c32013-02-15 11:42:30 -0600290 case CEPH_OSD_OP_ROLLBACK:
Alex Elder4c464592013-02-15 11:42:30 -0600291 case CEPH_OSD_OP_OMAPGETKEYS:
292 case CEPH_OSD_OP_OMAPGETVALS:
293 case CEPH_OSD_OP_OMAPGETHEADER:
294 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
295 case CEPH_OSD_OP_MODE_RD:
296 case CEPH_OSD_OP_OMAPSETVALS:
297 case CEPH_OSD_OP_OMAPSETHEADER:
298 case CEPH_OSD_OP_OMAPCLEAR:
299 case CEPH_OSD_OP_OMAPRMKEYS:
300 case CEPH_OSD_OP_OMAP_CMP:
301 case CEPH_OSD_OP_CLONERANGE:
302 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
303 case CEPH_OSD_OP_SRC_CMPXATTR:
Alex Eldera9f36c32013-02-15 11:42:30 -0600304 case CEPH_OSD_OP_GETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600305 case CEPH_OSD_OP_GETXATTRS:
Alex Eldera9f36c32013-02-15 11:42:30 -0600306 case CEPH_OSD_OP_CMPXATTR:
307 case CEPH_OSD_OP_SETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600308 case CEPH_OSD_OP_SETXATTRS:
309 case CEPH_OSD_OP_RESETXATTRS:
310 case CEPH_OSD_OP_RMXATTR:
311 case CEPH_OSD_OP_PULL:
312 case CEPH_OSD_OP_PUSH:
313 case CEPH_OSD_OP_BALANCEREADS:
314 case CEPH_OSD_OP_UNBALANCEREADS:
315 case CEPH_OSD_OP_SCRUB:
316 case CEPH_OSD_OP_SCRUB_RESERVE:
317 case CEPH_OSD_OP_SCRUB_UNRESERVE:
318 case CEPH_OSD_OP_SCRUB_STOP:
319 case CEPH_OSD_OP_SCRUB_MAP:
320 case CEPH_OSD_OP_WRLOCK:
321 case CEPH_OSD_OP_WRUNLOCK:
322 case CEPH_OSD_OP_RDLOCK:
323 case CEPH_OSD_OP_RDUNLOCK:
324 case CEPH_OSD_OP_UPLOCK:
325 case CEPH_OSD_OP_DNLOCK:
326 case CEPH_OSD_OP_PGLS:
327 case CEPH_OSD_OP_PGLS_FILTER:
328 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600329 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600330 WARN_ON(1);
331 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700332 }
333 dst->payload_len = cpu_to_le32(src->payload_len);
Alex Elder175face2013-03-08 13:35:36 -0600334
335 return out_data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700336}
337
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700338/*
339 * build new request AND message
340 *
341 */
342void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elder175face2013-03-08 13:35:36 -0600343 u64 off, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700344 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600345 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600346 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700347{
348 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700349 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700350 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800351 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700352 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600353 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700354 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355
Sage Weil1b83bef2013-02-25 16:11:12 -0800356 req->r_num_ops = num_ops;
357 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700358 req->r_snapc = ceph_get_snap_context(snapc);
359
Sage Weil1b83bef2013-02-25 16:11:12 -0800360 /* encode request */
361 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700362
Sage Weil1b83bef2013-02-25 16:11:12 -0800363 p = msg->front.iov_base;
364 ceph_encode_32(&p, 1); /* client_inc is always 1 */
365 req->r_request_osdmap_epoch = p;
366 p += 4;
367 req->r_request_flags = p;
368 p += 4;
369 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
370 ceph_encode_timespec(p, mtime);
371 p += sizeof(struct ceph_timespec);
372 req->r_request_reassert_version = p;
373 p += sizeof(struct ceph_eversion); /* will get filled in */
374
375 /* oloc */
376 ceph_encode_8(&p, 4);
377 ceph_encode_8(&p, 4);
378 ceph_encode_32(&p, 8 + 4 + 4);
379 req->r_request_pool = p;
380 p += 8;
381 ceph_encode_32(&p, -1); /* preferred */
382 ceph_encode_32(&p, 0); /* key len */
383
384 ceph_encode_8(&p, 1);
385 req->r_request_pgid = p;
386 p += 8 + 4;
387 ceph_encode_32(&p, -1); /* preferred */
388
389 /* oid */
390 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600391 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800392 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600393 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700394
Alex Elder175face2013-03-08 13:35:36 -0600395 /* ops--can imply data */
Sage Weil1b83bef2013-02-25 16:11:12 -0800396 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700397 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800398 req->r_request_ops = p;
Alex Elder175face2013-03-08 13:35:36 -0600399 data_len = 0;
Sage Weil1b83bef2013-02-25 16:11:12 -0800400 for (i = 0; i < num_ops; i++, src_op++) {
Alex Elder175face2013-03-08 13:35:36 -0600401 data_len += osd_req_encode_op(req, p, src_op);
Sage Weil1b83bef2013-02-25 16:11:12 -0800402 p += sizeof(struct ceph_osd_op);
403 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700404
Sage Weil1b83bef2013-02-25 16:11:12 -0800405 /* snaps */
406 ceph_encode_64(&p, req->r_snapid);
407 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
408 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
409 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700410 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800411 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700412 }
413 }
414
Sage Weil1b83bef2013-02-25 16:11:12 -0800415 req->r_request_attempts = p;
416 p += 4;
417
Alex Elder175face2013-03-08 13:35:36 -0600418 /* data */
419 if (flags & CEPH_OSD_FLAG_WRITE)
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700420 req->r_request->hdr.data_off = cpu_to_le16(off);
Alex Elderf44246e2013-02-14 12:16:43 -0600421 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800422
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700423 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
424 msg_size = p - msg->front.iov_base;
425 msg->front.iov_len = msg_size;
426 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800427
428 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
429 num_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700430 return;
431}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700432EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700433
Sage Weilf24e9982009-10-06 11:31:10 -0700434/*
435 * build new request AND message, calculate layout, and adjust file
436 * extent as needed.
437 *
438 * if the file was recently truncated, we include information about its
439 * old and new size so that the object can be updated appropriately. (we
440 * avoid synchronously deleting truncated objects because it's slow.)
441 *
442 * if @do_sync, include a 'startsync' command so that the osd will flush
443 * data quickly.
444 */
445struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
446 struct ceph_file_layout *layout,
447 struct ceph_vino vino,
448 u64 off, u64 *plen,
449 int opcode, int flags,
450 struct ceph_snap_context *snapc,
451 int do_sync,
452 u32 truncate_seq,
453 u64 truncate_size,
454 struct timespec *mtime,
Alex Elder153e5162013-03-01 18:00:15 -0600455 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700456{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600457 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700458 struct ceph_osd_request *req;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600459 unsigned int num_op = 1;
Alex Elder47a05812013-02-15 22:10:17 -0600460 u64 bno = 0;
Sage Weil68162822012-09-24 21:01:02 -0700461 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700462
Alex Elderae7ca4a32012-11-13 21:11:15 -0600463 memset(&ops, 0, sizeof ops);
464
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700465 ops[0].op = opcode;
466 ops[0].extent.truncate_seq = truncate_seq;
467 ops[0].extent.truncate_size = truncate_size;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700468
469 if (do_sync) {
470 ops[1].op = CEPH_OSD_OP_STARTSYNC;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600471 num_op++;
472 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700473
Alex Elderae7ca4a32012-11-13 21:11:15 -0600474 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
475 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700476 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700477 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600478 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700479
480 /* calculate max write size */
Alex Elder60cf5992013-02-15 22:10:17 -0600481 r = calc_layout(layout, off, plen, ops, &bno);
Alex Elder3ff5f382013-02-15 22:10:17 -0600482 if (r < 0) {
483 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700484 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600485 }
Sage Weilf24e9982009-10-06 11:31:10 -0700486 req->r_file_layout = *layout; /* keep a copy */
487
Alex Elderdbe0fc42013-02-15 22:10:17 -0600488 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
489 req->r_oid_len = strlen(req->r_oid);
490
Alex Elder175face2013-03-08 13:35:36 -0600491 ceph_osdc_build_request(req, off, num_op, ops,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600492 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700493
Sage Weilf24e9982009-10-06 11:31:10 -0700494 return req;
495}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700496EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700497
498/*
499 * We keep osd requests in an rbtree, sorted by ->r_tid.
500 */
501static void __insert_request(struct ceph_osd_client *osdc,
502 struct ceph_osd_request *new)
503{
504 struct rb_node **p = &osdc->requests.rb_node;
505 struct rb_node *parent = NULL;
506 struct ceph_osd_request *req = NULL;
507
508 while (*p) {
509 parent = *p;
510 req = rb_entry(parent, struct ceph_osd_request, r_node);
511 if (new->r_tid < req->r_tid)
512 p = &(*p)->rb_left;
513 else if (new->r_tid > req->r_tid)
514 p = &(*p)->rb_right;
515 else
516 BUG();
517 }
518
519 rb_link_node(&new->r_node, parent, p);
520 rb_insert_color(&new->r_node, &osdc->requests);
521}
522
523static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
524 u64 tid)
525{
526 struct ceph_osd_request *req;
527 struct rb_node *n = osdc->requests.rb_node;
528
529 while (n) {
530 req = rb_entry(n, struct ceph_osd_request, r_node);
531 if (tid < req->r_tid)
532 n = n->rb_left;
533 else if (tid > req->r_tid)
534 n = n->rb_right;
535 else
536 return req;
537 }
538 return NULL;
539}
540
541static struct ceph_osd_request *
542__lookup_request_ge(struct ceph_osd_client *osdc,
543 u64 tid)
544{
545 struct ceph_osd_request *req;
546 struct rb_node *n = osdc->requests.rb_node;
547
548 while (n) {
549 req = rb_entry(n, struct ceph_osd_request, r_node);
550 if (tid < req->r_tid) {
551 if (!n->rb_left)
552 return req;
553 n = n->rb_left;
554 } else if (tid > req->r_tid) {
555 n = n->rb_right;
556 } else {
557 return req;
558 }
559 }
560 return NULL;
561}
562
Sage Weil6f6c7002011-01-17 20:34:08 -0800563/*
564 * Resubmit requests pending on the given osd.
565 */
566static void __kick_osd_requests(struct ceph_osd_client *osdc,
567 struct ceph_osd *osd)
568{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700569 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800570 int err;
571
572 dout("__kick_osd_requests osd%d\n", osd->o_osd);
573 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600574 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800575 return;
576
577 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
578 list_move(&req->r_req_lru_item, &osdc->req_unsent);
579 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
580 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700581 if (!req->r_linger)
582 req->r_flags |= CEPH_OSD_FLAG_RETRY;
583 }
584
585 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
586 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700587 /*
588 * reregister request prior to unregistering linger so
589 * that r_osd is preserved.
590 */
591 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700592 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700593 list_add(&req->r_req_lru_item, &osdc->req_unsent);
594 list_add(&req->r_osd_item, &req->r_osd->o_requests);
595 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700596 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
597 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800598 }
599}
600
Sage Weilf24e9982009-10-06 11:31:10 -0700601/*
Sage Weil81b024e2009-10-09 10:29:18 -0700602 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700603 */
604static void osd_reset(struct ceph_connection *con)
605{
606 struct ceph_osd *osd = con->private;
607 struct ceph_osd_client *osdc;
608
609 if (!osd)
610 return;
611 dout("osd_reset osd%d\n", osd->o_osd);
612 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700613 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800614 mutex_lock(&osdc->request_mutex);
615 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600616 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800617 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700618 up_read(&osdc->map_sem);
619}
620
621/*
622 * Track open sessions with osds.
623 */
Alex Eldere10006f2012-05-26 23:26:43 -0500624static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700625{
626 struct ceph_osd *osd;
627
628 osd = kzalloc(sizeof(*osd), GFP_NOFS);
629 if (!osd)
630 return NULL;
631
632 atomic_set(&osd->o_ref, 1);
633 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500634 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600635 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700636 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700637 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800638 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700639 osd->o_incarnation = 1;
640
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700641 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800642
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800643 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700644 return osd;
645}
646
647static struct ceph_osd *get_osd(struct ceph_osd *osd)
648{
649 if (atomic_inc_not_zero(&osd->o_ref)) {
650 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
651 atomic_read(&osd->o_ref));
652 return osd;
653 } else {
654 dout("get_osd %p FAIL\n", osd);
655 return NULL;
656 }
657}
658
659static void put_osd(struct ceph_osd *osd)
660{
661 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
662 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500663 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700664 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
665
Alex Eldera2556512012-05-16 15:16:39 -0500666 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500667 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700668 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700669 }
Sage Weilf24e9982009-10-06 11:31:10 -0700670}
671
672/*
673 * remove an osd from our map
674 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800675static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700676{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800677 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700678 BUG_ON(!list_empty(&osd->o_requests));
679 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800680 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700681 ceph_con_close(&osd->o_con);
682 put_osd(osd);
683}
684
Sage Weilaca420b2011-08-31 14:45:53 -0700685static void remove_all_osds(struct ceph_osd_client *osdc)
686{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500687 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700688 mutex_lock(&osdc->request_mutex);
689 while (!RB_EMPTY_ROOT(&osdc->osds)) {
690 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
691 struct ceph_osd, o_node);
692 __remove_osd(osdc, osd);
693 }
694 mutex_unlock(&osdc->request_mutex);
695}
696
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800697static void __move_osd_to_lru(struct ceph_osd_client *osdc,
698 struct ceph_osd *osd)
699{
700 dout("__move_osd_to_lru %p\n", osd);
701 BUG_ON(!list_empty(&osd->o_osd_lru));
702 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700703 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800704}
705
706static void __remove_osd_from_lru(struct ceph_osd *osd)
707{
708 dout("__remove_osd_from_lru %p\n", osd);
709 if (!list_empty(&osd->o_osd_lru))
710 list_del_init(&osd->o_osd_lru);
711}
712
Sage Weilaca420b2011-08-31 14:45:53 -0700713static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800714{
715 struct ceph_osd *osd, *nosd;
716
717 dout("__remove_old_osds %p\n", osdc);
718 mutex_lock(&osdc->request_mutex);
719 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700720 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800721 break;
722 __remove_osd(osdc, osd);
723 }
724 mutex_unlock(&osdc->request_mutex);
725}
726
Sage Weilf24e9982009-10-06 11:31:10 -0700727/*
728 * reset osd connect
729 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800730static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700731{
Alex Elderc3acb182012-12-07 09:57:58 -0600732 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700733
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800734 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700735 if (list_empty(&osd->o_requests) &&
736 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800737 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600738
739 return -ENODEV;
740 }
741
742 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
743 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
744 !ceph_con_opened(&osd->o_con)) {
745 struct ceph_osd_request *req;
746
Sage Weil87b315a2010-03-22 14:51:18 -0700747 dout(" osd addr hasn't changed and connection never opened,"
748 " letting msgr retry");
749 /* touch each r_stamp for handle_timeout()'s benfit */
750 list_for_each_entry(req, &osd->o_requests, r_osd_item)
751 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600752
753 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700754 }
Alex Elderc3acb182012-12-07 09:57:58 -0600755
756 ceph_con_close(&osd->o_con);
757 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
758 osd->o_incarnation++;
759
760 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700761}
762
763static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
764{
765 struct rb_node **p = &osdc->osds.rb_node;
766 struct rb_node *parent = NULL;
767 struct ceph_osd *osd = NULL;
768
Sage Weilaca420b2011-08-31 14:45:53 -0700769 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700770 while (*p) {
771 parent = *p;
772 osd = rb_entry(parent, struct ceph_osd, o_node);
773 if (new->o_osd < osd->o_osd)
774 p = &(*p)->rb_left;
775 else if (new->o_osd > osd->o_osd)
776 p = &(*p)->rb_right;
777 else
778 BUG();
779 }
780
781 rb_link_node(&new->o_node, parent, p);
782 rb_insert_color(&new->o_node, &osdc->osds);
783}
784
785static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
786{
787 struct ceph_osd *osd;
788 struct rb_node *n = osdc->osds.rb_node;
789
790 while (n) {
791 osd = rb_entry(n, struct ceph_osd, o_node);
792 if (o < osd->o_osd)
793 n = n->rb_left;
794 else if (o > osd->o_osd)
795 n = n->rb_right;
796 else
797 return osd;
798 }
799 return NULL;
800}
801
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800802static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
803{
804 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700805 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800806}
807
808static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
809{
810 cancel_delayed_work(&osdc->timeout_work);
811}
Sage Weilf24e9982009-10-06 11:31:10 -0700812
813/*
814 * Register request, assign tid. If this is the first request, set up
815 * the timeout event.
816 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700817static void __register_request(struct ceph_osd_client *osdc,
818 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700819{
Sage Weilf24e9982009-10-06 11:31:10 -0700820 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800821 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700822 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700823 __insert_request(osdc, req);
824 ceph_osdc_get_request(req);
825 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700826 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800827 dout(" first request, scheduling timeout\n");
828 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700829 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700830}
831
832static void register_request(struct ceph_osd_client *osdc,
833 struct ceph_osd_request *req)
834{
835 mutex_lock(&osdc->request_mutex);
836 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700837 mutex_unlock(&osdc->request_mutex);
838}
839
840/*
841 * called under osdc->request_mutex
842 */
843static void __unregister_request(struct ceph_osd_client *osdc,
844 struct ceph_osd_request *req)
845{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500846 if (RB_EMPTY_NODE(&req->r_node)) {
847 dout("__unregister_request %p tid %lld not registered\n",
848 req, req->r_tid);
849 return;
850 }
851
Sage Weilf24e9982009-10-06 11:31:10 -0700852 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
853 rb_erase(&req->r_node, &osdc->requests);
854 osdc->num_requests--;
855
Sage Weil0ba64782009-10-08 16:57:16 -0700856 if (req->r_osd) {
857 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500858 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700859
860 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700861 if (list_empty(&req->r_osd->o_requests) &&
862 list_empty(&req->r_osd->o_linger_requests)) {
863 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800864 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700865 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700866 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700867 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700868 }
Sage Weilf24e9982009-10-06 11:31:10 -0700869
Alex Elder7d5f2482012-11-29 08:37:03 -0600870 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700871 ceph_osdc_put_request(req);
872
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800873 if (osdc->num_requests == 0) {
874 dout(" no requests, canceling timeout\n");
875 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700876 }
877}
878
879/*
880 * Cancel a previously queued request message
881 */
882static void __cancel_request(struct ceph_osd_request *req)
883{
Sage Weil6bc18872010-09-27 10:18:52 -0700884 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500885 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700886 req->r_sent = 0;
887 }
888}
889
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700890static void __register_linger_request(struct ceph_osd_client *osdc,
891 struct ceph_osd_request *req)
892{
893 dout("__register_linger_request %p\n", req);
894 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700895 if (req->r_osd)
896 list_add_tail(&req->r_linger_osd,
897 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700898}
899
900static void __unregister_linger_request(struct ceph_osd_client *osdc,
901 struct ceph_osd_request *req)
902{
903 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -0600904 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700905 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700906 list_del_init(&req->r_linger_osd);
907
908 if (list_empty(&req->r_osd->o_requests) &&
909 list_empty(&req->r_osd->o_linger_requests)) {
910 dout("moving osd to %p lru\n", req->r_osd);
911 __move_osd_to_lru(osdc, req->r_osd);
912 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700913 if (list_empty(&req->r_osd_item))
914 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700915 }
916}
917
918void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
919 struct ceph_osd_request *req)
920{
921 mutex_lock(&osdc->request_mutex);
922 if (req->r_linger) {
923 __unregister_linger_request(osdc, req);
924 ceph_osdc_put_request(req);
925 }
926 mutex_unlock(&osdc->request_mutex);
927}
928EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
929
930void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
931 struct ceph_osd_request *req)
932{
933 if (!req->r_linger) {
934 dout("set_request_linger %p\n", req);
935 req->r_linger = 1;
936 /*
937 * caller is now responsible for calling
938 * unregister_linger_request
939 */
940 ceph_osdc_get_request(req);
941 }
942}
943EXPORT_SYMBOL(ceph_osdc_set_request_linger);
944
Sage Weilf24e9982009-10-06 11:31:10 -0700945/*
946 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
947 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300948 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800949 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700950 *
951 * Return 0 if unchanged, 1 if changed, or negative on error.
952 *
953 * Caller should hold map_sem for read and request_mutex.
954 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800955static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700956 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700957{
Sage Weil5b191d92013-02-23 10:38:16 -0800958 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700959 int acting[CEPH_PG_MAX_SIZE];
960 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700961 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700962
Sage Weil6f6c7002011-01-17 20:34:08 -0800963 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -0600964 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
965 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -0800966 if (err) {
967 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700968 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800969 }
Sage Weil7740a422010-01-08 15:58:25 -0800970 req->r_pgid = pgid;
971
Sage Weild85b7052010-05-10 10:24:48 -0700972 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
973 if (err > 0) {
974 o = acting[0];
975 num = err;
976 }
Sage Weilf24e9982009-10-06 11:31:10 -0700977
Sage Weil38d64532011-10-14 13:33:55 -0700978 if ((!force_resend &&
979 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700980 req->r_sent >= req->r_osd->o_incarnation &&
981 req->r_num_pg_osds == num &&
982 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700983 (req->r_osd == NULL && o == -1))
984 return 0; /* no change */
985
Sage Weil5b191d92013-02-23 10:38:16 -0800986 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
987 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -0700988 req->r_osd ? req->r_osd->o_osd : -1);
989
Sage Weild85b7052010-05-10 10:24:48 -0700990 /* record full pg acting set */
991 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
992 req->r_num_pg_osds = num;
993
Sage Weilf24e9982009-10-06 11:31:10 -0700994 if (req->r_osd) {
995 __cancel_request(req);
996 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700997 req->r_osd = NULL;
998 }
999
1000 req->r_osd = __lookup_osd(osdc, o);
1001 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -08001002 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -05001003 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -08001004 if (!req->r_osd) {
1005 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001006 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001007 }
Sage Weilf24e9982009-10-06 11:31:10 -07001008
Sage Weil6f6c7002011-01-17 20:34:08 -08001009 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 __insert_osd(osdc, req->r_osd);
1011
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001012 ceph_con_open(&req->r_osd->o_con,
1013 CEPH_ENTITY_TYPE_OSD, o,
1014 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001015 }
1016
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001017 if (req->r_osd) {
1018 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001019 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001020 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1021 } else {
1022 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001023 }
Sage Weild85b7052010-05-10 10:24:48 -07001024 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001025
1026out:
Sage Weilf24e9982009-10-06 11:31:10 -07001027 return err;
1028}
1029
1030/*
1031 * caller should hold map_sem (for read) and request_mutex
1032 */
Sage Weil56e925b2012-01-03 12:34:34 -08001033static void __send_request(struct ceph_osd_client *osdc,
1034 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001035{
Sage Weil1b83bef2013-02-25 16:11:12 -08001036 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001037
Sage Weil1b83bef2013-02-25 16:11:12 -08001038 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1039 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1040 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001041
Sage Weil1b83bef2013-02-25 16:11:12 -08001042 /* fill in message content that changes each time we send it */
1043 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1044 put_unaligned_le32(req->r_flags, req->r_request_flags);
1045 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1046 p = req->r_request_pgid;
1047 ceph_encode_64(&p, req->r_pgid.pool);
1048 ceph_encode_32(&p, req->r_pgid.seed);
1049 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1050 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1051 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001052
Sage Weil3dd72fc2010-03-22 14:42:30 -07001053 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001054 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001055
1056 ceph_msg_get(req->r_request); /* send consumes a ref */
1057 ceph_con_send(&req->r_osd->o_con, req->r_request);
1058 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001059}
1060
1061/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001062 * Send any requests in the queue (req_unsent).
1063 */
Alex Elderf9d25192013-02-15 11:42:29 -06001064static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001065{
1066 struct ceph_osd_request *req, *tmp;
1067
Alex Elderf9d25192013-02-15 11:42:29 -06001068 dout("__send_queued\n");
1069 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001070 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001071}
1072
1073/*
Sage Weilf24e9982009-10-06 11:31:10 -07001074 * Timeout callback, called every N seconds when 1 or more osd
1075 * requests has been active for more than N seconds. When this
1076 * happens, we ping all OSDs with requests who have timed out to
1077 * ensure any communications channel reset is detected. Reset the
1078 * request timeouts another N seconds in the future as we go.
1079 * Reschedule the timeout event another N seconds in future (unless
1080 * there are no open requests).
1081 */
1082static void handle_timeout(struct work_struct *work)
1083{
1084 struct ceph_osd_client *osdc =
1085 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001086 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001087 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001088 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001089 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001090 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001091 dout("timeout\n");
1092 down_read(&osdc->map_sem);
1093
1094 ceph_monc_request_next_osdmap(&osdc->client->monc);
1095
1096 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001097
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001098 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001099 * ping osds that are a bit slow. this ensures that if there
1100 * is a break in the TCP connection we will notice, and reopen
1101 * a connection with that osd (from the fault callback).
1102 */
1103 INIT_LIST_HEAD(&slow_osds);
1104 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001105 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001106 break;
1107
1108 osd = req->r_osd;
1109 BUG_ON(!osd);
1110 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001111 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001112 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1113 }
1114 while (!list_empty(&slow_osds)) {
1115 osd = list_entry(slow_osds.next, struct ceph_osd,
1116 o_keepalive_item);
1117 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001118 ceph_con_keepalive(&osd->o_con);
1119 }
1120
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001121 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001122 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001123 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001124 up_read(&osdc->map_sem);
1125}
1126
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001127static void handle_osds_timeout(struct work_struct *work)
1128{
1129 struct ceph_osd_client *osdc =
1130 container_of(work, struct ceph_osd_client,
1131 osds_timeout_work.work);
1132 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001133 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001134
1135 dout("osds timeout\n");
1136 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001137 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001138 up_read(&osdc->map_sem);
1139
1140 schedule_delayed_work(&osdc->osds_timeout_work,
1141 round_jiffies_relative(delay));
1142}
1143
Sage Weil25845472011-06-03 09:37:09 -07001144static void complete_request(struct ceph_osd_request *req)
1145{
1146 if (req->r_safe_callback)
1147 req->r_safe_callback(req, NULL);
1148 complete_all(&req->r_safe_completion); /* fsync waiter */
1149}
1150
Sage Weil1b83bef2013-02-25 16:11:12 -08001151static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1152{
1153 __u8 v;
1154
1155 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1156 v = ceph_decode_8(p);
1157 if (v > 1) {
1158 pr_warning("do not understand pg encoding %d > 1", v);
1159 return -EINVAL;
1160 }
1161 pgid->pool = ceph_decode_64(p);
1162 pgid->seed = ceph_decode_32(p);
1163 *p += 4;
1164 return 0;
1165
1166bad:
1167 pr_warning("incomplete pg encoding");
1168 return -EINVAL;
1169}
1170
Sage Weilf24e9982009-10-06 11:31:10 -07001171/*
1172 * handle osd op reply. either call the callback if it is specified,
1173 * or do the completion to wake up the waiting thread.
1174 */
Sage Weil350b1c32009-12-22 10:45:45 -08001175static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1176 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001177{
Sage Weil1b83bef2013-02-25 16:11:12 -08001178 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001179 struct ceph_osd_request *req;
1180 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001181 int object_len;
1182 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001183 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001184 s32 retry_attempt;
1185 struct ceph_pg pg;
1186 int err;
1187 u32 reassert_epoch;
1188 u64 reassert_version;
1189 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001190 int already_completed;
Sage Weil1b83bef2013-02-25 16:11:12 -08001191 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001192
Sage Weil6df058c2009-12-22 11:24:33 -08001193 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001194 dout("handle_reply %p tid %llu\n", msg, tid);
1195
1196 p = msg->front.iov_base;
1197 end = p + msg->front.iov_len;
1198
1199 ceph_decode_need(&p, end, 4, bad);
1200 object_len = ceph_decode_32(&p);
1201 ceph_decode_need(&p, end, object_len, bad);
1202 p += object_len;
1203
1204 err = __decode_pgid(&p, end, &pg);
1205 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001206 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001207
1208 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1209 flags = ceph_decode_64(&p);
1210 result = ceph_decode_32(&p);
1211 reassert_epoch = ceph_decode_32(&p);
1212 reassert_version = ceph_decode_64(&p);
1213 osdmap_epoch = ceph_decode_32(&p);
1214
Sage Weilf24e9982009-10-06 11:31:10 -07001215 /* lookup */
1216 mutex_lock(&osdc->request_mutex);
1217 req = __lookup_request(osdc, tid);
1218 if (req == NULL) {
1219 dout("handle_reply tid %llu dne\n", tid);
1220 mutex_unlock(&osdc->request_mutex);
1221 return;
1222 }
1223 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001224
1225 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1226 req, result);
1227
1228 ceph_decode_need(&p, end, 4, bad);
1229 numops = ceph_decode_32(&p);
1230 if (numops > CEPH_OSD_MAX_OP)
1231 goto bad_put;
1232 if (numops != req->r_num_ops)
1233 goto bad_put;
1234 payload_len = 0;
1235 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1236 for (i = 0; i < numops; i++) {
1237 struct ceph_osd_op *op = p;
1238 int len;
1239
1240 len = le32_to_cpu(op->payload_len);
1241 req->r_reply_op_len[i] = len;
1242 dout(" op %d has %d bytes\n", i, len);
1243 payload_len += len;
1244 p += sizeof(*op);
1245 }
1246 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1247 pr_warning("sum of op payload lens %d != data_len %d",
1248 payload_len, le32_to_cpu(msg->hdr.data_len));
1249 goto bad_put;
1250 }
1251
1252 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1253 retry_attempt = ceph_decode_32(&p);
1254 for (i = 0; i < numops; i++)
1255 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001256
Sage Weil350b1c32009-12-22 10:45:45 -08001257 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001258 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001259 * avoid a (safe but slower) revoke later.
1260 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001261 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001262 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001263 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001264 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001265 }
1266
Sage Weilf24e9982009-10-06 11:31:10 -07001267 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001268 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001269
Sage Weil1b83bef2013-02-25 16:11:12 -08001270 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001271 bytes = le32_to_cpu(msg->hdr.data_len);
1272 dout("handle_reply result %d bytes %d\n", req->r_result,
1273 bytes);
1274 if (req->r_result == 0)
1275 req->r_result = bytes;
1276
1277 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001278 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1279 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001280
1281 req->r_got_reply = 1;
1282 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1283 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001284 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001285 goto done;
1286 }
1287
1288 dout("handle_reply tid %llu flags %d\n", tid, flags);
1289
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001290 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1291 __register_linger_request(osdc, req);
1292
Sage Weilf24e9982009-10-06 11:31:10 -07001293 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001294 if (result < 0 ||
1295 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001296 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1297 __unregister_request(osdc, req);
1298
Alex Elder0d5af162013-02-27 10:26:25 -06001299 already_completed = req->r_completed;
1300 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001301 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001302 if (already_completed)
1303 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001304
1305 if (req->r_callback)
1306 req->r_callback(req, msg);
1307 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001308 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001309
Sage Weil25845472011-06-03 09:37:09 -07001310 if (flags & CEPH_OSD_FLAG_ONDISK)
1311 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001312
1313done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001314 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001315 ceph_osdc_put_request(req);
1316 return;
1317
Sage Weil1b83bef2013-02-25 16:11:12 -08001318bad_put:
1319 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001320bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001321 pr_err("corrupt osd_op_reply got %d %d\n",
1322 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001323 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001324}
1325
Sage Weil6f6c7002011-01-17 20:34:08 -08001326static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001327{
Sage Weilf24e9982009-10-06 11:31:10 -07001328 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001329
Sage Weil6f6c7002011-01-17 20:34:08 -08001330 for (p = rb_first(&osdc->osds); p; p = n) {
1331 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001332
Sage Weil6f6c7002011-01-17 20:34:08 -08001333 n = rb_next(p);
1334 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1335 memcmp(&osd->o_con.peer_addr,
1336 ceph_osd_addr(osdc->osdmap,
1337 osd->o_osd),
1338 sizeof(struct ceph_entity_addr)) != 0)
1339 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001340 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001341}
1342
1343/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001344 * Requeue requests whose mapping to an OSD has changed. If requests map to
1345 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001346 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001347 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001348 */
Sage Weil38d64532011-10-14 13:33:55 -07001349static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001350{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001351 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001352 struct rb_node *p;
1353 int needmap = 0;
1354 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001355
Sage Weil38d64532011-10-14 13:33:55 -07001356 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001357 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001358 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001359 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001360 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001361
1362 /*
1363 * For linger requests that have not yet been
1364 * registered, move them to the linger list; they'll
1365 * be sent to the osd in the loop below. Unregister
1366 * the request before re-registering it as a linger
1367 * request to ensure the __map_request() below
1368 * will decide it needs to be sent.
1369 */
1370 if (req->r_linger && list_empty(&req->r_linger_item)) {
1371 dout("%p tid %llu restart on osd%d\n",
1372 req, req->r_tid,
1373 req->r_osd ? req->r_osd->o_osd : -1);
1374 __unregister_request(osdc, req);
1375 __register_linger_request(osdc, req);
1376 continue;
1377 }
1378
Sage Weil38d64532011-10-14 13:33:55 -07001379 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001380 if (err < 0)
1381 continue; /* error */
1382 if (req->r_osd == NULL) {
1383 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1384 needmap++; /* request a newer map */
1385 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001386 if (!req->r_linger) {
1387 dout("%p tid %llu requeued on osd%d\n", req,
1388 req->r_tid,
1389 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001390 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001391 }
1392 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001393 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001394
1395 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1396 r_linger_item) {
1397 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1398
Sage Weil38d64532011-10-14 13:33:55 -07001399 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001400 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001401 if (err == 0)
1402 continue; /* no change and no osd was specified */
1403 if (err < 0)
1404 continue; /* hrm! */
1405 if (req->r_osd == NULL) {
1406 dout("tid %llu maps to no valid osd\n", req->r_tid);
1407 needmap++; /* request a newer map */
1408 continue;
1409 }
1410
1411 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1412 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001413 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001414 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001415 }
Sage Weilf24e9982009-10-06 11:31:10 -07001416 mutex_unlock(&osdc->request_mutex);
1417
1418 if (needmap) {
1419 dout("%d requests for down osds, need new map\n", needmap);
1420 ceph_monc_request_next_osdmap(&osdc->client->monc);
1421 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001422 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001423}
Sage Weil6f6c7002011-01-17 20:34:08 -08001424
1425
Sage Weilf24e9982009-10-06 11:31:10 -07001426/*
1427 * Process updated osd map.
1428 *
1429 * The message contains any number of incremental and full maps, normally
1430 * indicating some sort of topology change in the cluster. Kick requests
1431 * off to different OSDs as needed.
1432 */
1433void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1434{
1435 void *p, *end, *next;
1436 u32 nr_maps, maplen;
1437 u32 epoch;
1438 struct ceph_osdmap *newmap = NULL, *oldmap;
1439 int err;
1440 struct ceph_fsid fsid;
1441
1442 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1443 p = msg->front.iov_base;
1444 end = p + msg->front.iov_len;
1445
1446 /* verify fsid */
1447 ceph_decode_need(&p, end, sizeof(fsid), bad);
1448 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001449 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1450 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001451
1452 down_write(&osdc->map_sem);
1453
1454 /* incremental maps */
1455 ceph_decode_32_safe(&p, end, nr_maps, bad);
1456 dout(" %d inc maps\n", nr_maps);
1457 while (nr_maps > 0) {
1458 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001459 epoch = ceph_decode_32(&p);
1460 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001461 ceph_decode_need(&p, end, maplen, bad);
1462 next = p + maplen;
1463 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1464 dout("applying incremental map %u len %d\n",
1465 epoch, maplen);
1466 newmap = osdmap_apply_incremental(&p, next,
1467 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001468 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001469 if (IS_ERR(newmap)) {
1470 err = PTR_ERR(newmap);
1471 goto bad;
1472 }
Sage Weil30dc6382009-12-21 14:49:37 -08001473 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001474 if (newmap != osdc->osdmap) {
1475 ceph_osdmap_destroy(osdc->osdmap);
1476 osdc->osdmap = newmap;
1477 }
Sage Weil38d64532011-10-14 13:33:55 -07001478 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001479 } else {
1480 dout("ignoring incremental map %u len %d\n",
1481 epoch, maplen);
1482 }
1483 p = next;
1484 nr_maps--;
1485 }
1486 if (newmap)
1487 goto done;
1488
1489 /* full maps */
1490 ceph_decode_32_safe(&p, end, nr_maps, bad);
1491 dout(" %d full maps\n", nr_maps);
1492 while (nr_maps) {
1493 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001494 epoch = ceph_decode_32(&p);
1495 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001496 ceph_decode_need(&p, end, maplen, bad);
1497 if (nr_maps > 1) {
1498 dout("skipping non-latest full map %u len %d\n",
1499 epoch, maplen);
1500 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1501 dout("skipping full map %u len %d, "
1502 "older than our %u\n", epoch, maplen,
1503 osdc->osdmap->epoch);
1504 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001505 int skipped_map = 0;
1506
Sage Weilf24e9982009-10-06 11:31:10 -07001507 dout("taking full map %u len %d\n", epoch, maplen);
1508 newmap = osdmap_decode(&p, p+maplen);
1509 if (IS_ERR(newmap)) {
1510 err = PTR_ERR(newmap);
1511 goto bad;
1512 }
Sage Weil30dc6382009-12-21 14:49:37 -08001513 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001514 oldmap = osdc->osdmap;
1515 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001516 if (oldmap) {
1517 if (oldmap->epoch + 1 < newmap->epoch)
1518 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001519 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001520 }
1521 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001522 }
1523 p += maplen;
1524 nr_maps--;
1525 }
1526
1527done:
1528 downgrade_write(&osdc->map_sem);
1529 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001530
1531 /*
1532 * subscribe to subsequent osdmap updates if full to ensure
1533 * we find out when we are no longer full and stop returning
1534 * ENOSPC.
1535 */
1536 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1537 ceph_monc_request_next_osdmap(&osdc->client->monc);
1538
Alex Elderf9d25192013-02-15 11:42:29 -06001539 mutex_lock(&osdc->request_mutex);
1540 __send_queued(osdc);
1541 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001542 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001543 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001544 return;
1545
1546bad:
1547 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001548 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001549 up_write(&osdc->map_sem);
1550 return;
1551}
1552
Sage Weilf24e9982009-10-06 11:31:10 -07001553/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001554 * watch/notify callback event infrastructure
1555 *
1556 * These callbacks are used both for watch and notify operations.
1557 */
1558static void __release_event(struct kref *kref)
1559{
1560 struct ceph_osd_event *event =
1561 container_of(kref, struct ceph_osd_event, kref);
1562
1563 dout("__release_event %p\n", event);
1564 kfree(event);
1565}
1566
1567static void get_event(struct ceph_osd_event *event)
1568{
1569 kref_get(&event->kref);
1570}
1571
1572void ceph_osdc_put_event(struct ceph_osd_event *event)
1573{
1574 kref_put(&event->kref, __release_event);
1575}
1576EXPORT_SYMBOL(ceph_osdc_put_event);
1577
1578static void __insert_event(struct ceph_osd_client *osdc,
1579 struct ceph_osd_event *new)
1580{
1581 struct rb_node **p = &osdc->event_tree.rb_node;
1582 struct rb_node *parent = NULL;
1583 struct ceph_osd_event *event = NULL;
1584
1585 while (*p) {
1586 parent = *p;
1587 event = rb_entry(parent, struct ceph_osd_event, node);
1588 if (new->cookie < event->cookie)
1589 p = &(*p)->rb_left;
1590 else if (new->cookie > event->cookie)
1591 p = &(*p)->rb_right;
1592 else
1593 BUG();
1594 }
1595
1596 rb_link_node(&new->node, parent, p);
1597 rb_insert_color(&new->node, &osdc->event_tree);
1598}
1599
1600static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1601 u64 cookie)
1602{
1603 struct rb_node **p = &osdc->event_tree.rb_node;
1604 struct rb_node *parent = NULL;
1605 struct ceph_osd_event *event = NULL;
1606
1607 while (*p) {
1608 parent = *p;
1609 event = rb_entry(parent, struct ceph_osd_event, node);
1610 if (cookie < event->cookie)
1611 p = &(*p)->rb_left;
1612 else if (cookie > event->cookie)
1613 p = &(*p)->rb_right;
1614 else
1615 return event;
1616 }
1617 return NULL;
1618}
1619
1620static void __remove_event(struct ceph_osd_event *event)
1621{
1622 struct ceph_osd_client *osdc = event->osdc;
1623
1624 if (!RB_EMPTY_NODE(&event->node)) {
1625 dout("__remove_event removed %p\n", event);
1626 rb_erase(&event->node, &osdc->event_tree);
1627 ceph_osdc_put_event(event);
1628 } else {
1629 dout("__remove_event didn't remove %p\n", event);
1630 }
1631}
1632
1633int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1634 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001635 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001636{
1637 struct ceph_osd_event *event;
1638
1639 event = kmalloc(sizeof(*event), GFP_NOIO);
1640 if (!event)
1641 return -ENOMEM;
1642
1643 dout("create_event %p\n", event);
1644 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001645 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001646 event->data = data;
1647 event->osdc = osdc;
1648 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001649 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001650 kref_init(&event->kref); /* one ref for us */
1651 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001652
1653 spin_lock(&osdc->event_lock);
1654 event->cookie = ++osdc->event_count;
1655 __insert_event(osdc, event);
1656 spin_unlock(&osdc->event_lock);
1657
1658 *pevent = event;
1659 return 0;
1660}
1661EXPORT_SYMBOL(ceph_osdc_create_event);
1662
1663void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1664{
1665 struct ceph_osd_client *osdc = event->osdc;
1666
1667 dout("cancel_event %p\n", event);
1668 spin_lock(&osdc->event_lock);
1669 __remove_event(event);
1670 spin_unlock(&osdc->event_lock);
1671 ceph_osdc_put_event(event); /* caller's */
1672}
1673EXPORT_SYMBOL(ceph_osdc_cancel_event);
1674
1675
1676static void do_event_work(struct work_struct *work)
1677{
1678 struct ceph_osd_event_work *event_work =
1679 container_of(work, struct ceph_osd_event_work, work);
1680 struct ceph_osd_event *event = event_work->event;
1681 u64 ver = event_work->ver;
1682 u64 notify_id = event_work->notify_id;
1683 u8 opcode = event_work->opcode;
1684
1685 dout("do_event_work completing %p\n", event);
1686 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001687 dout("do_event_work completed %p\n", event);
1688 ceph_osdc_put_event(event);
1689 kfree(event_work);
1690}
1691
1692
1693/*
1694 * Process osd watch notifications
1695 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001696static void handle_watch_notify(struct ceph_osd_client *osdc,
1697 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001698{
1699 void *p, *end;
1700 u8 proto_ver;
1701 u64 cookie, ver, notify_id;
1702 u8 opcode;
1703 struct ceph_osd_event *event;
1704 struct ceph_osd_event_work *event_work;
1705
1706 p = msg->front.iov_base;
1707 end = p + msg->front.iov_len;
1708
1709 ceph_decode_8_safe(&p, end, proto_ver, bad);
1710 ceph_decode_8_safe(&p, end, opcode, bad);
1711 ceph_decode_64_safe(&p, end, cookie, bad);
1712 ceph_decode_64_safe(&p, end, ver, bad);
1713 ceph_decode_64_safe(&p, end, notify_id, bad);
1714
1715 spin_lock(&osdc->event_lock);
1716 event = __find_event(osdc, cookie);
1717 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001718 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001719 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001720 }
1721 spin_unlock(&osdc->event_lock);
1722 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1723 cookie, ver, event);
1724 if (event) {
1725 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001726 if (!event_work) {
1727 dout("ERROR: could not allocate event_work\n");
1728 goto done_err;
1729 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001730 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001731 event_work->event = event;
1732 event_work->ver = ver;
1733 event_work->notify_id = notify_id;
1734 event_work->opcode = opcode;
1735 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1736 dout("WARNING: failed to queue notify event work\n");
1737 goto done_err;
1738 }
1739 }
1740
1741 return;
1742
1743done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001744 ceph_osdc_put_event(event);
1745 return;
1746
1747bad:
1748 pr_err("osdc handle_watch_notify corrupt msg\n");
1749 return;
1750}
1751
Alex Elder70636772013-03-04 18:29:06 -06001752static void ceph_osdc_msg_data_set(struct ceph_msg *msg,
1753 struct ceph_osd_data *osd_data)
1754{
1755 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
1756 BUG_ON(osd_data->length > (u64) SIZE_MAX);
1757 if (osd_data->length)
1758 ceph_msg_data_set_pages(msg, osd_data->pages,
1759 osd_data->length, osd_data->alignment);
1760#ifdef CONFIG_BLOCK
1761 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
1762 ceph_msg_data_set_bio(msg, osd_data->bio);
1763#endif
1764 } else {
1765 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
1766 }
1767}
1768
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001769/*
Sage Weilf24e9982009-10-06 11:31:10 -07001770 * Register request, send initial attempt.
1771 */
1772int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1773 struct ceph_osd_request *req,
1774 bool nofail)
1775{
Sage Weilc1ea8822009-10-08 16:55:47 -07001776 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001777
Alex Elder70636772013-03-04 18:29:06 -06001778 /* Set up response incoming data and request outgoing data fields */
Alex Elder0fff87e2013-02-14 12:16:43 -06001779
Alex Elder70636772013-03-04 18:29:06 -06001780 ceph_osdc_msg_data_set(req->r_reply, &req->r_data_in);
1781 ceph_osdc_msg_data_set(req->r_request, &req->r_data_out);
Alex Elderebf18f42013-03-04 22:29:57 -06001782 if (req->r_trail.length)
1783 ceph_msg_data_set_trail(req->r_request, &req->r_trail);
Sage Weilf24e9982009-10-06 11:31:10 -07001784
1785 register_request(osdc, req);
1786
1787 down_read(&osdc->map_sem);
1788 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001789 /*
1790 * a racing kick_requests() may have sent the message for us
1791 * while we dropped request_mutex above, so only send now if
1792 * the request still han't been touched yet.
1793 */
1794 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001795 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001796 if (rc < 0) {
1797 if (nofail) {
1798 dout("osdc_start_request failed map, "
1799 " will retry %lld\n", req->r_tid);
1800 rc = 0;
1801 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001802 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001803 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001804 if (req->r_osd == NULL) {
1805 dout("send_request %p no up osds in pg\n", req);
1806 ceph_monc_request_next_osdmap(&osdc->client->monc);
1807 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001808 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001809 }
Sage Weil56e925b2012-01-03 12:34:34 -08001810 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001811 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001812
1813out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001814 mutex_unlock(&osdc->request_mutex);
1815 up_read(&osdc->map_sem);
1816 return rc;
1817}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001818EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001819
1820/*
1821 * wait for a request to complete
1822 */
1823int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1824 struct ceph_osd_request *req)
1825{
1826 int rc;
1827
1828 rc = wait_for_completion_interruptible(&req->r_completion);
1829 if (rc < 0) {
1830 mutex_lock(&osdc->request_mutex);
1831 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001832 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001833 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001834 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001835 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001836 return rc;
1837 }
1838
1839 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1840 return req->r_result;
1841}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001842EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001843
1844/*
1845 * sync - wait for all in-flight requests to flush. avoid starvation.
1846 */
1847void ceph_osdc_sync(struct ceph_osd_client *osdc)
1848{
1849 struct ceph_osd_request *req;
1850 u64 last_tid, next_tid = 0;
1851
1852 mutex_lock(&osdc->request_mutex);
1853 last_tid = osdc->last_tid;
1854 while (1) {
1855 req = __lookup_request_ge(osdc, next_tid);
1856 if (!req)
1857 break;
1858 if (req->r_tid > last_tid)
1859 break;
1860
1861 next_tid = req->r_tid + 1;
1862 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1863 continue;
1864
1865 ceph_osdc_get_request(req);
1866 mutex_unlock(&osdc->request_mutex);
1867 dout("sync waiting on tid %llu (last is %llu)\n",
1868 req->r_tid, last_tid);
1869 wait_for_completion(&req->r_safe_completion);
1870 mutex_lock(&osdc->request_mutex);
1871 ceph_osdc_put_request(req);
1872 }
1873 mutex_unlock(&osdc->request_mutex);
1874 dout("sync done (thru tid %llu)\n", last_tid);
1875}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001876EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001877
1878/*
1879 * init, shutdown
1880 */
1881int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1882{
1883 int err;
1884
1885 dout("init\n");
1886 osdc->client = client;
1887 osdc->osdmap = NULL;
1888 init_rwsem(&osdc->map_sem);
1889 init_completion(&osdc->map_waiters);
1890 osdc->last_requested_map = 0;
1891 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001892 osdc->last_tid = 0;
1893 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001894 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001895 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001896 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001897 INIT_LIST_HEAD(&osdc->req_unsent);
1898 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001899 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001900 osdc->num_requests = 0;
1901 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001902 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001903 spin_lock_init(&osdc->event_lock);
1904 osdc->event_tree = RB_ROOT;
1905 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001906
1907 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001908 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001909
Sage Weil5f44f142009-11-18 14:52:18 -08001910 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001911 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1912 sizeof(struct ceph_osd_request));
1913 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001914 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001915
Sage Weild50b4092012-07-09 14:22:34 -07001916 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1917 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001918 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001919 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001920 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001921 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001922 OSD_OPREPLY_FRONT_LEN, 10, true,
1923 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001924 if (err < 0)
1925 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001926
1927 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1928 if (IS_ERR(osdc->notify_wq)) {
1929 err = PTR_ERR(osdc->notify_wq);
1930 osdc->notify_wq = NULL;
1931 goto out_msgpool;
1932 }
Sage Weilf24e9982009-10-06 11:31:10 -07001933 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001934
Sage Weilc16e7862010-03-01 13:02:00 -08001935out_msgpool:
1936 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001937out_mempool:
1938 mempool_destroy(osdc->req_mempool);
1939out:
1940 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001941}
1942
1943void ceph_osdc_stop(struct ceph_osd_client *osdc)
1944{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001945 flush_workqueue(osdc->notify_wq);
1946 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001947 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001948 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001949 if (osdc->osdmap) {
1950 ceph_osdmap_destroy(osdc->osdmap);
1951 osdc->osdmap = NULL;
1952 }
Sage Weilaca420b2011-08-31 14:45:53 -07001953 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001954 mempool_destroy(osdc->req_mempool);
1955 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001956 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001957}
1958
1959/*
1960 * Read some contiguous pages. If we cross a stripe boundary, shorten
1961 * *plen. Return number of bytes read, or error.
1962 */
1963int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1964 struct ceph_vino vino, struct ceph_file_layout *layout,
1965 u64 off, u64 *plen,
1966 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001967 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001968{
1969 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06001970 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07001971 int rc = 0;
1972
1973 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1974 vino.snap, off, *plen);
1975 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1976 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1977 NULL, 0, truncate_seq, truncate_size, NULL,
Alex Elder153e5162013-03-01 18:00:15 -06001978 false);
Sage Weil68162822012-09-24 21:01:02 -07001979 if (IS_ERR(req))
1980 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001981
1982 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06001983
1984 osd_data = &req->r_data_in;
1985 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
1986 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06001987 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06001988 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07001989
Alex Eldere0c59482013-03-07 15:38:25 -06001990 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
1991 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001992
1993 rc = ceph_osdc_start_request(osdc, req, false);
1994 if (!rc)
1995 rc = ceph_osdc_wait_request(osdc, req);
1996
1997 ceph_osdc_put_request(req);
1998 dout("readpages result %d\n", rc);
1999 return rc;
2000}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002001EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07002002
2003/*
2004 * do a synchronous write on N pages
2005 */
2006int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
2007 struct ceph_file_layout *layout,
2008 struct ceph_snap_context *snapc,
2009 u64 off, u64 len,
2010 u32 truncate_seq, u64 truncate_size,
2011 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002012 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002013{
2014 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002015 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002016 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002017 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002018
2019 BUG_ON(vino.snap != CEPH_NOSNAP);
2020 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2021 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002022 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06002023 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07002024 truncate_seq, truncate_size, mtime,
Alex Elder153e5162013-03-01 18:00:15 -06002025 true);
Sage Weil68162822012-09-24 21:01:02 -07002026 if (IS_ERR(req))
2027 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002028
2029 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002030 osd_data = &req->r_data_out;
2031 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2032 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002033 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002034 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002035 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002036
Alex Elder87f979d2013-02-15 11:42:29 -06002037 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002038 if (!rc)
2039 rc = ceph_osdc_wait_request(osdc, req);
2040
2041 ceph_osdc_put_request(req);
2042 if (rc == 0)
2043 rc = len;
2044 dout("writepages result %d\n", rc);
2045 return rc;
2046}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002047EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002048
2049/*
2050 * handle incoming message
2051 */
2052static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2053{
2054 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002055 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002056 int type = le16_to_cpu(msg->hdr.type);
2057
2058 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002059 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002060 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002061
2062 switch (type) {
2063 case CEPH_MSG_OSD_MAP:
2064 ceph_osdc_handle_map(osdc, msg);
2065 break;
2066 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002067 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002068 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002069 case CEPH_MSG_WATCH_NOTIFY:
2070 handle_watch_notify(osdc, msg);
2071 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002072
2073 default:
2074 pr_err("received unknown message type %d %s\n", type,
2075 ceph_msg_type_name(type));
2076 }
Sage Weil4a32f932010-06-13 10:27:53 -07002077out:
Sage Weilf24e9982009-10-06 11:31:10 -07002078 ceph_msg_put(msg);
2079}
2080
Sage Weil5b3a4db2010-02-19 21:43:23 -08002081/*
Sage Weil21b667f2010-03-04 10:22:59 -08002082 * lookup and return message for incoming reply. set up reply message
2083 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002084 */
2085static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002086 struct ceph_msg_header *hdr,
2087 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002088{
2089 struct ceph_osd *osd = con->private;
2090 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002091 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002092 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002093 int front = le32_to_cpu(hdr->front_len);
2094 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002095 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002096
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002097 tid = le64_to_cpu(hdr->tid);
2098 mutex_lock(&osdc->request_mutex);
2099 req = __lookup_request(osdc, tid);
2100 if (!req) {
2101 *skip = 1;
2102 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002103 dout("get_reply unknown tid %llu from osd%d\n", tid,
2104 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002105 goto out;
2106 }
Sage Weilc16e7862010-03-01 13:02:00 -08002107
2108 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002109 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002110 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002111 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002112 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002113 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002114 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002115
Sage Weilc16e7862010-03-01 13:02:00 -08002116 if (front > req->r_reply->front.iov_len) {
2117 pr_warning("get_reply front %d > preallocated %d\n",
2118 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002119 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002120 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002121 goto out;
2122 ceph_msg_put(req->r_reply);
2123 req->r_reply = m;
2124 }
2125 m = ceph_msg_get(req->r_reply);
2126
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002127 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002128 struct ceph_osd_data *osd_data = &req->r_data_in;
2129
2130 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002131 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002132 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002133
Alex Eldere0c59482013-03-07 15:38:25 -06002134 pr_warning("tid %lld reply has %d bytes "
2135 "we had only %llu bytes ready\n",
2136 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002137 *skip = 1;
2138 ceph_msg_put(m);
2139 m = NULL;
2140 goto out;
2141 }
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002142 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002143 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002144 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002145 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002146 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002147
2148out:
2149 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002150 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002151
2152}
2153
2154static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2155 struct ceph_msg_header *hdr,
2156 int *skip)
2157{
2158 struct ceph_osd *osd = con->private;
2159 int type = le16_to_cpu(hdr->type);
2160 int front = le32_to_cpu(hdr->front_len);
2161
Alex Elder1c20f2d2012-06-04 14:43:32 -05002162 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002163 switch (type) {
2164 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002165 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002166 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002167 case CEPH_MSG_OSD_OPREPLY:
2168 return get_reply(con, hdr, skip);
2169 default:
2170 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2171 osd->o_osd);
2172 *skip = 1;
2173 return NULL;
2174 }
Sage Weilf24e9982009-10-06 11:31:10 -07002175}
2176
2177/*
2178 * Wrappers to refcount containing ceph_osd struct
2179 */
2180static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2181{
2182 struct ceph_osd *osd = con->private;
2183 if (get_osd(osd))
2184 return con;
2185 return NULL;
2186}
2187
2188static void put_osd_con(struct ceph_connection *con)
2189{
2190 struct ceph_osd *osd = con->private;
2191 put_osd(osd);
2192}
2193
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002194/*
2195 * authentication
2196 */
Alex Eldera3530df2012-05-16 15:16:39 -05002197/*
2198 * Note: returned pointer is the address of a structure that's
2199 * managed separately. Caller must *not* attempt to free it.
2200 */
2201static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002202 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002203{
2204 struct ceph_osd *o = con->private;
2205 struct ceph_osd_client *osdc = o->o_osdc;
2206 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002207 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002208
Alex Elder74f18692012-05-16 15:16:39 -05002209 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002210 if (ac->ops && ac->ops->destroy_authorizer)
2211 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002212 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002213 }
Alex Eldera2556512012-05-16 15:16:39 -05002214 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002215 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2216 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002217 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002218 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002219 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002220 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002221
Alex Eldera3530df2012-05-16 15:16:39 -05002222 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002223}
2224
2225
2226static int verify_authorizer_reply(struct ceph_connection *con, int len)
2227{
2228 struct ceph_osd *o = con->private;
2229 struct ceph_osd_client *osdc = o->o_osdc;
2230 struct ceph_auth_client *ac = osdc->client->monc.auth;
2231
Alex Eldera2556512012-05-16 15:16:39 -05002232 /*
2233 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2234 * XXX which do we do: succeed or fail?
2235 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002236 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002237}
2238
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002239static int invalidate_authorizer(struct ceph_connection *con)
2240{
2241 struct ceph_osd *o = con->private;
2242 struct ceph_osd_client *osdc = o->o_osdc;
2243 struct ceph_auth_client *ac = osdc->client->monc.auth;
2244
Alex Eldera2556512012-05-16 15:16:39 -05002245 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002246 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2247
2248 return ceph_monc_validate_auth(&osdc->client->monc);
2249}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002250
Tobias Klauser9e327892010-05-20 10:40:19 +02002251static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002252 .get = get_osd_con,
2253 .put = put_osd_con,
2254 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002255 .get_authorizer = get_authorizer,
2256 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002257 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002258 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002259 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002260};