blob: 4402e917b9b15909658c170aa9ab101d5aef98f1 [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
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700225static void osd_req_encode_op(struct ceph_osd_request *req,
226 struct ceph_osd_op *dst,
227 struct ceph_osd_req_op *src)
228{
229 dst->op = cpu_to_le16(src->op);
230
Alex Elder065a68f2012-04-20 15:49:43 -0500231 switch (src->op) {
Alex Elderfbfab532013-02-08 09:55:48 -0600232 case CEPH_OSD_OP_STAT:
233 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700234 case CEPH_OSD_OP_READ:
235 case CEPH_OSD_OP_WRITE:
236 dst->extent.offset =
237 cpu_to_le64(src->extent.offset);
238 dst->extent.length =
239 cpu_to_le64(src->extent.length);
240 dst->extent.truncate_size =
241 cpu_to_le64(src->extent.truncate_size);
242 dst->extent.truncate_seq =
243 cpu_to_le32(src->extent.truncate_seq);
244 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700245 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700246 dst->cls.class_len = src->cls.class_len;
247 dst->cls.method_len = src->cls.method_len;
248 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
249
Alex Elderc8858372012-11-13 21:11:15 -0600250 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700251 src->cls.class_len);
Alex Elderc8858372012-11-13 21:11:15 -0600252 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700253 src->cls.method_len);
Alex Elderc8858372012-11-13 21:11:15 -0600254 ceph_pagelist_append(&req->r_trail, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700255 src->cls.indata_len);
256 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700257 case CEPH_OSD_OP_STARTSYNC:
258 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700259 case CEPH_OSD_OP_NOTIFY_ACK:
260 case CEPH_OSD_OP_WATCH:
261 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
262 dst->watch.ver = cpu_to_le64(src->watch.ver);
263 dst->watch.flag = src->watch.flag;
264 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700265 default:
Alex Elder8f63ca22013-03-04 11:08:29 -0600266 pr_err("unrecognized osd opcode %d\n", src->op);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700267 WARN_ON(1);
268 break;
Alex Elder4c464592013-02-15 11:42:30 -0600269 case CEPH_OSD_OP_MAPEXT:
270 case CEPH_OSD_OP_MASKTRUNC:
271 case CEPH_OSD_OP_SPARSE_READ:
Alex Eldera9f36c32013-02-15 11:42:30 -0600272 case CEPH_OSD_OP_NOTIFY:
Alex Elder4c464592013-02-15 11:42:30 -0600273 case CEPH_OSD_OP_ASSERT_VER:
274 case CEPH_OSD_OP_WRITEFULL:
275 case CEPH_OSD_OP_TRUNCATE:
276 case CEPH_OSD_OP_ZERO:
277 case CEPH_OSD_OP_DELETE:
278 case CEPH_OSD_OP_APPEND:
279 case CEPH_OSD_OP_SETTRUNC:
280 case CEPH_OSD_OP_TRIMTRUNC:
281 case CEPH_OSD_OP_TMAPUP:
282 case CEPH_OSD_OP_TMAPPUT:
283 case CEPH_OSD_OP_TMAPGET:
284 case CEPH_OSD_OP_CREATE:
Alex Eldera9f36c32013-02-15 11:42:30 -0600285 case CEPH_OSD_OP_ROLLBACK:
Alex Elder4c464592013-02-15 11:42:30 -0600286 case CEPH_OSD_OP_OMAPGETKEYS:
287 case CEPH_OSD_OP_OMAPGETVALS:
288 case CEPH_OSD_OP_OMAPGETHEADER:
289 case CEPH_OSD_OP_OMAPGETVALSBYKEYS:
290 case CEPH_OSD_OP_MODE_RD:
291 case CEPH_OSD_OP_OMAPSETVALS:
292 case CEPH_OSD_OP_OMAPSETHEADER:
293 case CEPH_OSD_OP_OMAPCLEAR:
294 case CEPH_OSD_OP_OMAPRMKEYS:
295 case CEPH_OSD_OP_OMAP_CMP:
296 case CEPH_OSD_OP_CLONERANGE:
297 case CEPH_OSD_OP_ASSERT_SRC_VERSION:
298 case CEPH_OSD_OP_SRC_CMPXATTR:
Alex Eldera9f36c32013-02-15 11:42:30 -0600299 case CEPH_OSD_OP_GETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600300 case CEPH_OSD_OP_GETXATTRS:
Alex Eldera9f36c32013-02-15 11:42:30 -0600301 case CEPH_OSD_OP_CMPXATTR:
302 case CEPH_OSD_OP_SETXATTR:
Alex Elder4c464592013-02-15 11:42:30 -0600303 case CEPH_OSD_OP_SETXATTRS:
304 case CEPH_OSD_OP_RESETXATTRS:
305 case CEPH_OSD_OP_RMXATTR:
306 case CEPH_OSD_OP_PULL:
307 case CEPH_OSD_OP_PUSH:
308 case CEPH_OSD_OP_BALANCEREADS:
309 case CEPH_OSD_OP_UNBALANCEREADS:
310 case CEPH_OSD_OP_SCRUB:
311 case CEPH_OSD_OP_SCRUB_RESERVE:
312 case CEPH_OSD_OP_SCRUB_UNRESERVE:
313 case CEPH_OSD_OP_SCRUB_STOP:
314 case CEPH_OSD_OP_SCRUB_MAP:
315 case CEPH_OSD_OP_WRLOCK:
316 case CEPH_OSD_OP_WRUNLOCK:
317 case CEPH_OSD_OP_RDLOCK:
318 case CEPH_OSD_OP_RDUNLOCK:
319 case CEPH_OSD_OP_UPLOCK:
320 case CEPH_OSD_OP_DNLOCK:
321 case CEPH_OSD_OP_PGLS:
322 case CEPH_OSD_OP_PGLS_FILTER:
323 pr_err("unsupported osd opcode %s\n",
Alex Elder8f63ca22013-03-04 11:08:29 -0600324 ceph_osd_op_name(src->op));
Alex Elder4c464592013-02-15 11:42:30 -0600325 WARN_ON(1);
326 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700327 }
328 dst->payload_len = cpu_to_le32(src->payload_len);
329}
330
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700331/*
332 * build new request AND message
333 *
334 */
335void ceph_osdc_build_request(struct ceph_osd_request *req,
Sage Weil1b83bef2013-02-25 16:11:12 -0800336 u64 off, u64 len, unsigned int num_ops,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700337 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600338 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600339 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700340{
341 struct ceph_msg *msg = req->r_request;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700342 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700343 void *p;
Sage Weil1b83bef2013-02-25 16:11:12 -0800344 size_t msg_size;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700345 int flags = req->r_flags;
Alex Elderf44246e2013-02-14 12:16:43 -0600346 u64 data_len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700347 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700348
Sage Weil1b83bef2013-02-25 16:11:12 -0800349 req->r_num_ops = num_ops;
350 req->r_snapid = snap_id;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700351 req->r_snapc = ceph_get_snap_context(snapc);
352
Sage Weil1b83bef2013-02-25 16:11:12 -0800353 /* encode request */
354 msg->hdr.version = cpu_to_le16(4);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700355
Sage Weil1b83bef2013-02-25 16:11:12 -0800356 p = msg->front.iov_base;
357 ceph_encode_32(&p, 1); /* client_inc is always 1 */
358 req->r_request_osdmap_epoch = p;
359 p += 4;
360 req->r_request_flags = p;
361 p += 4;
362 if (req->r_flags & CEPH_OSD_FLAG_WRITE)
363 ceph_encode_timespec(p, mtime);
364 p += sizeof(struct ceph_timespec);
365 req->r_request_reassert_version = p;
366 p += sizeof(struct ceph_eversion); /* will get filled in */
367
368 /* oloc */
369 ceph_encode_8(&p, 4);
370 ceph_encode_8(&p, 4);
371 ceph_encode_32(&p, 8 + 4 + 4);
372 req->r_request_pool = p;
373 p += 8;
374 ceph_encode_32(&p, -1); /* preferred */
375 ceph_encode_32(&p, 0); /* key len */
376
377 ceph_encode_8(&p, 1);
378 req->r_request_pgid = p;
379 p += 8 + 4;
380 ceph_encode_32(&p, -1); /* preferred */
381
382 /* oid */
383 ceph_encode_32(&p, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600384 memcpy(p, req->r_oid, req->r_oid_len);
Sage Weil1b83bef2013-02-25 16:11:12 -0800385 dout("oid '%.*s' len %d\n", req->r_oid_len, req->r_oid, req->r_oid_len);
Alex Elderaf77f262012-11-09 08:43:15 -0600386 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700387
Sage Weil1b83bef2013-02-25 16:11:12 -0800388 /* ops */
389 ceph_encode_16(&p, num_ops);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700390 src_op = src_ops;
Sage Weil1b83bef2013-02-25 16:11:12 -0800391 req->r_request_ops = p;
392 for (i = 0; i < num_ops; i++, src_op++) {
393 osd_req_encode_op(req, p, src_op);
394 p += sizeof(struct ceph_osd_op);
395 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700396
Sage Weil1b83bef2013-02-25 16:11:12 -0800397 /* snaps */
398 ceph_encode_64(&p, req->r_snapid);
399 ceph_encode_64(&p, req->r_snapc ? req->r_snapc->seq : 0);
400 ceph_encode_32(&p, req->r_snapc ? req->r_snapc->num_snaps : 0);
401 if (req->r_snapc) {
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700402 for (i = 0; i < snapc->num_snaps; i++) {
Sage Weil1b83bef2013-02-25 16:11:12 -0800403 ceph_encode_64(&p, req->r_snapc->snaps[i]);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700404 }
405 }
406
Sage Weil1b83bef2013-02-25 16:11:12 -0800407 req->r_request_attempts = p;
408 p += 4;
409
Alex Elderf44246e2013-02-14 12:16:43 -0600410 data_len = req->r_trail.length;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700411 if (flags & CEPH_OSD_FLAG_WRITE) {
412 req->r_request->hdr.data_off = cpu_to_le16(off);
Alex Elderf44246e2013-02-14 12:16:43 -0600413 data_len += len;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700414 }
Alex Elderf44246e2013-02-14 12:16:43 -0600415 req->r_request->hdr.data_len = cpu_to_le32(data_len);
Sage Weilc5c6b192010-11-09 12:40:00 -0800416
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700417 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
418 msg_size = p - msg->front.iov_base;
419 msg->front.iov_len = msg_size;
420 msg->hdr.front_len = cpu_to_le32(msg_size);
Sage Weil1b83bef2013-02-25 16:11:12 -0800421
422 dout("build_request msg_size was %d num_ops %d\n", (int)msg_size,
423 num_ops);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700424 return;
425}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700426EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700427
Sage Weilf24e9982009-10-06 11:31:10 -0700428/*
429 * build new request AND message, calculate layout, and adjust file
430 * extent as needed.
431 *
432 * if the file was recently truncated, we include information about its
433 * old and new size so that the object can be updated appropriately. (we
434 * avoid synchronously deleting truncated objects because it's slow.)
435 *
436 * if @do_sync, include a 'startsync' command so that the osd will flush
437 * data quickly.
438 */
439struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
440 struct ceph_file_layout *layout,
441 struct ceph_vino vino,
442 u64 off, u64 *plen,
443 int opcode, int flags,
444 struct ceph_snap_context *snapc,
445 int do_sync,
446 u32 truncate_seq,
447 u64 truncate_size,
448 struct timespec *mtime,
Alex Elder153e5162013-03-01 18:00:15 -0600449 bool use_mempool)
Sage Weilf24e9982009-10-06 11:31:10 -0700450{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600451 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700452 struct ceph_osd_request *req;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600453 unsigned int num_op = 1;
Alex Elder47a05812013-02-15 22:10:17 -0600454 u64 bno = 0;
Sage Weil68162822012-09-24 21:01:02 -0700455 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700456
Alex Elderae7ca4a32012-11-13 21:11:15 -0600457 memset(&ops, 0, sizeof ops);
458
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700459 ops[0].op = opcode;
460 ops[0].extent.truncate_seq = truncate_seq;
461 ops[0].extent.truncate_size = truncate_size;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700462
463 if (do_sync) {
464 ops[1].op = CEPH_OSD_OP_STARTSYNC;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600465 num_op++;
466 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700467
Alex Elderae7ca4a32012-11-13 21:11:15 -0600468 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
469 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700470 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700471 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600472 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700473
474 /* calculate max write size */
Alex Elder60cf5992013-02-15 22:10:17 -0600475 r = calc_layout(layout, off, plen, ops, &bno);
Alex Elder3ff5f382013-02-15 22:10:17 -0600476 if (r < 0) {
477 ceph_osdc_put_request(req);
Sage Weil68162822012-09-24 21:01:02 -0700478 return ERR_PTR(r);
Alex Elder3ff5f382013-02-15 22:10:17 -0600479 }
Alex Elder47a05812013-02-15 22:10:17 -0600480
Sage Weilf24e9982009-10-06 11:31:10 -0700481 req->r_file_layout = *layout; /* keep a copy */
482
Alex Elderdbe0fc42013-02-15 22:10:17 -0600483 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
484 req->r_oid_len = strlen(req->r_oid);
485
Alex Elderae7ca4a32012-11-13 21:11:15 -0600486 ceph_osdc_build_request(req, off, *plen, num_op, ops,
487 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700488
Sage Weilf24e9982009-10-06 11:31:10 -0700489 return req;
490}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700491EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700492
493/*
494 * We keep osd requests in an rbtree, sorted by ->r_tid.
495 */
496static void __insert_request(struct ceph_osd_client *osdc,
497 struct ceph_osd_request *new)
498{
499 struct rb_node **p = &osdc->requests.rb_node;
500 struct rb_node *parent = NULL;
501 struct ceph_osd_request *req = NULL;
502
503 while (*p) {
504 parent = *p;
505 req = rb_entry(parent, struct ceph_osd_request, r_node);
506 if (new->r_tid < req->r_tid)
507 p = &(*p)->rb_left;
508 else if (new->r_tid > req->r_tid)
509 p = &(*p)->rb_right;
510 else
511 BUG();
512 }
513
514 rb_link_node(&new->r_node, parent, p);
515 rb_insert_color(&new->r_node, &osdc->requests);
516}
517
518static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
519 u64 tid)
520{
521 struct ceph_osd_request *req;
522 struct rb_node *n = osdc->requests.rb_node;
523
524 while (n) {
525 req = rb_entry(n, struct ceph_osd_request, r_node);
526 if (tid < req->r_tid)
527 n = n->rb_left;
528 else if (tid > req->r_tid)
529 n = n->rb_right;
530 else
531 return req;
532 }
533 return NULL;
534}
535
536static struct ceph_osd_request *
537__lookup_request_ge(struct ceph_osd_client *osdc,
538 u64 tid)
539{
540 struct ceph_osd_request *req;
541 struct rb_node *n = osdc->requests.rb_node;
542
543 while (n) {
544 req = rb_entry(n, struct ceph_osd_request, r_node);
545 if (tid < req->r_tid) {
546 if (!n->rb_left)
547 return req;
548 n = n->rb_left;
549 } else if (tid > req->r_tid) {
550 n = n->rb_right;
551 } else {
552 return req;
553 }
554 }
555 return NULL;
556}
557
Sage Weil6f6c7002011-01-17 20:34:08 -0800558/*
559 * Resubmit requests pending on the given osd.
560 */
561static void __kick_osd_requests(struct ceph_osd_client *osdc,
562 struct ceph_osd *osd)
563{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700564 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800565 int err;
566
567 dout("__kick_osd_requests osd%d\n", osd->o_osd);
568 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600569 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800570 return;
571
572 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
573 list_move(&req->r_req_lru_item, &osdc->req_unsent);
574 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
575 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700576 if (!req->r_linger)
577 req->r_flags |= CEPH_OSD_FLAG_RETRY;
578 }
579
580 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
581 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700582 /*
583 * reregister request prior to unregistering linger so
584 * that r_osd is preserved.
585 */
586 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700587 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700588 list_add(&req->r_req_lru_item, &osdc->req_unsent);
589 list_add(&req->r_osd_item, &req->r_osd->o_requests);
590 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700591 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
592 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800593 }
594}
595
Sage Weilf24e9982009-10-06 11:31:10 -0700596/*
Sage Weil81b024e2009-10-09 10:29:18 -0700597 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700598 */
599static void osd_reset(struct ceph_connection *con)
600{
601 struct ceph_osd *osd = con->private;
602 struct ceph_osd_client *osdc;
603
604 if (!osd)
605 return;
606 dout("osd_reset osd%d\n", osd->o_osd);
607 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700608 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800609 mutex_lock(&osdc->request_mutex);
610 __kick_osd_requests(osdc, osd);
Alex Elderf9d25192013-02-15 11:42:29 -0600611 __send_queued(osdc);
Sage Weil83aff952012-11-28 12:28:24 -0800612 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -0700613 up_read(&osdc->map_sem);
614}
615
616/*
617 * Track open sessions with osds.
618 */
Alex Eldere10006f2012-05-26 23:26:43 -0500619static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700620{
621 struct ceph_osd *osd;
622
623 osd = kzalloc(sizeof(*osd), GFP_NOFS);
624 if (!osd)
625 return NULL;
626
627 atomic_set(&osd->o_ref, 1);
628 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500629 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600630 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700631 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700632 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800633 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700634 osd->o_incarnation = 1;
635
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700636 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800637
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800638 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700639 return osd;
640}
641
642static struct ceph_osd *get_osd(struct ceph_osd *osd)
643{
644 if (atomic_inc_not_zero(&osd->o_ref)) {
645 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
646 atomic_read(&osd->o_ref));
647 return osd;
648 } else {
649 dout("get_osd %p FAIL\n", osd);
650 return NULL;
651 }
652}
653
654static void put_osd(struct ceph_osd *osd)
655{
656 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
657 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500658 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700659 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
660
Alex Eldera2556512012-05-16 15:16:39 -0500661 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500662 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700663 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700664 }
Sage Weilf24e9982009-10-06 11:31:10 -0700665}
666
667/*
668 * remove an osd from our map
669 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800670static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700671{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800672 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700673 BUG_ON(!list_empty(&osd->o_requests));
674 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800675 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700676 ceph_con_close(&osd->o_con);
677 put_osd(osd);
678}
679
Sage Weilaca420b2011-08-31 14:45:53 -0700680static void remove_all_osds(struct ceph_osd_client *osdc)
681{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500682 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700683 mutex_lock(&osdc->request_mutex);
684 while (!RB_EMPTY_ROOT(&osdc->osds)) {
685 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
686 struct ceph_osd, o_node);
687 __remove_osd(osdc, osd);
688 }
689 mutex_unlock(&osdc->request_mutex);
690}
691
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800692static void __move_osd_to_lru(struct ceph_osd_client *osdc,
693 struct ceph_osd *osd)
694{
695 dout("__move_osd_to_lru %p\n", osd);
696 BUG_ON(!list_empty(&osd->o_osd_lru));
697 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700698 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800699}
700
701static void __remove_osd_from_lru(struct ceph_osd *osd)
702{
703 dout("__remove_osd_from_lru %p\n", osd);
704 if (!list_empty(&osd->o_osd_lru))
705 list_del_init(&osd->o_osd_lru);
706}
707
Sage Weilaca420b2011-08-31 14:45:53 -0700708static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800709{
710 struct ceph_osd *osd, *nosd;
711
712 dout("__remove_old_osds %p\n", osdc);
713 mutex_lock(&osdc->request_mutex);
714 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700715 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800716 break;
717 __remove_osd(osdc, osd);
718 }
719 mutex_unlock(&osdc->request_mutex);
720}
721
Sage Weilf24e9982009-10-06 11:31:10 -0700722/*
723 * reset osd connect
724 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800725static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700726{
Alex Elderc3acb182012-12-07 09:57:58 -0600727 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700728
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800729 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700730 if (list_empty(&osd->o_requests) &&
731 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800732 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600733
734 return -ENODEV;
735 }
736
737 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
738 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
739 !ceph_con_opened(&osd->o_con)) {
740 struct ceph_osd_request *req;
741
Sage Weil87b315a2010-03-22 14:51:18 -0700742 dout(" osd addr hasn't changed and connection never opened,"
743 " letting msgr retry");
744 /* touch each r_stamp for handle_timeout()'s benfit */
745 list_for_each_entry(req, &osd->o_requests, r_osd_item)
746 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600747
748 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700749 }
Alex Elderc3acb182012-12-07 09:57:58 -0600750
751 ceph_con_close(&osd->o_con);
752 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
753 osd->o_incarnation++;
754
755 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700756}
757
758static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
759{
760 struct rb_node **p = &osdc->osds.rb_node;
761 struct rb_node *parent = NULL;
762 struct ceph_osd *osd = NULL;
763
Sage Weilaca420b2011-08-31 14:45:53 -0700764 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700765 while (*p) {
766 parent = *p;
767 osd = rb_entry(parent, struct ceph_osd, o_node);
768 if (new->o_osd < osd->o_osd)
769 p = &(*p)->rb_left;
770 else if (new->o_osd > osd->o_osd)
771 p = &(*p)->rb_right;
772 else
773 BUG();
774 }
775
776 rb_link_node(&new->o_node, parent, p);
777 rb_insert_color(&new->o_node, &osdc->osds);
778}
779
780static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
781{
782 struct ceph_osd *osd;
783 struct rb_node *n = osdc->osds.rb_node;
784
785 while (n) {
786 osd = rb_entry(n, struct ceph_osd, o_node);
787 if (o < osd->o_osd)
788 n = n->rb_left;
789 else if (o > osd->o_osd)
790 n = n->rb_right;
791 else
792 return osd;
793 }
794 return NULL;
795}
796
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800797static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
798{
799 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700800 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800801}
802
803static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
804{
805 cancel_delayed_work(&osdc->timeout_work);
806}
Sage Weilf24e9982009-10-06 11:31:10 -0700807
808/*
809 * Register request, assign tid. If this is the first request, set up
810 * the timeout event.
811 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700812static void __register_request(struct ceph_osd_client *osdc,
813 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700814{
Sage Weilf24e9982009-10-06 11:31:10 -0700815 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800816 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700817 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700818 __insert_request(osdc, req);
819 ceph_osdc_get_request(req);
820 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700821 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800822 dout(" first request, scheduling timeout\n");
823 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700824 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700825}
826
827static void register_request(struct ceph_osd_client *osdc,
828 struct ceph_osd_request *req)
829{
830 mutex_lock(&osdc->request_mutex);
831 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700832 mutex_unlock(&osdc->request_mutex);
833}
834
835/*
836 * called under osdc->request_mutex
837 */
838static void __unregister_request(struct ceph_osd_client *osdc,
839 struct ceph_osd_request *req)
840{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500841 if (RB_EMPTY_NODE(&req->r_node)) {
842 dout("__unregister_request %p tid %lld not registered\n",
843 req, req->r_tid);
844 return;
845 }
846
Sage Weilf24e9982009-10-06 11:31:10 -0700847 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
848 rb_erase(&req->r_node, &osdc->requests);
849 osdc->num_requests--;
850
Sage Weil0ba64782009-10-08 16:57:16 -0700851 if (req->r_osd) {
852 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500853 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700854
855 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700856 if (list_empty(&req->r_osd->o_requests) &&
857 list_empty(&req->r_osd->o_linger_requests)) {
858 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800859 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700860 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700861 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700862 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700863 }
Sage Weilf24e9982009-10-06 11:31:10 -0700864
Alex Elder7d5f2482012-11-29 08:37:03 -0600865 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700866 ceph_osdc_put_request(req);
867
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800868 if (osdc->num_requests == 0) {
869 dout(" no requests, canceling timeout\n");
870 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700871 }
872}
873
874/*
875 * Cancel a previously queued request message
876 */
877static void __cancel_request(struct ceph_osd_request *req)
878{
Sage Weil6bc18872010-09-27 10:18:52 -0700879 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500880 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700881 req->r_sent = 0;
882 }
883}
884
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700885static void __register_linger_request(struct ceph_osd_client *osdc,
886 struct ceph_osd_request *req)
887{
888 dout("__register_linger_request %p\n", req);
889 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700890 if (req->r_osd)
891 list_add_tail(&req->r_linger_osd,
892 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700893}
894
895static void __unregister_linger_request(struct ceph_osd_client *osdc,
896 struct ceph_osd_request *req)
897{
898 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -0600899 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700900 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700901 list_del_init(&req->r_linger_osd);
902
903 if (list_empty(&req->r_osd->o_requests) &&
904 list_empty(&req->r_osd->o_linger_requests)) {
905 dout("moving osd to %p lru\n", req->r_osd);
906 __move_osd_to_lru(osdc, req->r_osd);
907 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700908 if (list_empty(&req->r_osd_item))
909 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700910 }
911}
912
913void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
914 struct ceph_osd_request *req)
915{
916 mutex_lock(&osdc->request_mutex);
917 if (req->r_linger) {
918 __unregister_linger_request(osdc, req);
919 ceph_osdc_put_request(req);
920 }
921 mutex_unlock(&osdc->request_mutex);
922}
923EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
924
925void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
926 struct ceph_osd_request *req)
927{
928 if (!req->r_linger) {
929 dout("set_request_linger %p\n", req);
930 req->r_linger = 1;
931 /*
932 * caller is now responsible for calling
933 * unregister_linger_request
934 */
935 ceph_osdc_get_request(req);
936 }
937}
938EXPORT_SYMBOL(ceph_osdc_set_request_linger);
939
Sage Weilf24e9982009-10-06 11:31:10 -0700940/*
941 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
942 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300943 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800944 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700945 *
946 * Return 0 if unchanged, 1 if changed, or negative on error.
947 *
948 * Caller should hold map_sem for read and request_mutex.
949 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800950static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700951 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700952{
Sage Weil5b191d92013-02-23 10:38:16 -0800953 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700954 int acting[CEPH_PG_MAX_SIZE];
955 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700956 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700957
Sage Weil6f6c7002011-01-17 20:34:08 -0800958 dout("map_request %p tid %lld\n", req, req->r_tid);
Alex Elder41766f82013-03-01 18:00:15 -0600959 err = ceph_calc_ceph_pg(&pgid, req->r_oid, osdc->osdmap,
960 ceph_file_layout_pg_pool(req->r_file_layout));
Sage Weil6f6c7002011-01-17 20:34:08 -0800961 if (err) {
962 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700963 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800964 }
Sage Weil7740a422010-01-08 15:58:25 -0800965 req->r_pgid = pgid;
966
Sage Weild85b7052010-05-10 10:24:48 -0700967 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
968 if (err > 0) {
969 o = acting[0];
970 num = err;
971 }
Sage Weilf24e9982009-10-06 11:31:10 -0700972
Sage Weil38d64532011-10-14 13:33:55 -0700973 if ((!force_resend &&
974 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700975 req->r_sent >= req->r_osd->o_incarnation &&
976 req->r_num_pg_osds == num &&
977 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700978 (req->r_osd == NULL && o == -1))
979 return 0; /* no change */
980
Sage Weil5b191d92013-02-23 10:38:16 -0800981 dout("map_request tid %llu pgid %lld.%x osd%d (was osd%d)\n",
982 req->r_tid, pgid.pool, pgid.seed, o,
Sage Weilf24e9982009-10-06 11:31:10 -0700983 req->r_osd ? req->r_osd->o_osd : -1);
984
Sage Weild85b7052010-05-10 10:24:48 -0700985 /* record full pg acting set */
986 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
987 req->r_num_pg_osds = num;
988
Sage Weilf24e9982009-10-06 11:31:10 -0700989 if (req->r_osd) {
990 __cancel_request(req);
991 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700992 req->r_osd = NULL;
993 }
994
995 req->r_osd = __lookup_osd(osdc, o);
996 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800997 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -0500998 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -0800999 if (!req->r_osd) {
1000 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -08001001 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -08001002 }
Sage Weilf24e9982009-10-06 11:31:10 -07001003
Sage Weil6f6c7002011-01-17 20:34:08 -08001004 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -07001005 __insert_osd(osdc, req->r_osd);
1006
Sage Weilb7a9e5d2012-06-27 12:24:08 -07001007 ceph_con_open(&req->r_osd->o_con,
1008 CEPH_ENTITY_TYPE_OSD, o,
1009 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -07001010 }
1011
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001012 if (req->r_osd) {
1013 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001014 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -08001015 list_move(&req->r_req_lru_item, &osdc->req_unsent);
1016 } else {
1017 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001018 }
Sage Weild85b7052010-05-10 10:24:48 -07001019 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -07001020
1021out:
Sage Weilf24e9982009-10-06 11:31:10 -07001022 return err;
1023}
1024
1025/*
1026 * caller should hold map_sem (for read) and request_mutex
1027 */
Sage Weil56e925b2012-01-03 12:34:34 -08001028static void __send_request(struct ceph_osd_client *osdc,
1029 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -07001030{
Sage Weil1b83bef2013-02-25 16:11:12 -08001031 void *p;
Sage Weilf24e9982009-10-06 11:31:10 -07001032
Sage Weil1b83bef2013-02-25 16:11:12 -08001033 dout("send_request %p tid %llu to osd%d flags %d pg %lld.%x\n",
1034 req, req->r_tid, req->r_osd->o_osd, req->r_flags,
1035 (unsigned long long)req->r_pgid.pool, req->r_pgid.seed);
Sage Weilf24e9982009-10-06 11:31:10 -07001036
Sage Weil1b83bef2013-02-25 16:11:12 -08001037 /* fill in message content that changes each time we send it */
1038 put_unaligned_le32(osdc->osdmap->epoch, req->r_request_osdmap_epoch);
1039 put_unaligned_le32(req->r_flags, req->r_request_flags);
1040 put_unaligned_le64(req->r_pgid.pool, req->r_request_pool);
1041 p = req->r_request_pgid;
1042 ceph_encode_64(&p, req->r_pgid.pool);
1043 ceph_encode_32(&p, req->r_pgid.seed);
1044 put_unaligned_le64(1, req->r_request_attempts); /* FIXME */
1045 memcpy(req->r_request_reassert_version, &req->r_reassert_version,
1046 sizeof(req->r_reassert_version));
Sage Weil2169aea2013-02-25 16:13:08 -08001047
Sage Weil3dd72fc2010-03-22 14:42:30 -07001048 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -07001049 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001050
1051 ceph_msg_get(req->r_request); /* send consumes a ref */
1052 ceph_con_send(&req->r_osd->o_con, req->r_request);
1053 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -07001054}
1055
1056/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001057 * Send any requests in the queue (req_unsent).
1058 */
Alex Elderf9d25192013-02-15 11:42:29 -06001059static void __send_queued(struct ceph_osd_client *osdc)
Sage Weil6f6c7002011-01-17 20:34:08 -08001060{
1061 struct ceph_osd_request *req, *tmp;
1062
Alex Elderf9d25192013-02-15 11:42:29 -06001063 dout("__send_queued\n");
1064 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item)
Sage Weil6f6c7002011-01-17 20:34:08 -08001065 __send_request(osdc, req);
Sage Weil6f6c7002011-01-17 20:34:08 -08001066}
1067
1068/*
Sage Weilf24e9982009-10-06 11:31:10 -07001069 * Timeout callback, called every N seconds when 1 or more osd
1070 * requests has been active for more than N seconds. When this
1071 * happens, we ping all OSDs with requests who have timed out to
1072 * ensure any communications channel reset is detected. Reset the
1073 * request timeouts another N seconds in the future as we go.
1074 * Reschedule the timeout event another N seconds in future (unless
1075 * there are no open requests).
1076 */
1077static void handle_timeout(struct work_struct *work)
1078{
1079 struct ceph_osd_client *osdc =
1080 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001081 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001082 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001083 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001084 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001085 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001086 dout("timeout\n");
1087 down_read(&osdc->map_sem);
1088
1089 ceph_monc_request_next_osdmap(&osdc->client->monc);
1090
1091 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001092
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001093 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001094 * ping osds that are a bit slow. this ensures that if there
1095 * is a break in the TCP connection we will notice, and reopen
1096 * a connection with that osd (from the fault callback).
1097 */
1098 INIT_LIST_HEAD(&slow_osds);
1099 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001100 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001101 break;
1102
1103 osd = req->r_osd;
1104 BUG_ON(!osd);
1105 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001106 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001107 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1108 }
1109 while (!list_empty(&slow_osds)) {
1110 osd = list_entry(slow_osds.next, struct ceph_osd,
1111 o_keepalive_item);
1112 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001113 ceph_con_keepalive(&osd->o_con);
1114 }
1115
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001116 __schedule_osd_timeout(osdc);
Alex Elderf9d25192013-02-15 11:42:29 -06001117 __send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001118 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001119 up_read(&osdc->map_sem);
1120}
1121
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001122static void handle_osds_timeout(struct work_struct *work)
1123{
1124 struct ceph_osd_client *osdc =
1125 container_of(work, struct ceph_osd_client,
1126 osds_timeout_work.work);
1127 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001128 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001129
1130 dout("osds timeout\n");
1131 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001132 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001133 up_read(&osdc->map_sem);
1134
1135 schedule_delayed_work(&osdc->osds_timeout_work,
1136 round_jiffies_relative(delay));
1137}
1138
Sage Weil25845472011-06-03 09:37:09 -07001139static void complete_request(struct ceph_osd_request *req)
1140{
1141 if (req->r_safe_callback)
1142 req->r_safe_callback(req, NULL);
1143 complete_all(&req->r_safe_completion); /* fsync waiter */
1144}
1145
Sage Weil1b83bef2013-02-25 16:11:12 -08001146static int __decode_pgid(void **p, void *end, struct ceph_pg *pgid)
1147{
1148 __u8 v;
1149
1150 ceph_decode_need(p, end, 1 + 8 + 4 + 4, bad);
1151 v = ceph_decode_8(p);
1152 if (v > 1) {
1153 pr_warning("do not understand pg encoding %d > 1", v);
1154 return -EINVAL;
1155 }
1156 pgid->pool = ceph_decode_64(p);
1157 pgid->seed = ceph_decode_32(p);
1158 *p += 4;
1159 return 0;
1160
1161bad:
1162 pr_warning("incomplete pg encoding");
1163 return -EINVAL;
1164}
1165
Sage Weilf24e9982009-10-06 11:31:10 -07001166/*
1167 * handle osd op reply. either call the callback if it is specified,
1168 * or do the completion to wake up the waiting thread.
1169 */
Sage Weil350b1c32009-12-22 10:45:45 -08001170static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1171 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001172{
Sage Weil1b83bef2013-02-25 16:11:12 -08001173 void *p, *end;
Sage Weilf24e9982009-10-06 11:31:10 -07001174 struct ceph_osd_request *req;
1175 u64 tid;
Sage Weil1b83bef2013-02-25 16:11:12 -08001176 int object_len;
1177 int numops, payload_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001178 s32 result;
Sage Weil1b83bef2013-02-25 16:11:12 -08001179 s32 retry_attempt;
1180 struct ceph_pg pg;
1181 int err;
1182 u32 reassert_epoch;
1183 u64 reassert_version;
1184 u32 osdmap_epoch;
Alex Elder0d5af162013-02-27 10:26:25 -06001185 int already_completed;
Sage Weil1b83bef2013-02-25 16:11:12 -08001186 int i;
Sage Weilf24e9982009-10-06 11:31:10 -07001187
Sage Weil6df058c2009-12-22 11:24:33 -08001188 tid = le64_to_cpu(msg->hdr.tid);
Sage Weil1b83bef2013-02-25 16:11:12 -08001189 dout("handle_reply %p tid %llu\n", msg, tid);
1190
1191 p = msg->front.iov_base;
1192 end = p + msg->front.iov_len;
1193
1194 ceph_decode_need(&p, end, 4, bad);
1195 object_len = ceph_decode_32(&p);
1196 ceph_decode_need(&p, end, object_len, bad);
1197 p += object_len;
1198
1199 err = __decode_pgid(&p, end, &pg);
1200 if (err)
Sage Weilf24e9982009-10-06 11:31:10 -07001201 goto bad;
Sage Weil1b83bef2013-02-25 16:11:12 -08001202
1203 ceph_decode_need(&p, end, 8 + 4 + 4 + 8 + 4, bad);
1204 flags = ceph_decode_64(&p);
1205 result = ceph_decode_32(&p);
1206 reassert_epoch = ceph_decode_32(&p);
1207 reassert_version = ceph_decode_64(&p);
1208 osdmap_epoch = ceph_decode_32(&p);
1209
Sage Weilf24e9982009-10-06 11:31:10 -07001210 /* lookup */
1211 mutex_lock(&osdc->request_mutex);
1212 req = __lookup_request(osdc, tid);
1213 if (req == NULL) {
1214 dout("handle_reply tid %llu dne\n", tid);
1215 mutex_unlock(&osdc->request_mutex);
1216 return;
1217 }
1218 ceph_osdc_get_request(req);
Sage Weil1b83bef2013-02-25 16:11:12 -08001219
1220 dout("handle_reply %p tid %llu req %p result %d\n", msg, tid,
1221 req, result);
1222
1223 ceph_decode_need(&p, end, 4, bad);
1224 numops = ceph_decode_32(&p);
1225 if (numops > CEPH_OSD_MAX_OP)
1226 goto bad_put;
1227 if (numops != req->r_num_ops)
1228 goto bad_put;
1229 payload_len = 0;
1230 ceph_decode_need(&p, end, numops * sizeof(struct ceph_osd_op), bad);
1231 for (i = 0; i < numops; i++) {
1232 struct ceph_osd_op *op = p;
1233 int len;
1234
1235 len = le32_to_cpu(op->payload_len);
1236 req->r_reply_op_len[i] = len;
1237 dout(" op %d has %d bytes\n", i, len);
1238 payload_len += len;
1239 p += sizeof(*op);
1240 }
1241 if (payload_len != le32_to_cpu(msg->hdr.data_len)) {
1242 pr_warning("sum of op payload lens %d != data_len %d",
1243 payload_len, le32_to_cpu(msg->hdr.data_len));
1244 goto bad_put;
1245 }
1246
1247 ceph_decode_need(&p, end, 4 + numops * 4, bad);
1248 retry_attempt = ceph_decode_32(&p);
1249 for (i = 0; i < numops; i++)
1250 req->r_reply_op_result[i] = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001251
Sage Weil350b1c32009-12-22 10:45:45 -08001252 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001253 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001254 * avoid a (safe but slower) revoke later.
1255 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001256 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001257 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001258 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001259 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001260 }
1261
Sage Weilf24e9982009-10-06 11:31:10 -07001262 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001263 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001264
Sage Weil1b83bef2013-02-25 16:11:12 -08001265 req->r_result = result;
Sage Weilf24e9982009-10-06 11:31:10 -07001266 bytes = le32_to_cpu(msg->hdr.data_len);
1267 dout("handle_reply result %d bytes %d\n", req->r_result,
1268 bytes);
1269 if (req->r_result == 0)
1270 req->r_result = bytes;
1271
1272 /* in case this is a write and we need to replay, */
Sage Weil1b83bef2013-02-25 16:11:12 -08001273 req->r_reassert_version.epoch = cpu_to_le32(reassert_epoch);
1274 req->r_reassert_version.version = cpu_to_le64(reassert_version);
Sage Weilf24e9982009-10-06 11:31:10 -07001275
1276 req->r_got_reply = 1;
1277 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1278 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001279 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001280 goto done;
1281 }
1282
1283 dout("handle_reply tid %llu flags %d\n", tid, flags);
1284
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001285 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1286 __register_linger_request(osdc, req);
1287
Sage Weilf24e9982009-10-06 11:31:10 -07001288 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001289 if (result < 0 ||
1290 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001291 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1292 __unregister_request(osdc, req);
1293
Alex Elder0d5af162013-02-27 10:26:25 -06001294 already_completed = req->r_completed;
1295 req->r_completed = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001296 mutex_unlock(&osdc->request_mutex);
Alex Elder0d5af162013-02-27 10:26:25 -06001297 if (already_completed)
1298 goto done;
Sage Weilf24e9982009-10-06 11:31:10 -07001299
1300 if (req->r_callback)
1301 req->r_callback(req, msg);
1302 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001303 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001304
Sage Weil25845472011-06-03 09:37:09 -07001305 if (flags & CEPH_OSD_FLAG_ONDISK)
1306 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001307
1308done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001309 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001310 ceph_osdc_put_request(req);
1311 return;
1312
Sage Weil1b83bef2013-02-25 16:11:12 -08001313bad_put:
1314 ceph_osdc_put_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001315bad:
Sage Weil1b83bef2013-02-25 16:11:12 -08001316 pr_err("corrupt osd_op_reply got %d %d\n",
1317 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001318 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001319}
1320
Sage Weil6f6c7002011-01-17 20:34:08 -08001321static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001322{
Sage Weilf24e9982009-10-06 11:31:10 -07001323 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001324
Sage Weil6f6c7002011-01-17 20:34:08 -08001325 for (p = rb_first(&osdc->osds); p; p = n) {
1326 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001327
Sage Weil6f6c7002011-01-17 20:34:08 -08001328 n = rb_next(p);
1329 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1330 memcmp(&osd->o_con.peer_addr,
1331 ceph_osd_addr(osdc->osdmap,
1332 osd->o_osd),
1333 sizeof(struct ceph_entity_addr)) != 0)
1334 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001335 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001336}
1337
1338/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001339 * Requeue requests whose mapping to an OSD has changed. If requests map to
1340 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001341 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001342 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001343 */
Sage Weil38d64532011-10-14 13:33:55 -07001344static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001345{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001346 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001347 struct rb_node *p;
1348 int needmap = 0;
1349 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001350
Sage Weil38d64532011-10-14 13:33:55 -07001351 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001352 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001353 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001354 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001355 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001356
1357 /*
1358 * For linger requests that have not yet been
1359 * registered, move them to the linger list; they'll
1360 * be sent to the osd in the loop below. Unregister
1361 * the request before re-registering it as a linger
1362 * request to ensure the __map_request() below
1363 * will decide it needs to be sent.
1364 */
1365 if (req->r_linger && list_empty(&req->r_linger_item)) {
1366 dout("%p tid %llu restart on osd%d\n",
1367 req, req->r_tid,
1368 req->r_osd ? req->r_osd->o_osd : -1);
1369 __unregister_request(osdc, req);
1370 __register_linger_request(osdc, req);
1371 continue;
1372 }
1373
Sage Weil38d64532011-10-14 13:33:55 -07001374 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001375 if (err < 0)
1376 continue; /* error */
1377 if (req->r_osd == NULL) {
1378 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1379 needmap++; /* request a newer map */
1380 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001381 if (!req->r_linger) {
1382 dout("%p tid %llu requeued on osd%d\n", req,
1383 req->r_tid,
1384 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001385 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001386 }
1387 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001388 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001389
1390 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1391 r_linger_item) {
1392 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1393
Sage Weil38d64532011-10-14 13:33:55 -07001394 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001395 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001396 if (err == 0)
1397 continue; /* no change and no osd was specified */
1398 if (err < 0)
1399 continue; /* hrm! */
1400 if (req->r_osd == NULL) {
1401 dout("tid %llu maps to no valid osd\n", req->r_tid);
1402 needmap++; /* request a newer map */
1403 continue;
1404 }
1405
1406 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1407 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001408 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001409 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001410 }
Sage Weilf24e9982009-10-06 11:31:10 -07001411 mutex_unlock(&osdc->request_mutex);
1412
1413 if (needmap) {
1414 dout("%d requests for down osds, need new map\n", needmap);
1415 ceph_monc_request_next_osdmap(&osdc->client->monc);
1416 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001417 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001418}
Sage Weil6f6c7002011-01-17 20:34:08 -08001419
1420
Sage Weilf24e9982009-10-06 11:31:10 -07001421/*
1422 * Process updated osd map.
1423 *
1424 * The message contains any number of incremental and full maps, normally
1425 * indicating some sort of topology change in the cluster. Kick requests
1426 * off to different OSDs as needed.
1427 */
1428void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1429{
1430 void *p, *end, *next;
1431 u32 nr_maps, maplen;
1432 u32 epoch;
1433 struct ceph_osdmap *newmap = NULL, *oldmap;
1434 int err;
1435 struct ceph_fsid fsid;
1436
1437 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1438 p = msg->front.iov_base;
1439 end = p + msg->front.iov_len;
1440
1441 /* verify fsid */
1442 ceph_decode_need(&p, end, sizeof(fsid), bad);
1443 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001444 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1445 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001446
1447 down_write(&osdc->map_sem);
1448
1449 /* incremental maps */
1450 ceph_decode_32_safe(&p, end, nr_maps, bad);
1451 dout(" %d inc maps\n", nr_maps);
1452 while (nr_maps > 0) {
1453 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001454 epoch = ceph_decode_32(&p);
1455 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001456 ceph_decode_need(&p, end, maplen, bad);
1457 next = p + maplen;
1458 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1459 dout("applying incremental map %u len %d\n",
1460 epoch, maplen);
1461 newmap = osdmap_apply_incremental(&p, next,
1462 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001463 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001464 if (IS_ERR(newmap)) {
1465 err = PTR_ERR(newmap);
1466 goto bad;
1467 }
Sage Weil30dc6382009-12-21 14:49:37 -08001468 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001469 if (newmap != osdc->osdmap) {
1470 ceph_osdmap_destroy(osdc->osdmap);
1471 osdc->osdmap = newmap;
1472 }
Sage Weil38d64532011-10-14 13:33:55 -07001473 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001474 } else {
1475 dout("ignoring incremental map %u len %d\n",
1476 epoch, maplen);
1477 }
1478 p = next;
1479 nr_maps--;
1480 }
1481 if (newmap)
1482 goto done;
1483
1484 /* full maps */
1485 ceph_decode_32_safe(&p, end, nr_maps, bad);
1486 dout(" %d full maps\n", nr_maps);
1487 while (nr_maps) {
1488 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001489 epoch = ceph_decode_32(&p);
1490 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001491 ceph_decode_need(&p, end, maplen, bad);
1492 if (nr_maps > 1) {
1493 dout("skipping non-latest full map %u len %d\n",
1494 epoch, maplen);
1495 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1496 dout("skipping full map %u len %d, "
1497 "older than our %u\n", epoch, maplen,
1498 osdc->osdmap->epoch);
1499 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001500 int skipped_map = 0;
1501
Sage Weilf24e9982009-10-06 11:31:10 -07001502 dout("taking full map %u len %d\n", epoch, maplen);
1503 newmap = osdmap_decode(&p, p+maplen);
1504 if (IS_ERR(newmap)) {
1505 err = PTR_ERR(newmap);
1506 goto bad;
1507 }
Sage Weil30dc6382009-12-21 14:49:37 -08001508 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001509 oldmap = osdc->osdmap;
1510 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001511 if (oldmap) {
1512 if (oldmap->epoch + 1 < newmap->epoch)
1513 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001514 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001515 }
1516 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001517 }
1518 p += maplen;
1519 nr_maps--;
1520 }
1521
1522done:
1523 downgrade_write(&osdc->map_sem);
1524 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001525
1526 /*
1527 * subscribe to subsequent osdmap updates if full to ensure
1528 * we find out when we are no longer full and stop returning
1529 * ENOSPC.
1530 */
1531 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1532 ceph_monc_request_next_osdmap(&osdc->client->monc);
1533
Alex Elderf9d25192013-02-15 11:42:29 -06001534 mutex_lock(&osdc->request_mutex);
1535 __send_queued(osdc);
1536 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001537 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001538 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001539 return;
1540
1541bad:
1542 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001543 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001544 up_write(&osdc->map_sem);
1545 return;
1546}
1547
Sage Weilf24e9982009-10-06 11:31:10 -07001548/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001549 * watch/notify callback event infrastructure
1550 *
1551 * These callbacks are used both for watch and notify operations.
1552 */
1553static void __release_event(struct kref *kref)
1554{
1555 struct ceph_osd_event *event =
1556 container_of(kref, struct ceph_osd_event, kref);
1557
1558 dout("__release_event %p\n", event);
1559 kfree(event);
1560}
1561
1562static void get_event(struct ceph_osd_event *event)
1563{
1564 kref_get(&event->kref);
1565}
1566
1567void ceph_osdc_put_event(struct ceph_osd_event *event)
1568{
1569 kref_put(&event->kref, __release_event);
1570}
1571EXPORT_SYMBOL(ceph_osdc_put_event);
1572
1573static void __insert_event(struct ceph_osd_client *osdc,
1574 struct ceph_osd_event *new)
1575{
1576 struct rb_node **p = &osdc->event_tree.rb_node;
1577 struct rb_node *parent = NULL;
1578 struct ceph_osd_event *event = NULL;
1579
1580 while (*p) {
1581 parent = *p;
1582 event = rb_entry(parent, struct ceph_osd_event, node);
1583 if (new->cookie < event->cookie)
1584 p = &(*p)->rb_left;
1585 else if (new->cookie > event->cookie)
1586 p = &(*p)->rb_right;
1587 else
1588 BUG();
1589 }
1590
1591 rb_link_node(&new->node, parent, p);
1592 rb_insert_color(&new->node, &osdc->event_tree);
1593}
1594
1595static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1596 u64 cookie)
1597{
1598 struct rb_node **p = &osdc->event_tree.rb_node;
1599 struct rb_node *parent = NULL;
1600 struct ceph_osd_event *event = NULL;
1601
1602 while (*p) {
1603 parent = *p;
1604 event = rb_entry(parent, struct ceph_osd_event, node);
1605 if (cookie < event->cookie)
1606 p = &(*p)->rb_left;
1607 else if (cookie > event->cookie)
1608 p = &(*p)->rb_right;
1609 else
1610 return event;
1611 }
1612 return NULL;
1613}
1614
1615static void __remove_event(struct ceph_osd_event *event)
1616{
1617 struct ceph_osd_client *osdc = event->osdc;
1618
1619 if (!RB_EMPTY_NODE(&event->node)) {
1620 dout("__remove_event removed %p\n", event);
1621 rb_erase(&event->node, &osdc->event_tree);
1622 ceph_osdc_put_event(event);
1623 } else {
1624 dout("__remove_event didn't remove %p\n", event);
1625 }
1626}
1627
1628int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1629 void (*event_cb)(u64, u64, u8, void *),
Alex Elder3c663bb2013-02-15 11:42:30 -06001630 void *data, struct ceph_osd_event **pevent)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001631{
1632 struct ceph_osd_event *event;
1633
1634 event = kmalloc(sizeof(*event), GFP_NOIO);
1635 if (!event)
1636 return -ENOMEM;
1637
1638 dout("create_event %p\n", event);
1639 event->cb = event_cb;
Alex Elder3c663bb2013-02-15 11:42:30 -06001640 event->one_shot = 0;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001641 event->data = data;
1642 event->osdc = osdc;
1643 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001644 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001645 kref_init(&event->kref); /* one ref for us */
1646 kref_get(&event->kref); /* one ref for the caller */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001647
1648 spin_lock(&osdc->event_lock);
1649 event->cookie = ++osdc->event_count;
1650 __insert_event(osdc, event);
1651 spin_unlock(&osdc->event_lock);
1652
1653 *pevent = event;
1654 return 0;
1655}
1656EXPORT_SYMBOL(ceph_osdc_create_event);
1657
1658void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1659{
1660 struct ceph_osd_client *osdc = event->osdc;
1661
1662 dout("cancel_event %p\n", event);
1663 spin_lock(&osdc->event_lock);
1664 __remove_event(event);
1665 spin_unlock(&osdc->event_lock);
1666 ceph_osdc_put_event(event); /* caller's */
1667}
1668EXPORT_SYMBOL(ceph_osdc_cancel_event);
1669
1670
1671static void do_event_work(struct work_struct *work)
1672{
1673 struct ceph_osd_event_work *event_work =
1674 container_of(work, struct ceph_osd_event_work, work);
1675 struct ceph_osd_event *event = event_work->event;
1676 u64 ver = event_work->ver;
1677 u64 notify_id = event_work->notify_id;
1678 u8 opcode = event_work->opcode;
1679
1680 dout("do_event_work completing %p\n", event);
1681 event->cb(ver, notify_id, opcode, event->data);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001682 dout("do_event_work completed %p\n", event);
1683 ceph_osdc_put_event(event);
1684 kfree(event_work);
1685}
1686
1687
1688/*
1689 * Process osd watch notifications
1690 */
Alex Elder3c663bb2013-02-15 11:42:30 -06001691static void handle_watch_notify(struct ceph_osd_client *osdc,
1692 struct ceph_msg *msg)
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001693{
1694 void *p, *end;
1695 u8 proto_ver;
1696 u64 cookie, ver, notify_id;
1697 u8 opcode;
1698 struct ceph_osd_event *event;
1699 struct ceph_osd_event_work *event_work;
1700
1701 p = msg->front.iov_base;
1702 end = p + msg->front.iov_len;
1703
1704 ceph_decode_8_safe(&p, end, proto_ver, bad);
1705 ceph_decode_8_safe(&p, end, opcode, bad);
1706 ceph_decode_64_safe(&p, end, cookie, bad);
1707 ceph_decode_64_safe(&p, end, ver, bad);
1708 ceph_decode_64_safe(&p, end, notify_id, bad);
1709
1710 spin_lock(&osdc->event_lock);
1711 event = __find_event(osdc, cookie);
1712 if (event) {
Alex Elder3c663bb2013-02-15 11:42:30 -06001713 BUG_ON(event->one_shot);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001714 get_event(event);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001715 }
1716 spin_unlock(&osdc->event_lock);
1717 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1718 cookie, ver, event);
1719 if (event) {
1720 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001721 if (!event_work) {
1722 dout("ERROR: could not allocate event_work\n");
1723 goto done_err;
1724 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001725 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001726 event_work->event = event;
1727 event_work->ver = ver;
1728 event_work->notify_id = notify_id;
1729 event_work->opcode = opcode;
1730 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1731 dout("WARNING: failed to queue notify event work\n");
1732 goto done_err;
1733 }
1734 }
1735
1736 return;
1737
1738done_err:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001739 ceph_osdc_put_event(event);
1740 return;
1741
1742bad:
1743 pr_err("osdc handle_watch_notify corrupt msg\n");
1744 return;
1745}
1746
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001747/*
Sage Weilf24e9982009-10-06 11:31:10 -07001748 * Register request, send initial attempt.
1749 */
1750int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1751 struct ceph_osd_request *req,
1752 bool nofail)
1753{
Sage Weilc1ea8822009-10-08 16:55:47 -07001754 int rc = 0;
Alex Elder0fff87e2013-02-14 12:16:43 -06001755 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07001756
Alex Elder0fff87e2013-02-14 12:16:43 -06001757 /* Set up outgoing data */
1758
1759 osd_data = &req->r_data_out;
1760 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elderf1baeb22013-03-07 15:38:26 -06001761 BUG_ON(osd_data->length > (u64) SIZE_MAX);
Alex Elderebf18f42013-03-04 22:29:57 -06001762 if (osd_data->length)
1763 ceph_msg_data_set_pages(req->r_request,
1764 osd_data->pages, osd_data->length,
1765 osd_data->alignment);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001766#ifdef CONFIG_BLOCK
Alex Elder0fff87e2013-02-14 12:16:43 -06001767 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder27fa8382013-02-14 12:16:43 -06001768 ceph_msg_data_set_bio(req->r_request, osd_data->bio);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001769#endif
Alex Elder2ac2b7a2013-02-14 12:16:43 -06001770 } else {
Alex Elder0fff87e2013-02-14 12:16:43 -06001771 BUG_ON(osd_data->type != CEPH_OSD_DATA_TYPE_NONE);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06001772 }
Alex Elderebf18f42013-03-04 22:29:57 -06001773 if (req->r_trail.length)
1774 ceph_msg_data_set_trail(req->r_request, &req->r_trail);
Sage Weilf24e9982009-10-06 11:31:10 -07001775
1776 register_request(osdc, req);
1777
1778 down_read(&osdc->map_sem);
1779 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001780 /*
1781 * a racing kick_requests() may have sent the message for us
1782 * while we dropped request_mutex above, so only send now if
1783 * the request still han't been touched yet.
1784 */
1785 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001786 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001787 if (rc < 0) {
1788 if (nofail) {
1789 dout("osdc_start_request failed map, "
1790 " will retry %lld\n", req->r_tid);
1791 rc = 0;
1792 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001793 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001794 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001795 if (req->r_osd == NULL) {
1796 dout("send_request %p no up osds in pg\n", req);
1797 ceph_monc_request_next_osdmap(&osdc->client->monc);
1798 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001799 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001800 }
Sage Weil56e925b2012-01-03 12:34:34 -08001801 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001802 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001803
1804out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001805 mutex_unlock(&osdc->request_mutex);
1806 up_read(&osdc->map_sem);
1807 return rc;
1808}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001809EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001810
1811/*
1812 * wait for a request to complete
1813 */
1814int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1815 struct ceph_osd_request *req)
1816{
1817 int rc;
1818
1819 rc = wait_for_completion_interruptible(&req->r_completion);
1820 if (rc < 0) {
1821 mutex_lock(&osdc->request_mutex);
1822 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001823 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001824 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001825 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001826 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001827 return rc;
1828 }
1829
1830 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1831 return req->r_result;
1832}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001833EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001834
1835/*
1836 * sync - wait for all in-flight requests to flush. avoid starvation.
1837 */
1838void ceph_osdc_sync(struct ceph_osd_client *osdc)
1839{
1840 struct ceph_osd_request *req;
1841 u64 last_tid, next_tid = 0;
1842
1843 mutex_lock(&osdc->request_mutex);
1844 last_tid = osdc->last_tid;
1845 while (1) {
1846 req = __lookup_request_ge(osdc, next_tid);
1847 if (!req)
1848 break;
1849 if (req->r_tid > last_tid)
1850 break;
1851
1852 next_tid = req->r_tid + 1;
1853 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1854 continue;
1855
1856 ceph_osdc_get_request(req);
1857 mutex_unlock(&osdc->request_mutex);
1858 dout("sync waiting on tid %llu (last is %llu)\n",
1859 req->r_tid, last_tid);
1860 wait_for_completion(&req->r_safe_completion);
1861 mutex_lock(&osdc->request_mutex);
1862 ceph_osdc_put_request(req);
1863 }
1864 mutex_unlock(&osdc->request_mutex);
1865 dout("sync done (thru tid %llu)\n", last_tid);
1866}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001867EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001868
1869/*
1870 * init, shutdown
1871 */
1872int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1873{
1874 int err;
1875
1876 dout("init\n");
1877 osdc->client = client;
1878 osdc->osdmap = NULL;
1879 init_rwsem(&osdc->map_sem);
1880 init_completion(&osdc->map_waiters);
1881 osdc->last_requested_map = 0;
1882 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001883 osdc->last_tid = 0;
1884 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001885 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001886 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001887 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001888 INIT_LIST_HEAD(&osdc->req_unsent);
1889 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001890 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001891 osdc->num_requests = 0;
1892 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001893 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001894 spin_lock_init(&osdc->event_lock);
1895 osdc->event_tree = RB_ROOT;
1896 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001897
1898 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001899 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001900
Sage Weil5f44f142009-11-18 14:52:18 -08001901 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001902 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1903 sizeof(struct ceph_osd_request));
1904 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001905 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001906
Sage Weild50b4092012-07-09 14:22:34 -07001907 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1908 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001909 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001910 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001911 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001912 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001913 OSD_OPREPLY_FRONT_LEN, 10, true,
1914 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001915 if (err < 0)
1916 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001917
1918 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1919 if (IS_ERR(osdc->notify_wq)) {
1920 err = PTR_ERR(osdc->notify_wq);
1921 osdc->notify_wq = NULL;
1922 goto out_msgpool;
1923 }
Sage Weilf24e9982009-10-06 11:31:10 -07001924 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001925
Sage Weilc16e7862010-03-01 13:02:00 -08001926out_msgpool:
1927 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001928out_mempool:
1929 mempool_destroy(osdc->req_mempool);
1930out:
1931 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001932}
1933
1934void ceph_osdc_stop(struct ceph_osd_client *osdc)
1935{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001936 flush_workqueue(osdc->notify_wq);
1937 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001938 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001939 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001940 if (osdc->osdmap) {
1941 ceph_osdmap_destroy(osdc->osdmap);
1942 osdc->osdmap = NULL;
1943 }
Sage Weilaca420b2011-08-31 14:45:53 -07001944 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001945 mempool_destroy(osdc->req_mempool);
1946 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001947 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001948}
1949
1950/*
1951 * Read some contiguous pages. If we cross a stripe boundary, shorten
1952 * *plen. Return number of bytes read, or error.
1953 */
1954int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1955 struct ceph_vino vino, struct ceph_file_layout *layout,
1956 u64 off, u64 *plen,
1957 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001958 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001959{
1960 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06001961 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07001962 int rc = 0;
1963
1964 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1965 vino.snap, off, *plen);
1966 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1967 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1968 NULL, 0, truncate_seq, truncate_size, NULL,
Alex Elder153e5162013-03-01 18:00:15 -06001969 false);
Sage Weil68162822012-09-24 21:01:02 -07001970 if (IS_ERR(req))
1971 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001972
1973 /* it may be a short read due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06001974
1975 osd_data = &req->r_data_in;
1976 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
1977 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06001978 osd_data->length = *plen;
Alex Elder0fff87e2013-02-14 12:16:43 -06001979 osd_data->alignment = page_align;
Sage Weilf24e9982009-10-06 11:31:10 -07001980
Alex Eldere0c59482013-03-07 15:38:25 -06001981 dout("readpages final extent is %llu~%llu (%llu bytes align %d)\n",
1982 off, *plen, osd_data->length, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001983
1984 rc = ceph_osdc_start_request(osdc, req, false);
1985 if (!rc)
1986 rc = ceph_osdc_wait_request(osdc, req);
1987
1988 ceph_osdc_put_request(req);
1989 dout("readpages result %d\n", rc);
1990 return rc;
1991}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001992EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001993
1994/*
1995 * do a synchronous write on N pages
1996 */
1997int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1998 struct ceph_file_layout *layout,
1999 struct ceph_snap_context *snapc,
2000 u64 off, u64 len,
2001 u32 truncate_seq, u64 truncate_size,
2002 struct timespec *mtime,
Alex Elder24808822013-02-15 11:42:29 -06002003 struct page **pages, int num_pages)
Sage Weilf24e9982009-10-06 11:31:10 -07002004{
2005 struct ceph_osd_request *req;
Alex Elder0fff87e2013-02-14 12:16:43 -06002006 struct ceph_osd_data *osd_data;
Sage Weilf24e9982009-10-06 11:31:10 -07002007 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08002008 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07002009
2010 BUG_ON(vino.snap != CEPH_NOSNAP);
2011 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
2012 CEPH_OSD_OP_WRITE,
Alex Elder24808822013-02-15 11:42:29 -06002013 CEPH_OSD_FLAG_ONDISK | CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06002014 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07002015 truncate_seq, truncate_size, mtime,
Alex Elder153e5162013-03-01 18:00:15 -06002016 true);
Sage Weil68162822012-09-24 21:01:02 -07002017 if (IS_ERR(req))
2018 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07002019
2020 /* it may be a short write due to an object boundary */
Alex Elder0fff87e2013-02-14 12:16:43 -06002021 osd_data = &req->r_data_out;
2022 osd_data->type = CEPH_OSD_DATA_TYPE_PAGES;
2023 osd_data->pages = pages;
Alex Eldere0c59482013-03-07 15:38:25 -06002024 osd_data->length = len;
Alex Elder0fff87e2013-02-14 12:16:43 -06002025 osd_data->alignment = page_align;
Alex Eldere0c59482013-03-07 15:38:25 -06002026 dout("writepages %llu~%llu (%llu bytes)\n", off, len, osd_data->length);
Sage Weilf24e9982009-10-06 11:31:10 -07002027
Alex Elder87f979d2013-02-15 11:42:29 -06002028 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07002029 if (!rc)
2030 rc = ceph_osdc_wait_request(osdc, req);
2031
2032 ceph_osdc_put_request(req);
2033 if (rc == 0)
2034 rc = len;
2035 dout("writepages result %d\n", rc);
2036 return rc;
2037}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07002038EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07002039
2040/*
2041 * handle incoming message
2042 */
2043static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
2044{
2045 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01002046 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002047 int type = le16_to_cpu(msg->hdr.type);
2048
2049 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07002050 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01002051 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07002052
2053 switch (type) {
2054 case CEPH_MSG_OSD_MAP:
2055 ceph_osdc_handle_map(osdc, msg);
2056 break;
2057 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08002058 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07002059 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002060 case CEPH_MSG_WATCH_NOTIFY:
2061 handle_watch_notify(osdc, msg);
2062 break;
Sage Weilf24e9982009-10-06 11:31:10 -07002063
2064 default:
2065 pr_err("received unknown message type %d %s\n", type,
2066 ceph_msg_type_name(type));
2067 }
Sage Weil4a32f932010-06-13 10:27:53 -07002068out:
Sage Weilf24e9982009-10-06 11:31:10 -07002069 ceph_msg_put(msg);
2070}
2071
Sage Weil5b3a4db2010-02-19 21:43:23 -08002072/*
Sage Weil21b667f2010-03-04 10:22:59 -08002073 * lookup and return message for incoming reply. set up reply message
2074 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08002075 */
2076static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08002077 struct ceph_msg_header *hdr,
2078 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07002079{
2080 struct ceph_osd *osd = con->private;
2081 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08002082 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002083 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002084 int front = le32_to_cpu(hdr->front_len);
2085 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002086 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07002087
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002088 tid = le64_to_cpu(hdr->tid);
2089 mutex_lock(&osdc->request_mutex);
2090 req = __lookup_request(osdc, tid);
2091 if (!req) {
2092 *skip = 1;
2093 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07002094 dout("get_reply unknown tid %llu from osd%d\n", tid,
2095 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002096 goto out;
2097 }
Sage Weilc16e7862010-03-01 13:02:00 -08002098
2099 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05002100 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08002101 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05002102 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07002103 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07002104 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07002105 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08002106
Sage Weilc16e7862010-03-01 13:02:00 -08002107 if (front > req->r_reply->front.iov_len) {
2108 pr_warning("get_reply front %d > preallocated %d\n",
2109 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07002110 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07002111 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08002112 goto out;
2113 ceph_msg_put(req->r_reply);
2114 req->r_reply = m;
2115 }
2116 m = ceph_msg_get(req->r_reply);
2117
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002118 if (data_len > 0) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002119 struct ceph_osd_data *osd_data = &req->r_data_in;
2120
2121 if (osd_data->type == CEPH_OSD_DATA_TYPE_PAGES) {
Alex Elder0fff87e2013-02-14 12:16:43 -06002122 if (osd_data->pages &&
Alex Eldere0c59482013-03-07 15:38:25 -06002123 unlikely(osd_data->length < data_len)) {
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002124
Alex Eldere0c59482013-03-07 15:38:25 -06002125 pr_warning("tid %lld reply has %d bytes "
2126 "we had only %llu bytes ready\n",
2127 tid, data_len, osd_data->length);
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002128 *skip = 1;
2129 ceph_msg_put(m);
2130 m = NULL;
2131 goto out;
2132 }
Alex Elderf1baeb22013-03-07 15:38:26 -06002133 BUG_ON(osd_data->length > (u64) SIZE_MAX);
Alex Elder02afca62013-02-14 12:16:43 -06002134 ceph_msg_data_set_pages(m, osd_data->pages,
Alex Elderf1baeb22013-03-07 15:38:26 -06002135 osd_data->length, osd_data->alignment);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002136#ifdef CONFIG_BLOCK
Alex Elder0fff87e2013-02-14 12:16:43 -06002137 } else if (osd_data->type == CEPH_OSD_DATA_TYPE_BIO) {
Alex Elder27fa8382013-02-14 12:16:43 -06002138 ceph_msg_data_set_bio(m, osd_data->bio);
Yehuda Sadeh68b44762010-04-06 15:01:27 -07002139#endif
Alex Elder2ac2b7a2013-02-14 12:16:43 -06002140 }
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002141 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002142 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002143 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002144 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002145
2146out:
2147 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002148 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002149
2150}
2151
2152static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2153 struct ceph_msg_header *hdr,
2154 int *skip)
2155{
2156 struct ceph_osd *osd = con->private;
2157 int type = le16_to_cpu(hdr->type);
2158 int front = le32_to_cpu(hdr->front_len);
2159
Alex Elder1c20f2d2012-06-04 14:43:32 -05002160 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002161 switch (type) {
2162 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002163 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002164 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002165 case CEPH_MSG_OSD_OPREPLY:
2166 return get_reply(con, hdr, skip);
2167 default:
2168 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2169 osd->o_osd);
2170 *skip = 1;
2171 return NULL;
2172 }
Sage Weilf24e9982009-10-06 11:31:10 -07002173}
2174
2175/*
2176 * Wrappers to refcount containing ceph_osd struct
2177 */
2178static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2179{
2180 struct ceph_osd *osd = con->private;
2181 if (get_osd(osd))
2182 return con;
2183 return NULL;
2184}
2185
2186static void put_osd_con(struct ceph_connection *con)
2187{
2188 struct ceph_osd *osd = con->private;
2189 put_osd(osd);
2190}
2191
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002192/*
2193 * authentication
2194 */
Alex Eldera3530df2012-05-16 15:16:39 -05002195/*
2196 * Note: returned pointer is the address of a structure that's
2197 * managed separately. Caller must *not* attempt to free it.
2198 */
2199static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002200 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002201{
2202 struct ceph_osd *o = con->private;
2203 struct ceph_osd_client *osdc = o->o_osdc;
2204 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002205 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002206
Alex Elder74f18692012-05-16 15:16:39 -05002207 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002208 if (ac->ops && ac->ops->destroy_authorizer)
2209 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002210 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002211 }
Alex Eldera2556512012-05-16 15:16:39 -05002212 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002213 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2214 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002215 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002216 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002217 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002218 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002219
Alex Eldera3530df2012-05-16 15:16:39 -05002220 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002221}
2222
2223
2224static int verify_authorizer_reply(struct ceph_connection *con, int len)
2225{
2226 struct ceph_osd *o = con->private;
2227 struct ceph_osd_client *osdc = o->o_osdc;
2228 struct ceph_auth_client *ac = osdc->client->monc.auth;
2229
Alex Eldera2556512012-05-16 15:16:39 -05002230 /*
2231 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2232 * XXX which do we do: succeed or fail?
2233 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002234 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002235}
2236
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002237static int invalidate_authorizer(struct ceph_connection *con)
2238{
2239 struct ceph_osd *o = con->private;
2240 struct ceph_osd_client *osdc = o->o_osdc;
2241 struct ceph_auth_client *ac = osdc->client->monc.auth;
2242
Alex Eldera2556512012-05-16 15:16:39 -05002243 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002244 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2245
2246 return ceph_monc_validate_auth(&osdc->client->monc);
2247}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002248
Tobias Klauser9e327892010-05-20 10:40:19 +02002249static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002250 .get = get_osd_con,
2251 .put = put_osd_con,
2252 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002253 .get_authorizer = get_authorizer,
2254 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002255 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002256 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002257 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002258};