blob: ac186b7c998685ce1ff77e91e95c95740c7e7343 [file] [log] [blame]
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001#include <linux/ceph/ceph_debug.h>
Sage Weilf24e9982009-10-06 11:31:10 -07002
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07003#include <linux/module.h>
Sage Weilf24e9982009-10-06 11:31:10 -07004#include <linux/err.h>
5#include <linux/highmem.h>
6#include <linux/mm.h>
7#include <linux/pagemap.h>
8#include <linux/slab.h>
9#include <linux/uaccess.h>
Yehuda Sadeh68b44762010-04-06 15:01:27 -070010#ifdef CONFIG_BLOCK
11#include <linux/bio.h>
12#endif
Sage Weilf24e9982009-10-06 11:31:10 -070013
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070014#include <linux/ceph/libceph.h>
15#include <linux/ceph/osd_client.h>
16#include <linux/ceph/messenger.h>
17#include <linux/ceph/decode.h>
18#include <linux/ceph/auth.h>
19#include <linux/ceph/pagelist.h>
Sage Weilf24e9982009-10-06 11:31:10 -070020
Sage Weilc16e7862010-03-01 13:02:00 -080021#define OSD_OP_FRONT_LEN 4096
22#define OSD_OPREPLY_FRONT_LEN 512
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -080023
Tobias Klauser9e327892010-05-20 10:40:19 +020024static const struct ceph_connection_operations osd_con_ops;
Sage Weilf24e9982009-10-06 11:31:10 -070025
Sage Weil6f6c7002011-01-17 20:34:08 -080026static void send_queued(struct ceph_osd_client *osdc);
27static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -070028static void __register_request(struct ceph_osd_client *osdc,
29 struct ceph_osd_request *req);
30static void __unregister_linger_request(struct ceph_osd_client *osdc,
31 struct ceph_osd_request *req);
Sage Weil56e925b2012-01-03 12:34:34 -080032static void __send_request(struct ceph_osd_client *osdc,
33 struct ceph_osd_request *req);
Sage Weilf24e9982009-10-06 11:31:10 -070034
Yehuda Sadeh68b44762010-04-06 15:01:27 -070035static int op_has_extent(int op)
36{
37 return (op == CEPH_OSD_OP_READ ||
38 op == CEPH_OSD_OP_WRITE);
39}
40
Alex Eldere75b45c2012-11-13 21:11:14 -060041int ceph_calc_raw_layout(struct ceph_file_layout *layout,
Yehuda Sadeh68b44762010-04-06 15:01:27 -070042 u64 off, u64 *plen, u64 *bno,
43 struct ceph_osd_request *req,
44 struct ceph_osd_req_op *op)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070045{
Yehuda Sadeh68b44762010-04-06 15:01:27 -070046 u64 orig_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070047 u64 objoff, objlen; /* extent in object */
Sage Weild63b77f2012-09-24 20:59:48 -070048 int r;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070049
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070050 /* object extent? */
Alex Eldere8afad62012-11-14 09:38:19 -060051 r = ceph_calc_file_object_mapping(layout, off, orig_len, bno,
Sage Weild63b77f2012-09-24 20:59:48 -070052 &objoff, &objlen);
53 if (r < 0)
54 return r;
Alex Eldere8afad62012-11-14 09:38:19 -060055 if (objlen < orig_len) {
56 *plen = objlen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070057 dout(" skipping last %llu, final file extent %llu~%llu\n",
Yehuda Sadeh68b44762010-04-06 15:01:27 -070058 orig_len - *plen, off, *plen);
Alex Eldere8afad62012-11-14 09:38:19 -060059 }
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070060
Yehuda Sadeh68b44762010-04-06 15:01:27 -070061 if (op_has_extent(op->op)) {
Yan, Zhenga41bad12012-11-30 13:49:51 +080062 u32 osize = le32_to_cpu(layout->fl_object_size);
Yehuda Sadeh68b44762010-04-06 15:01:27 -070063 op->extent.offset = objoff;
64 op->extent.length = objlen;
Yan, Zhenga41bad12012-11-30 13:49:51 +080065 if (op->extent.truncate_size <= off - objoff) {
66 op->extent.truncate_size = 0;
67 } else {
68 op->extent.truncate_size -= off - objoff;
69 if (op->extent.truncate_size > osize)
70 op->extent.truncate_size = osize;
71 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -070072 }
73 req->r_num_pages = calc_pages_for(off, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -080074 req->r_page_alignment = off & ~PAGE_MASK;
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070075 if (op->op == CEPH_OSD_OP_WRITE)
76 op->payload_len = *plen;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070077
78 dout("calc_layout bno=%llx %llu~%llu (%d pages)\n",
79 *bno, objoff, objlen, req->r_num_pages);
Sage Weild63b77f2012-09-24 20:59:48 -070080 return 0;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070081}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -070082EXPORT_SYMBOL(ceph_calc_raw_layout);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -070083
Sage Weilf24e9982009-10-06 11:31:10 -070084/*
85 * Implement client access to distributed object storage cluster.
86 *
87 * All data objects are stored within a cluster/cloud of OSDs, or
88 * "object storage devices." (Note that Ceph OSDs have _nothing_ to
89 * do with the T10 OSD extensions to SCSI.) Ceph OSDs are simply
90 * remote daemons serving up and coordinating consistent and safe
91 * access to storage.
92 *
93 * Cluster membership and the mapping of data objects onto storage devices
94 * are described by the osd map.
95 *
96 * We keep track of pending OSD requests (read, write), resubmit
97 * requests to different OSDs when the cluster topology/data layout
98 * change, or retry the affected requests when the communications
99 * channel with an OSD is reset.
100 */
101
102/*
103 * calculate the mapping of a file extent onto an object, and fill out the
104 * request accordingly. shorten extent as necessary if it crosses an
105 * object boundary.
106 *
107 * fill osd op in request message.
108 */
Alex Eldere75b45c2012-11-13 21:11:14 -0600109static int calc_layout(struct ceph_vino vino,
Sage Weild63b77f2012-09-24 20:59:48 -0700110 struct ceph_file_layout *layout,
111 u64 off, u64 *plen,
112 struct ceph_osd_request *req,
113 struct ceph_osd_req_op *op)
Sage Weilf24e9982009-10-06 11:31:10 -0700114{
Sage Weilf24e9982009-10-06 11:31:10 -0700115 u64 bno;
Sage Weild63b77f2012-09-24 20:59:48 -0700116 int r;
Sage Weilf24e9982009-10-06 11:31:10 -0700117
Alex Eldere75b45c2012-11-13 21:11:14 -0600118 r = ceph_calc_raw_layout(layout, off, plen, &bno, req, op);
Sage Weild63b77f2012-09-24 20:59:48 -0700119 if (r < 0)
120 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700121
Sage Weil2dab0362011-05-12 14:29:51 -0700122 snprintf(req->r_oid, sizeof(req->r_oid), "%llx.%08llx", vino.ino, bno);
Sage Weilf24e9982009-10-06 11:31:10 -0700123 req->r_oid_len = strlen(req->r_oid);
Sage Weild63b77f2012-09-24 20:59:48 -0700124
125 return r;
Sage Weilf24e9982009-10-06 11:31:10 -0700126}
127
Sage Weilf24e9982009-10-06 11:31:10 -0700128/*
129 * requests
130 */
Sage Weil415e49a2009-12-07 13:37:03 -0800131void ceph_osdc_release_request(struct kref *kref)
Sage Weilf24e9982009-10-06 11:31:10 -0700132{
Sage Weil415e49a2009-12-07 13:37:03 -0800133 struct ceph_osd_request *req = container_of(kref,
134 struct ceph_osd_request,
135 r_kref);
136
137 if (req->r_request)
138 ceph_msg_put(req->r_request);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -0800139 if (req->r_con_filling_msg) {
Alex Elder9cbb1d72013-01-31 16:02:00 -0600140 dout("%s revoking msg %p from con %p\n", __func__,
141 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -0500142 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -0700143 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Alex Elder9cbb1d72013-01-31 16:02:00 -0600144 req->r_con_filling_msg = NULL;
Sage Weil350b1c32009-12-22 10:45:45 -0800145 }
Alex Elderab8cb342012-06-04 14:43:32 -0500146 if (req->r_reply)
147 ceph_msg_put(req->r_reply);
Sage Weil415e49a2009-12-07 13:37:03 -0800148 if (req->r_own_pages)
149 ceph_release_page_vector(req->r_pages,
150 req->r_num_pages);
151 ceph_put_snap_context(req->r_snapc);
Alex Elderc8858372012-11-13 21:11:15 -0600152 ceph_pagelist_release(&req->r_trail);
Sage Weil415e49a2009-12-07 13:37:03 -0800153 if (req->r_mempool)
154 mempool_free(req, req->r_osdc->req_mempool);
155 else
156 kfree(req);
Sage Weilf24e9982009-10-06 11:31:10 -0700157}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700158EXPORT_SYMBOL(ceph_osdc_release_request);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700159
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700160struct ceph_osd_request *ceph_osdc_alloc_request(struct ceph_osd_client *osdc,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700161 struct ceph_snap_context *snapc,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600162 unsigned int num_op,
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700163 bool use_mempool,
Alex Elder54a54002012-11-13 21:11:15 -0600164 gfp_t gfp_flags)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700165{
166 struct ceph_osd_request *req;
167 struct ceph_msg *msg;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700168 size_t msg_size = sizeof(struct ceph_osd_request_head);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700169
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700170 msg_size += num_op*sizeof(struct ceph_osd_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700171
172 if (use_mempool) {
173 req = mempool_alloc(osdc->req_mempool, gfp_flags);
174 memset(req, 0, sizeof(*req));
175 } else {
176 req = kzalloc(sizeof(*req), gfp_flags);
177 }
178 if (req == NULL)
179 return NULL;
180
181 req->r_osdc = osdc;
182 req->r_mempool = use_mempool;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700183
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700184 kref_init(&req->r_kref);
185 init_completion(&req->r_completion);
186 init_completion(&req->r_safe_completion);
Alex Eldera978fa22012-12-17 12:23:48 -0600187 RB_CLEAR_NODE(&req->r_node);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700188 INIT_LIST_HEAD(&req->r_unsafe_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700189 INIT_LIST_HEAD(&req->r_linger_item);
190 INIT_LIST_HEAD(&req->r_linger_osd);
Sage Weil935b6392011-09-16 11:13:17 -0700191 INIT_LIST_HEAD(&req->r_req_lru_item);
Sage Weilcd430452012-07-09 14:31:41 -0700192 INIT_LIST_HEAD(&req->r_osd_item);
193
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700194 /* create reply message */
195 if (use_mempool)
196 msg = ceph_msgpool_get(&osdc->msgpool_op_reply, 0);
197 else
198 msg = ceph_msg_new(CEPH_MSG_OSD_OPREPLY,
Sage Weilb61c2762011-08-09 15:03:46 -0700199 OSD_OPREPLY_FRONT_LEN, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700200 if (!msg) {
201 ceph_osdc_put_request(req);
202 return NULL;
203 }
204 req->r_reply = msg;
205
Alex Elderc8858372012-11-13 21:11:15 -0600206 ceph_pagelist_init(&req->r_trail);
Sage Weild50b4092012-07-09 14:22:34 -0700207
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700208 /* create request message; allow space for oid */
Stratos Psomadakis224736d2011-11-10 15:45:37 +0200209 msg_size += MAX_OBJ_NAME_SIZE;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700210 if (snapc)
211 msg_size += sizeof(u64) * snapc->num_snaps;
212 if (use_mempool)
213 msg = ceph_msgpool_get(&osdc->msgpool_op, 0);
214 else
Sage Weilb61c2762011-08-09 15:03:46 -0700215 msg = ceph_msg_new(CEPH_MSG_OSD_OP, msg_size, gfp_flags, true);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700216 if (!msg) {
217 ceph_osdc_put_request(req);
218 return NULL;
219 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700220
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700221 memset(msg->front.iov_base, 0, msg->front.iov_len);
222
223 req->r_request = msg;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700224
225 return req;
226}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700227EXPORT_SYMBOL(ceph_osdc_alloc_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700228
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700229static void osd_req_encode_op(struct ceph_osd_request *req,
230 struct ceph_osd_op *dst,
231 struct ceph_osd_req_op *src)
232{
233 dst->op = cpu_to_le16(src->op);
234
Alex Elder065a68f2012-04-20 15:49:43 -0500235 switch (src->op) {
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700236 case CEPH_OSD_OP_READ:
237 case CEPH_OSD_OP_WRITE:
238 dst->extent.offset =
239 cpu_to_le64(src->extent.offset);
240 dst->extent.length =
241 cpu_to_le64(src->extent.length);
242 dst->extent.truncate_size =
243 cpu_to_le64(src->extent.truncate_size);
244 dst->extent.truncate_seq =
245 cpu_to_le32(src->extent.truncate_seq);
246 break;
247
248 case CEPH_OSD_OP_GETXATTR:
249 case CEPH_OSD_OP_SETXATTR:
250 case CEPH_OSD_OP_CMPXATTR:
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700251 dst->xattr.name_len = cpu_to_le32(src->xattr.name_len);
252 dst->xattr.value_len = cpu_to_le32(src->xattr.value_len);
253 dst->xattr.cmp_op = src->xattr.cmp_op;
254 dst->xattr.cmp_mode = src->xattr.cmp_mode;
Alex Elderc8858372012-11-13 21:11:15 -0600255 ceph_pagelist_append(&req->r_trail, src->xattr.name,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700256 src->xattr.name_len);
Alex Elderc8858372012-11-13 21:11:15 -0600257 ceph_pagelist_append(&req->r_trail, src->xattr.val,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700258 src->xattr.value_len);
259 break;
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700260 case CEPH_OSD_OP_CALL:
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700261 dst->cls.class_len = src->cls.class_len;
262 dst->cls.method_len = src->cls.method_len;
263 dst->cls.indata_len = cpu_to_le32(src->cls.indata_len);
264
Alex Elderc8858372012-11-13 21:11:15 -0600265 ceph_pagelist_append(&req->r_trail, src->cls.class_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700266 src->cls.class_len);
Alex Elderc8858372012-11-13 21:11:15 -0600267 ceph_pagelist_append(&req->r_trail, src->cls.method_name,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700268 src->cls.method_len);
Alex Elderc8858372012-11-13 21:11:15 -0600269 ceph_pagelist_append(&req->r_trail, src->cls.indata,
Yehuda Sadehae1533b2010-05-18 16:38:08 -0700270 src->cls.indata_len);
271 break;
272 case CEPH_OSD_OP_ROLLBACK:
273 dst->snap.snapid = cpu_to_le64(src->snap.snapid);
274 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700275 case CEPH_OSD_OP_STARTSYNC:
276 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700277 case CEPH_OSD_OP_NOTIFY:
278 {
279 __le32 prot_ver = cpu_to_le32(src->watch.prot_ver);
280 __le32 timeout = cpu_to_le32(src->watch.timeout);
281
Alex Elderc8858372012-11-13 21:11:15 -0600282 ceph_pagelist_append(&req->r_trail,
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700283 &prot_ver, sizeof(prot_ver));
Alex Elderc8858372012-11-13 21:11:15 -0600284 ceph_pagelist_append(&req->r_trail,
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700285 &timeout, sizeof(timeout));
286 }
287 case CEPH_OSD_OP_NOTIFY_ACK:
288 case CEPH_OSD_OP_WATCH:
289 dst->watch.cookie = cpu_to_le64(src->watch.cookie);
290 dst->watch.ver = cpu_to_le64(src->watch.ver);
291 dst->watch.flag = src->watch.flag;
292 break;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700293 default:
294 pr_err("unrecognized osd opcode %d\n", dst->op);
295 WARN_ON(1);
296 break;
297 }
298 dst->payload_len = cpu_to_le32(src->payload_len);
299}
300
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700301/*
302 * build new request AND message
303 *
304 */
305void ceph_osdc_build_request(struct ceph_osd_request *req,
Alex Elderae7ca4a32012-11-13 21:11:15 -0600306 u64 off, u64 len, unsigned int num_op,
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700307 struct ceph_osd_req_op *src_ops,
Alex Elder4d6b2502012-11-13 21:11:15 -0600308 struct ceph_snap_context *snapc, u64 snap_id,
Alex Elderaf77f262012-11-09 08:43:15 -0600309 struct timespec *mtime)
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700310{
311 struct ceph_msg *msg = req->r_request;
312 struct ceph_osd_request_head *head;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700313 struct ceph_osd_req_op *src_op;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700314 struct ceph_osd_op *op;
315 void *p;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700316 size_t msg_size = sizeof(*head) + num_op*sizeof(*op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700317 int flags = req->r_flags;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700318 u64 data_len = 0;
319 int i;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700320
Alex Elderd178a9e2012-11-13 21:11:15 -0600321 WARN_ON((flags & (CEPH_OSD_FLAG_READ|CEPH_OSD_FLAG_WRITE)) == 0);
322
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700323 head = msg->front.iov_base;
Alex Elder4d6b2502012-11-13 21:11:15 -0600324 head->snapid = cpu_to_le64(snap_id);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700325 op = (void *)(head + 1);
326 p = (void *)(op + num_op);
327
328 req->r_snapc = ceph_get_snap_context(snapc);
329
330 head->client_inc = cpu_to_le32(1); /* always, for now. */
331 head->flags = cpu_to_le32(flags);
332 if (flags & CEPH_OSD_FLAG_WRITE)
333 ceph_encode_timespec(&head->mtime, mtime);
Alex Elderae7ca4a32012-11-13 21:11:15 -0600334 BUG_ON(num_op > (unsigned int) ((u16) -1));
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700335 head->num_ops = cpu_to_le16(num_op);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700336
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700337 /* fill in oid */
Alex Elderaf77f262012-11-09 08:43:15 -0600338 head->object_len = cpu_to_le32(req->r_oid_len);
339 memcpy(p, req->r_oid, req->r_oid_len);
340 p += req->r_oid_len;
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700341
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700342 src_op = src_ops;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600343 while (num_op--)
344 osd_req_encode_op(req, op++, src_op++);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700345
Alex Elderc8858372012-11-13 21:11:15 -0600346 data_len += req->r_trail.length;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700347
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700348 if (snapc) {
349 head->snap_seq = cpu_to_le64(snapc->seq);
350 head->num_snaps = cpu_to_le32(snapc->num_snaps);
351 for (i = 0; i < snapc->num_snaps; i++) {
352 put_unaligned_le64(snapc->snaps[i], p);
353 p += sizeof(u64);
354 }
355 }
356
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700357 if (flags & CEPH_OSD_FLAG_WRITE) {
358 req->r_request->hdr.data_off = cpu_to_le16(off);
Alex Elder0120be32012-11-14 09:38:19 -0600359 req->r_request->hdr.data_len = cpu_to_le32(len + data_len);
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700360 } else if (data_len) {
361 req->r_request->hdr.data_off = 0;
362 req->r_request->hdr.data_len = cpu_to_le32(data_len);
363 }
364
Sage Weilc5c6b192010-11-09 12:40:00 -0800365 req->r_request->page_alignment = req->r_page_alignment;
366
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700367 BUG_ON(p > msg->front.iov_base + msg->front.iov_len);
368 msg_size = p - msg->front.iov_base;
369 msg->front.iov_len = msg_size;
370 msg->hdr.front_len = cpu_to_le32(msg_size);
371 return;
372}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700373EXPORT_SYMBOL(ceph_osdc_build_request);
Yehuda Sadeh3499e8a2010-04-06 14:51:47 -0700374
Sage Weilf24e9982009-10-06 11:31:10 -0700375/*
376 * build new request AND message, calculate layout, and adjust file
377 * extent as needed.
378 *
379 * if the file was recently truncated, we include information about its
380 * old and new size so that the object can be updated appropriately. (we
381 * avoid synchronously deleting truncated objects because it's slow.)
382 *
383 * if @do_sync, include a 'startsync' command so that the osd will flush
384 * data quickly.
385 */
386struct ceph_osd_request *ceph_osdc_new_request(struct ceph_osd_client *osdc,
387 struct ceph_file_layout *layout,
388 struct ceph_vino vino,
389 u64 off, u64 *plen,
390 int opcode, int flags,
391 struct ceph_snap_context *snapc,
392 int do_sync,
393 u32 truncate_seq,
394 u64 truncate_size,
395 struct timespec *mtime,
Sage Weilb7495fc2010-11-09 12:43:12 -0800396 bool use_mempool, int num_reply,
397 int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -0700398{
Alex Elderae7ca4a32012-11-13 21:11:15 -0600399 struct ceph_osd_req_op ops[2];
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700400 struct ceph_osd_request *req;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600401 unsigned int num_op = 1;
Sage Weil68162822012-09-24 21:01:02 -0700402 int r;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700403
Alex Elderae7ca4a32012-11-13 21:11:15 -0600404 memset(&ops, 0, sizeof ops);
405
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700406 ops[0].op = opcode;
407 ops[0].extent.truncate_seq = truncate_seq;
408 ops[0].extent.truncate_size = truncate_size;
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700409
410 if (do_sync) {
411 ops[1].op = CEPH_OSD_OP_STARTSYNC;
Alex Elderae7ca4a32012-11-13 21:11:15 -0600412 num_op++;
413 }
Yehuda Sadeh68b44762010-04-06 15:01:27 -0700414
Alex Elderae7ca4a32012-11-13 21:11:15 -0600415 req = ceph_osdc_alloc_request(osdc, snapc, num_op, use_mempool,
416 GFP_NOFS);
Sage Weil4ad12622011-05-03 09:23:36 -0700417 if (!req)
Sage Weil68162822012-09-24 21:01:02 -0700418 return ERR_PTR(-ENOMEM);
Alex Elderd178a9e2012-11-13 21:11:15 -0600419 req->r_flags = flags;
Sage Weilf24e9982009-10-06 11:31:10 -0700420
421 /* calculate max write size */
Alex Eldere75b45c2012-11-13 21:11:14 -0600422 r = calc_layout(vino, layout, off, plen, req, ops);
Sage Weil68162822012-09-24 21:01:02 -0700423 if (r < 0)
424 return ERR_PTR(r);
Sage Weilf24e9982009-10-06 11:31:10 -0700425 req->r_file_layout = *layout; /* keep a copy */
426
Sage Weil9bb0ce22011-06-13 16:20:18 -0700427 /* in case it differs from natural (file) alignment that
428 calc_layout filled in for us */
429 req->r_num_pages = calc_pages_for(page_align, *plen);
Sage Weilb7495fc2010-11-09 12:43:12 -0800430 req->r_page_alignment = page_align;
431
Alex Elderae7ca4a32012-11-13 21:11:15 -0600432 ceph_osdc_build_request(req, off, *plen, num_op, ops,
433 snapc, vino.snap, mtime);
Sage Weilf24e9982009-10-06 11:31:10 -0700434
Sage Weilf24e9982009-10-06 11:31:10 -0700435 return req;
436}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700437EXPORT_SYMBOL(ceph_osdc_new_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700438
439/*
440 * We keep osd requests in an rbtree, sorted by ->r_tid.
441 */
442static void __insert_request(struct ceph_osd_client *osdc,
443 struct ceph_osd_request *new)
444{
445 struct rb_node **p = &osdc->requests.rb_node;
446 struct rb_node *parent = NULL;
447 struct ceph_osd_request *req = NULL;
448
449 while (*p) {
450 parent = *p;
451 req = rb_entry(parent, struct ceph_osd_request, r_node);
452 if (new->r_tid < req->r_tid)
453 p = &(*p)->rb_left;
454 else if (new->r_tid > req->r_tid)
455 p = &(*p)->rb_right;
456 else
457 BUG();
458 }
459
460 rb_link_node(&new->r_node, parent, p);
461 rb_insert_color(&new->r_node, &osdc->requests);
462}
463
464static struct ceph_osd_request *__lookup_request(struct ceph_osd_client *osdc,
465 u64 tid)
466{
467 struct ceph_osd_request *req;
468 struct rb_node *n = osdc->requests.rb_node;
469
470 while (n) {
471 req = rb_entry(n, struct ceph_osd_request, r_node);
472 if (tid < req->r_tid)
473 n = n->rb_left;
474 else if (tid > req->r_tid)
475 n = n->rb_right;
476 else
477 return req;
478 }
479 return NULL;
480}
481
482static struct ceph_osd_request *
483__lookup_request_ge(struct ceph_osd_client *osdc,
484 u64 tid)
485{
486 struct ceph_osd_request *req;
487 struct rb_node *n = osdc->requests.rb_node;
488
489 while (n) {
490 req = rb_entry(n, struct ceph_osd_request, r_node);
491 if (tid < req->r_tid) {
492 if (!n->rb_left)
493 return req;
494 n = n->rb_left;
495 } else if (tid > req->r_tid) {
496 n = n->rb_right;
497 } else {
498 return req;
499 }
500 }
501 return NULL;
502}
503
Sage Weil6f6c7002011-01-17 20:34:08 -0800504/*
505 * Resubmit requests pending on the given osd.
506 */
507static void __kick_osd_requests(struct ceph_osd_client *osdc,
508 struct ceph_osd *osd)
509{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700510 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -0800511 int err;
512
513 dout("__kick_osd_requests osd%d\n", osd->o_osd);
514 err = __reset_osd(osdc, osd);
Alex Elder685a7552012-12-07 09:57:58 -0600515 if (err)
Sage Weil6f6c7002011-01-17 20:34:08 -0800516 return;
517
518 list_for_each_entry(req, &osd->o_requests, r_osd_item) {
519 list_move(&req->r_req_lru_item, &osdc->req_unsent);
520 dout("requeued %p tid %llu osd%d\n", req, req->r_tid,
521 osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700522 if (!req->r_linger)
523 req->r_flags |= CEPH_OSD_FLAG_RETRY;
524 }
525
526 list_for_each_entry_safe(req, nreq, &osd->o_linger_requests,
527 r_linger_osd) {
Sage Weil77f38e02011-04-06 09:09:16 -0700528 /*
529 * reregister request prior to unregistering linger so
530 * that r_osd is preserved.
531 */
532 BUG_ON(!list_empty(&req->r_req_lru_item));
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700533 __register_request(osdc, req);
Sage Weil77f38e02011-04-06 09:09:16 -0700534 list_add(&req->r_req_lru_item, &osdc->req_unsent);
535 list_add(&req->r_osd_item, &req->r_osd->o_requests);
536 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700537 dout("requeued lingering %p tid %llu osd%d\n", req, req->r_tid,
538 osd->o_osd);
Sage Weil6f6c7002011-01-17 20:34:08 -0800539 }
540}
541
Sage Weilf24e9982009-10-06 11:31:10 -0700542/*
Sage Weil81b024e2009-10-09 10:29:18 -0700543 * If the osd connection drops, we need to resubmit all requests.
Sage Weilf24e9982009-10-06 11:31:10 -0700544 */
545static void osd_reset(struct ceph_connection *con)
546{
547 struct ceph_osd *osd = con->private;
548 struct ceph_osd_client *osdc;
549
550 if (!osd)
551 return;
552 dout("osd_reset osd%d\n", osd->o_osd);
553 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -0700554 down_read(&osdc->map_sem);
Sage Weil83aff952012-11-28 12:28:24 -0800555 mutex_lock(&osdc->request_mutex);
556 __kick_osd_requests(osdc, osd);
557 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -0800558 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700559 up_read(&osdc->map_sem);
560}
561
562/*
563 * Track open sessions with osds.
564 */
Alex Eldere10006f2012-05-26 23:26:43 -0500565static struct ceph_osd *create_osd(struct ceph_osd_client *osdc, int onum)
Sage Weilf24e9982009-10-06 11:31:10 -0700566{
567 struct ceph_osd *osd;
568
569 osd = kzalloc(sizeof(*osd), GFP_NOFS);
570 if (!osd)
571 return NULL;
572
573 atomic_set(&osd->o_ref, 1);
574 osd->o_osdc = osdc;
Alex Eldere10006f2012-05-26 23:26:43 -0500575 osd->o_osd = onum;
Alex Elderf4077312012-12-06 07:22:04 -0600576 RB_CLEAR_NODE(&osd->o_node);
Sage Weilf24e9982009-10-06 11:31:10 -0700577 INIT_LIST_HEAD(&osd->o_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700578 INIT_LIST_HEAD(&osd->o_linger_requests);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800579 INIT_LIST_HEAD(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700580 osd->o_incarnation = 1;
581
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700582 ceph_con_init(&osd->o_con, osd, &osd_con_ops, &osdc->client->msgr);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800583
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800584 INIT_LIST_HEAD(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700585 return osd;
586}
587
588static struct ceph_osd *get_osd(struct ceph_osd *osd)
589{
590 if (atomic_inc_not_zero(&osd->o_ref)) {
591 dout("get_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref)-1,
592 atomic_read(&osd->o_ref));
593 return osd;
594 } else {
595 dout("get_osd %p FAIL\n", osd);
596 return NULL;
597 }
598}
599
600static void put_osd(struct ceph_osd *osd)
601{
602 dout("put_osd %p %d -> %d\n", osd, atomic_read(&osd->o_ref),
603 atomic_read(&osd->o_ref) - 1);
Alex Eldera2556512012-05-16 15:16:39 -0500604 if (atomic_dec_and_test(&osd->o_ref) && osd->o_auth.authorizer) {
Sage Weil79494d12010-05-27 14:15:49 -0700605 struct ceph_auth_client *ac = osd->o_osdc->client->monc.auth;
606
Alex Eldera2556512012-05-16 15:16:39 -0500607 if (ac->ops && ac->ops->destroy_authorizer)
Alex Elder6c4a1912012-05-16 15:16:38 -0500608 ac->ops->destroy_authorizer(ac, osd->o_auth.authorizer);
Sage Weilf24e9982009-10-06 11:31:10 -0700609 kfree(osd);
Sage Weil79494d12010-05-27 14:15:49 -0700610 }
Sage Weilf24e9982009-10-06 11:31:10 -0700611}
612
613/*
614 * remove an osd from our map
615 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800616static void __remove_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700617{
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800618 dout("__remove_osd %p\n", osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700619 BUG_ON(!list_empty(&osd->o_requests));
620 rb_erase(&osd->o_node, &osdc->osds);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800621 list_del_init(&osd->o_osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700622 ceph_con_close(&osd->o_con);
623 put_osd(osd);
624}
625
Sage Weilaca420b2011-08-31 14:45:53 -0700626static void remove_all_osds(struct ceph_osd_client *osdc)
627{
Jiaju Zhang048a9d22012-07-20 08:18:36 -0500628 dout("%s %p\n", __func__, osdc);
Sage Weilaca420b2011-08-31 14:45:53 -0700629 mutex_lock(&osdc->request_mutex);
630 while (!RB_EMPTY_ROOT(&osdc->osds)) {
631 struct ceph_osd *osd = rb_entry(rb_first(&osdc->osds),
632 struct ceph_osd, o_node);
633 __remove_osd(osdc, osd);
634 }
635 mutex_unlock(&osdc->request_mutex);
636}
637
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800638static void __move_osd_to_lru(struct ceph_osd_client *osdc,
639 struct ceph_osd *osd)
640{
641 dout("__move_osd_to_lru %p\n", osd);
642 BUG_ON(!list_empty(&osd->o_osd_lru));
643 list_add_tail(&osd->o_osd_lru, &osdc->osd_lru);
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700644 osd->lru_ttl = jiffies + osdc->client->options->osd_idle_ttl * HZ;
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800645}
646
647static void __remove_osd_from_lru(struct ceph_osd *osd)
648{
649 dout("__remove_osd_from_lru %p\n", osd);
650 if (!list_empty(&osd->o_osd_lru))
651 list_del_init(&osd->o_osd_lru);
652}
653
Sage Weilaca420b2011-08-31 14:45:53 -0700654static void remove_old_osds(struct ceph_osd_client *osdc)
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800655{
656 struct ceph_osd *osd, *nosd;
657
658 dout("__remove_old_osds %p\n", osdc);
659 mutex_lock(&osdc->request_mutex);
660 list_for_each_entry_safe(osd, nosd, &osdc->osd_lru, o_osd_lru) {
Sage Weilaca420b2011-08-31 14:45:53 -0700661 if (time_before(jiffies, osd->lru_ttl))
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800662 break;
663 __remove_osd(osdc, osd);
664 }
665 mutex_unlock(&osdc->request_mutex);
666}
667
Sage Weilf24e9982009-10-06 11:31:10 -0700668/*
669 * reset osd connect
670 */
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800671static int __reset_osd(struct ceph_osd_client *osdc, struct ceph_osd *osd)
Sage Weilf24e9982009-10-06 11:31:10 -0700672{
Alex Elderc3acb182012-12-07 09:57:58 -0600673 struct ceph_entity_addr *peer_addr;
Sage Weilf24e9982009-10-06 11:31:10 -0700674
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800675 dout("__reset_osd %p osd%d\n", osd, osd->o_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700676 if (list_empty(&osd->o_requests) &&
677 list_empty(&osd->o_linger_requests)) {
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800678 __remove_osd(osdc, osd);
Alex Elderc3acb182012-12-07 09:57:58 -0600679
680 return -ENODEV;
681 }
682
683 peer_addr = &osdc->osdmap->osd_addr[osd->o_osd];
684 if (!memcmp(peer_addr, &osd->o_con.peer_addr, sizeof (*peer_addr)) &&
685 !ceph_con_opened(&osd->o_con)) {
686 struct ceph_osd_request *req;
687
Sage Weil87b315a2010-03-22 14:51:18 -0700688 dout(" osd addr hasn't changed and connection never opened,"
689 " letting msgr retry");
690 /* touch each r_stamp for handle_timeout()'s benfit */
691 list_for_each_entry(req, &osd->o_requests, r_osd_item)
692 req->r_stamp = jiffies;
Alex Elderc3acb182012-12-07 09:57:58 -0600693
694 return -EAGAIN;
Sage Weilf24e9982009-10-06 11:31:10 -0700695 }
Alex Elderc3acb182012-12-07 09:57:58 -0600696
697 ceph_con_close(&osd->o_con);
698 ceph_con_open(&osd->o_con, CEPH_ENTITY_TYPE_OSD, osd->o_osd, peer_addr);
699 osd->o_incarnation++;
700
701 return 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700702}
703
704static void __insert_osd(struct ceph_osd_client *osdc, struct ceph_osd *new)
705{
706 struct rb_node **p = &osdc->osds.rb_node;
707 struct rb_node *parent = NULL;
708 struct ceph_osd *osd = NULL;
709
Sage Weilaca420b2011-08-31 14:45:53 -0700710 dout("__insert_osd %p osd%d\n", new, new->o_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700711 while (*p) {
712 parent = *p;
713 osd = rb_entry(parent, struct ceph_osd, o_node);
714 if (new->o_osd < osd->o_osd)
715 p = &(*p)->rb_left;
716 else if (new->o_osd > osd->o_osd)
717 p = &(*p)->rb_right;
718 else
719 BUG();
720 }
721
722 rb_link_node(&new->o_node, parent, p);
723 rb_insert_color(&new->o_node, &osdc->osds);
724}
725
726static struct ceph_osd *__lookup_osd(struct ceph_osd_client *osdc, int o)
727{
728 struct ceph_osd *osd;
729 struct rb_node *n = osdc->osds.rb_node;
730
731 while (n) {
732 osd = rb_entry(n, struct ceph_osd, o_node);
733 if (o < osd->o_osd)
734 n = n->rb_left;
735 else if (o > osd->o_osd)
736 n = n->rb_right;
737 else
738 return osd;
739 }
740 return NULL;
741}
742
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800743static void __schedule_osd_timeout(struct ceph_osd_client *osdc)
744{
745 schedule_delayed_work(&osdc->timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -0700746 osdc->client->options->osd_keepalive_timeout * HZ);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800747}
748
749static void __cancel_osd_timeout(struct ceph_osd_client *osdc)
750{
751 cancel_delayed_work(&osdc->timeout_work);
752}
Sage Weilf24e9982009-10-06 11:31:10 -0700753
754/*
755 * Register request, assign tid. If this is the first request, set up
756 * the timeout event.
757 */
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700758static void __register_request(struct ceph_osd_client *osdc,
759 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700760{
Sage Weilf24e9982009-10-06 11:31:10 -0700761 req->r_tid = ++osdc->last_tid;
Sage Weil6df058c2009-12-22 11:24:33 -0800762 req->r_request->hdr.tid = cpu_to_le64(req->r_tid);
Sage Weil77f38e02011-04-06 09:09:16 -0700763 dout("__register_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700764 __insert_request(osdc, req);
765 ceph_osdc_get_request(req);
766 osdc->num_requests++;
Sage Weilf24e9982009-10-06 11:31:10 -0700767 if (osdc->num_requests == 1) {
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800768 dout(" first request, scheduling timeout\n");
769 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700770 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700771}
772
773static void register_request(struct ceph_osd_client *osdc,
774 struct ceph_osd_request *req)
775{
776 mutex_lock(&osdc->request_mutex);
777 __register_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -0700778 mutex_unlock(&osdc->request_mutex);
779}
780
781/*
782 * called under osdc->request_mutex
783 */
784static void __unregister_request(struct ceph_osd_client *osdc,
785 struct ceph_osd_request *req)
786{
Sage Weil35f9f8a2012-05-16 15:16:38 -0500787 if (RB_EMPTY_NODE(&req->r_node)) {
788 dout("__unregister_request %p tid %lld not registered\n",
789 req, req->r_tid);
790 return;
791 }
792
Sage Weilf24e9982009-10-06 11:31:10 -0700793 dout("__unregister_request %p tid %lld\n", req, req->r_tid);
794 rb_erase(&req->r_node, &osdc->requests);
795 osdc->num_requests--;
796
Sage Weil0ba64782009-10-08 16:57:16 -0700797 if (req->r_osd) {
798 /* make sure the original request isn't in flight. */
Alex Elder6740a842012-06-01 14:56:43 -0500799 ceph_msg_revoke(req->r_request);
Sage Weil0ba64782009-10-08 16:57:16 -0700800
801 list_del_init(&req->r_osd_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700802 if (list_empty(&req->r_osd->o_requests) &&
803 list_empty(&req->r_osd->o_linger_requests)) {
804 dout("moving osd to %p lru\n", req->r_osd);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800805 __move_osd_to_lru(osdc, req->r_osd);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700806 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700807 if (list_empty(&req->r_linger_item))
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700808 req->r_osd = NULL;
Sage Weil0ba64782009-10-08 16:57:16 -0700809 }
Sage Weilf24e9982009-10-06 11:31:10 -0700810
Alex Elder7d5f2482012-11-29 08:37:03 -0600811 list_del_init(&req->r_req_lru_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700812 ceph_osdc_put_request(req);
813
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -0800814 if (osdc->num_requests == 0) {
815 dout(" no requests, canceling timeout\n");
816 __cancel_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -0700817 }
818}
819
820/*
821 * Cancel a previously queued request message
822 */
823static void __cancel_request(struct ceph_osd_request *req)
824{
Sage Weil6bc18872010-09-27 10:18:52 -0700825 if (req->r_sent && req->r_osd) {
Alex Elder6740a842012-06-01 14:56:43 -0500826 ceph_msg_revoke(req->r_request);
Sage Weilf24e9982009-10-06 11:31:10 -0700827 req->r_sent = 0;
828 }
829}
830
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700831static void __register_linger_request(struct ceph_osd_client *osdc,
832 struct ceph_osd_request *req)
833{
834 dout("__register_linger_request %p\n", req);
835 list_add_tail(&req->r_linger_item, &osdc->req_linger);
Sage Weil6194ea82012-07-30 16:19:28 -0700836 if (req->r_osd)
837 list_add_tail(&req->r_linger_osd,
838 &req->r_osd->o_linger_requests);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700839}
840
841static void __unregister_linger_request(struct ceph_osd_client *osdc,
842 struct ceph_osd_request *req)
843{
844 dout("__unregister_linger_request %p\n", req);
Alex Elder61c74032012-12-06 09:37:23 -0600845 list_del_init(&req->r_linger_item);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700846 if (req->r_osd) {
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700847 list_del_init(&req->r_linger_osd);
848
849 if (list_empty(&req->r_osd->o_requests) &&
850 list_empty(&req->r_osd->o_linger_requests)) {
851 dout("moving osd to %p lru\n", req->r_osd);
852 __move_osd_to_lru(osdc, req->r_osd);
853 }
Sage Weilfbdb9192011-03-29 12:11:06 -0700854 if (list_empty(&req->r_osd_item))
855 req->r_osd = NULL;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -0700856 }
857}
858
859void ceph_osdc_unregister_linger_request(struct ceph_osd_client *osdc,
860 struct ceph_osd_request *req)
861{
862 mutex_lock(&osdc->request_mutex);
863 if (req->r_linger) {
864 __unregister_linger_request(osdc, req);
865 ceph_osdc_put_request(req);
866 }
867 mutex_unlock(&osdc->request_mutex);
868}
869EXPORT_SYMBOL(ceph_osdc_unregister_linger_request);
870
871void ceph_osdc_set_request_linger(struct ceph_osd_client *osdc,
872 struct ceph_osd_request *req)
873{
874 if (!req->r_linger) {
875 dout("set_request_linger %p\n", req);
876 req->r_linger = 1;
877 /*
878 * caller is now responsible for calling
879 * unregister_linger_request
880 */
881 ceph_osdc_get_request(req);
882 }
883}
884EXPORT_SYMBOL(ceph_osdc_set_request_linger);
885
Sage Weilf24e9982009-10-06 11:31:10 -0700886/*
887 * Pick an osd (the first 'up' osd in the pg), allocate the osd struct
888 * (as needed), and set the request r_osd appropriately. If there is
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300889 * no up osd, set r_osd to NULL. Move the request to the appropriate list
Sage Weil6f6c7002011-01-17 20:34:08 -0800890 * (unsent, homeless) or leave on in-flight lru.
Sage Weilf24e9982009-10-06 11:31:10 -0700891 *
892 * Return 0 if unchanged, 1 if changed, or negative on error.
893 *
894 * Caller should hold map_sem for read and request_mutex.
895 */
Sage Weil6f6c7002011-01-17 20:34:08 -0800896static int __map_request(struct ceph_osd_client *osdc,
Sage Weil38d64532011-10-14 13:33:55 -0700897 struct ceph_osd_request *req, int force_resend)
Sage Weilf24e9982009-10-06 11:31:10 -0700898{
899 struct ceph_osd_request_head *reqhead = req->r_request->front.iov_base;
Sage Weil51042122009-11-04 11:39:12 -0800900 struct ceph_pg pgid;
Sage Weild85b7052010-05-10 10:24:48 -0700901 int acting[CEPH_PG_MAX_SIZE];
902 int o = -1, num = 0;
Sage Weilf24e9982009-10-06 11:31:10 -0700903 int err;
Sage Weilf24e9982009-10-06 11:31:10 -0700904
Sage Weil6f6c7002011-01-17 20:34:08 -0800905 dout("map_request %p tid %lld\n", req, req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -0700906 err = ceph_calc_object_layout(&reqhead->layout, req->r_oid,
907 &req->r_file_layout, osdc->osdmap);
Sage Weil6f6c7002011-01-17 20:34:08 -0800908 if (err) {
909 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilf24e9982009-10-06 11:31:10 -0700910 return err;
Sage Weil6f6c7002011-01-17 20:34:08 -0800911 }
Sage Weil51042122009-11-04 11:39:12 -0800912 pgid = reqhead->layout.ol_pgid;
Sage Weil7740a422010-01-08 15:58:25 -0800913 req->r_pgid = pgid;
914
Sage Weild85b7052010-05-10 10:24:48 -0700915 err = ceph_calc_pg_acting(osdc->osdmap, pgid, acting);
916 if (err > 0) {
917 o = acting[0];
918 num = err;
919 }
Sage Weilf24e9982009-10-06 11:31:10 -0700920
Sage Weil38d64532011-10-14 13:33:55 -0700921 if ((!force_resend &&
922 req->r_osd && req->r_osd->o_osd == o &&
Sage Weild85b7052010-05-10 10:24:48 -0700923 req->r_sent >= req->r_osd->o_incarnation &&
924 req->r_num_pg_osds == num &&
925 memcmp(req->r_pg_osds, acting, sizeof(acting[0])*num) == 0) ||
Sage Weilf24e9982009-10-06 11:31:10 -0700926 (req->r_osd == NULL && o == -1))
927 return 0; /* no change */
928
Sage Weil6f6c7002011-01-17 20:34:08 -0800929 dout("map_request tid %llu pgid %d.%x osd%d (was osd%d)\n",
Sage Weil51042122009-11-04 11:39:12 -0800930 req->r_tid, le32_to_cpu(pgid.pool), le16_to_cpu(pgid.ps), o,
Sage Weilf24e9982009-10-06 11:31:10 -0700931 req->r_osd ? req->r_osd->o_osd : -1);
932
Sage Weild85b7052010-05-10 10:24:48 -0700933 /* record full pg acting set */
934 memcpy(req->r_pg_osds, acting, sizeof(acting[0]) * num);
935 req->r_num_pg_osds = num;
936
Sage Weilf24e9982009-10-06 11:31:10 -0700937 if (req->r_osd) {
938 __cancel_request(req);
939 list_del_init(&req->r_osd_item);
Sage Weilf24e9982009-10-06 11:31:10 -0700940 req->r_osd = NULL;
941 }
942
943 req->r_osd = __lookup_osd(osdc, o);
944 if (!req->r_osd && o >= 0) {
Sage Weilc99eb1c2010-02-26 09:37:33 -0800945 err = -ENOMEM;
Alex Eldere10006f2012-05-26 23:26:43 -0500946 req->r_osd = create_osd(osdc, o);
Sage Weil6f6c7002011-01-17 20:34:08 -0800947 if (!req->r_osd) {
948 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Sage Weilc99eb1c2010-02-26 09:37:33 -0800949 goto out;
Sage Weil6f6c7002011-01-17 20:34:08 -0800950 }
Sage Weilf24e9982009-10-06 11:31:10 -0700951
Sage Weil6f6c7002011-01-17 20:34:08 -0800952 dout("map_request osd %p is osd%d\n", req->r_osd, o);
Sage Weilf24e9982009-10-06 11:31:10 -0700953 __insert_osd(osdc, req->r_osd);
954
Sage Weilb7a9e5d2012-06-27 12:24:08 -0700955 ceph_con_open(&req->r_osd->o_con,
956 CEPH_ENTITY_TYPE_OSD, o,
957 &osdc->osdmap->osd_addr[o]);
Sage Weilf24e9982009-10-06 11:31:10 -0700958 }
959
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800960 if (req->r_osd) {
961 __remove_osd_from_lru(req->r_osd);
Sage Weilf24e9982009-10-06 11:31:10 -0700962 list_add(&req->r_osd_item, &req->r_osd->o_requests);
Sage Weil6f6c7002011-01-17 20:34:08 -0800963 list_move(&req->r_req_lru_item, &osdc->req_unsent);
964 } else {
965 list_move(&req->r_req_lru_item, &osdc->req_notarget);
Yehuda Sadehf5a20412010-02-03 11:00:26 -0800966 }
Sage Weild85b7052010-05-10 10:24:48 -0700967 err = 1; /* osd or pg changed */
Sage Weilf24e9982009-10-06 11:31:10 -0700968
969out:
Sage Weilf24e9982009-10-06 11:31:10 -0700970 return err;
971}
972
973/*
974 * caller should hold map_sem (for read) and request_mutex
975 */
Sage Weil56e925b2012-01-03 12:34:34 -0800976static void __send_request(struct ceph_osd_client *osdc,
977 struct ceph_osd_request *req)
Sage Weilf24e9982009-10-06 11:31:10 -0700978{
979 struct ceph_osd_request_head *reqhead;
Sage Weilf24e9982009-10-06 11:31:10 -0700980
981 dout("send_request %p tid %llu to osd%d flags %d\n",
982 req, req->r_tid, req->r_osd->o_osd, req->r_flags);
983
984 reqhead = req->r_request->front.iov_base;
985 reqhead->osdmap_epoch = cpu_to_le32(osdc->osdmap->epoch);
986 reqhead->flags |= cpu_to_le32(req->r_flags); /* e.g., RETRY */
987 reqhead->reassert_version = req->r_reassert_version;
988
Sage Weil3dd72fc2010-03-22 14:42:30 -0700989 req->r_stamp = jiffies;
Henry C Chang07a27e22010-08-22 21:34:27 -0700990 list_move_tail(&req->r_req_lru_item, &osdc->req_lru);
Sage Weilf24e9982009-10-06 11:31:10 -0700991
992 ceph_msg_get(req->r_request); /* send consumes a ref */
993 ceph_con_send(&req->r_osd->o_con, req->r_request);
994 req->r_sent = req->r_osd->o_incarnation;
Sage Weilf24e9982009-10-06 11:31:10 -0700995}
996
997/*
Sage Weil6f6c7002011-01-17 20:34:08 -0800998 * Send any requests in the queue (req_unsent).
999 */
1000static void send_queued(struct ceph_osd_client *osdc)
1001{
1002 struct ceph_osd_request *req, *tmp;
1003
1004 dout("send_queued\n");
1005 mutex_lock(&osdc->request_mutex);
1006 list_for_each_entry_safe(req, tmp, &osdc->req_unsent, r_req_lru_item) {
1007 __send_request(osdc, req);
1008 }
1009 mutex_unlock(&osdc->request_mutex);
1010}
1011
1012/*
Sage Weilf24e9982009-10-06 11:31:10 -07001013 * Timeout callback, called every N seconds when 1 or more osd
1014 * requests has been active for more than N seconds. When this
1015 * happens, we ping all OSDs with requests who have timed out to
1016 * ensure any communications channel reset is detected. Reset the
1017 * request timeouts another N seconds in the future as we go.
1018 * Reschedule the timeout event another N seconds in future (unless
1019 * there are no open requests).
1020 */
1021static void handle_timeout(struct work_struct *work)
1022{
1023 struct ceph_osd_client *osdc =
1024 container_of(work, struct ceph_osd_client, timeout_work.work);
Sage Weil83aff952012-11-28 12:28:24 -08001025 struct ceph_osd_request *req;
Sage Weilf24e9982009-10-06 11:31:10 -07001026 struct ceph_osd *osd;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001027 unsigned long keepalive =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001028 osdc->client->options->osd_keepalive_timeout * HZ;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001029 struct list_head slow_osds;
Sage Weilf24e9982009-10-06 11:31:10 -07001030 dout("timeout\n");
1031 down_read(&osdc->map_sem);
1032
1033 ceph_monc_request_next_osdmap(&osdc->client->monc);
1034
1035 mutex_lock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001036
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001037 /*
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001038 * ping osds that are a bit slow. this ensures that if there
1039 * is a break in the TCP connection we will notice, and reopen
1040 * a connection with that osd (from the fault callback).
1041 */
1042 INIT_LIST_HEAD(&slow_osds);
1043 list_for_each_entry(req, &osdc->req_lru, r_req_lru_item) {
Sage Weil3dd72fc2010-03-22 14:42:30 -07001044 if (time_before(jiffies, req->r_stamp + keepalive))
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001045 break;
1046
1047 osd = req->r_osd;
1048 BUG_ON(!osd);
1049 dout(" tid %llu is slow, will send keepalive on osd%d\n",
Sage Weilf24e9982009-10-06 11:31:10 -07001050 req->r_tid, osd->o_osd);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001051 list_move_tail(&osd->o_keepalive_item, &slow_osds);
1052 }
1053 while (!list_empty(&slow_osds)) {
1054 osd = list_entry(slow_osds.next, struct ceph_osd,
1055 o_keepalive_item);
1056 list_del_init(&osd->o_keepalive_item);
Sage Weilf24e9982009-10-06 11:31:10 -07001057 ceph_con_keepalive(&osd->o_con);
1058 }
1059
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001060 __schedule_osd_timeout(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001061 mutex_unlock(&osdc->request_mutex);
Sage Weil6f6c7002011-01-17 20:34:08 -08001062 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001063 up_read(&osdc->map_sem);
1064}
1065
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001066static void handle_osds_timeout(struct work_struct *work)
1067{
1068 struct ceph_osd_client *osdc =
1069 container_of(work, struct ceph_osd_client,
1070 osds_timeout_work.work);
1071 unsigned long delay =
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001072 osdc->client->options->osd_idle_ttl * HZ >> 2;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001073
1074 dout("osds timeout\n");
1075 down_read(&osdc->map_sem);
Sage Weilaca420b2011-08-31 14:45:53 -07001076 remove_old_osds(osdc);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001077 up_read(&osdc->map_sem);
1078
1079 schedule_delayed_work(&osdc->osds_timeout_work,
1080 round_jiffies_relative(delay));
1081}
1082
Sage Weil25845472011-06-03 09:37:09 -07001083static void complete_request(struct ceph_osd_request *req)
1084{
1085 if (req->r_safe_callback)
1086 req->r_safe_callback(req, NULL);
1087 complete_all(&req->r_safe_completion); /* fsync waiter */
1088}
1089
Sage Weilf24e9982009-10-06 11:31:10 -07001090/*
1091 * handle osd op reply. either call the callback if it is specified,
1092 * or do the completion to wake up the waiting thread.
1093 */
Sage Weil350b1c32009-12-22 10:45:45 -08001094static void handle_reply(struct ceph_osd_client *osdc, struct ceph_msg *msg,
1095 struct ceph_connection *con)
Sage Weilf24e9982009-10-06 11:31:10 -07001096{
1097 struct ceph_osd_reply_head *rhead = msg->front.iov_base;
1098 struct ceph_osd_request *req;
1099 u64 tid;
1100 int numops, object_len, flags;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001101 s32 result;
Sage Weilf24e9982009-10-06 11:31:10 -07001102
Sage Weil6df058c2009-12-22 11:24:33 -08001103 tid = le64_to_cpu(msg->hdr.tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001104 if (msg->front.iov_len < sizeof(*rhead))
1105 goto bad;
Sage Weilf24e9982009-10-06 11:31:10 -07001106 numops = le32_to_cpu(rhead->num_ops);
1107 object_len = le32_to_cpu(rhead->object_len);
Sage Weil0ceed5d2010-05-11 09:53:18 -07001108 result = le32_to_cpu(rhead->result);
Sage Weilf24e9982009-10-06 11:31:10 -07001109 if (msg->front.iov_len != sizeof(*rhead) + object_len +
1110 numops * sizeof(struct ceph_osd_op))
1111 goto bad;
Sage Weil0ceed5d2010-05-11 09:53:18 -07001112 dout("handle_reply %p tid %llu result %d\n", msg, tid, (int)result);
Sage Weilf24e9982009-10-06 11:31:10 -07001113 /* lookup */
1114 mutex_lock(&osdc->request_mutex);
1115 req = __lookup_request(osdc, tid);
1116 if (req == NULL) {
1117 dout("handle_reply tid %llu dne\n", tid);
1118 mutex_unlock(&osdc->request_mutex);
1119 return;
1120 }
1121 ceph_osdc_get_request(req);
1122 flags = le32_to_cpu(rhead->flags);
1123
Sage Weil350b1c32009-12-22 10:45:45 -08001124 /*
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001125 * if this connection filled our message, drop our reference now, to
Sage Weil350b1c32009-12-22 10:45:45 -08001126 * avoid a (safe but slower) revoke later.
1127 */
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001128 if (req->r_con_filling_msg == con && req->r_reply == msg) {
Sage Weilc16e7862010-03-01 13:02:00 -08001129 dout(" dropping con_filling_msg ref %p\n", con);
Yehuda Sadeh0d59ab82010-01-13 17:03:23 -08001130 req->r_con_filling_msg = NULL;
Sage Weil0d477662012-05-31 20:22:18 -07001131 con->ops->put(con);
Sage Weil350b1c32009-12-22 10:45:45 -08001132 }
1133
Sage Weilf24e9982009-10-06 11:31:10 -07001134 if (!req->r_got_reply) {
Eric Dumazet95c96172012-04-15 05:58:06 +00001135 unsigned int bytes;
Sage Weilf24e9982009-10-06 11:31:10 -07001136
1137 req->r_result = le32_to_cpu(rhead->result);
1138 bytes = le32_to_cpu(msg->hdr.data_len);
1139 dout("handle_reply result %d bytes %d\n", req->r_result,
1140 bytes);
1141 if (req->r_result == 0)
1142 req->r_result = bytes;
1143
1144 /* in case this is a write and we need to replay, */
1145 req->r_reassert_version = rhead->reassert_version;
1146
1147 req->r_got_reply = 1;
1148 } else if ((flags & CEPH_OSD_FLAG_ONDISK) == 0) {
1149 dout("handle_reply tid %llu dup ack\n", tid);
Sage Weil34b43a52009-12-01 12:23:54 -08001150 mutex_unlock(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001151 goto done;
1152 }
1153
1154 dout("handle_reply tid %llu flags %d\n", tid, flags);
1155
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001156 if (req->r_linger && (flags & CEPH_OSD_FLAG_ONDISK))
1157 __register_linger_request(osdc, req);
1158
Sage Weilf24e9982009-10-06 11:31:10 -07001159 /* either this is a read, or we got the safe response */
Sage Weil0ceed5d2010-05-11 09:53:18 -07001160 if (result < 0 ||
1161 (flags & CEPH_OSD_FLAG_ONDISK) ||
Sage Weilf24e9982009-10-06 11:31:10 -07001162 ((flags & CEPH_OSD_FLAG_WRITE) == 0))
1163 __unregister_request(osdc, req);
1164
1165 mutex_unlock(&osdc->request_mutex);
1166
1167 if (req->r_callback)
1168 req->r_callback(req, msg);
1169 else
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001170 complete_all(&req->r_completion);
Sage Weilf24e9982009-10-06 11:31:10 -07001171
Sage Weil25845472011-06-03 09:37:09 -07001172 if (flags & CEPH_OSD_FLAG_ONDISK)
1173 complete_request(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001174
1175done:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001176 dout("req=%p req->r_linger=%d\n", req, req->r_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001177 ceph_osdc_put_request(req);
1178 return;
1179
1180bad:
1181 pr_err("corrupt osd_op_reply got %d %d expected %d\n",
1182 (int)msg->front.iov_len, le32_to_cpu(msg->hdr.front_len),
1183 (int)sizeof(*rhead));
Sage Weil9ec7cab2009-12-14 15:13:47 -08001184 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001185}
1186
Sage Weil6f6c7002011-01-17 20:34:08 -08001187static void reset_changed_osds(struct ceph_osd_client *osdc)
Sage Weilf24e9982009-10-06 11:31:10 -07001188{
Sage Weilf24e9982009-10-06 11:31:10 -07001189 struct rb_node *p, *n;
Sage Weilf24e9982009-10-06 11:31:10 -07001190
Sage Weil6f6c7002011-01-17 20:34:08 -08001191 for (p = rb_first(&osdc->osds); p; p = n) {
1192 struct ceph_osd *osd = rb_entry(p, struct ceph_osd, o_node);
Sage Weilf24e9982009-10-06 11:31:10 -07001193
Sage Weil6f6c7002011-01-17 20:34:08 -08001194 n = rb_next(p);
1195 if (!ceph_osd_is_up(osdc->osdmap, osd->o_osd) ||
1196 memcmp(&osd->o_con.peer_addr,
1197 ceph_osd_addr(osdc->osdmap,
1198 osd->o_osd),
1199 sizeof(struct ceph_entity_addr)) != 0)
1200 __reset_osd(osdc, osd);
Sage Weilf24e9982009-10-06 11:31:10 -07001201 }
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001202}
1203
1204/*
Sage Weil6f6c7002011-01-17 20:34:08 -08001205 * Requeue requests whose mapping to an OSD has changed. If requests map to
1206 * no osd, request a new map.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001207 *
Alex Eldere6d50f62012-12-26 14:31:40 -06001208 * Caller should hold map_sem for read.
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001209 */
Sage Weil38d64532011-10-14 13:33:55 -07001210static void kick_requests(struct ceph_osd_client *osdc, int force_resend)
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001211{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001212 struct ceph_osd_request *req, *nreq;
Sage Weil6f6c7002011-01-17 20:34:08 -08001213 struct rb_node *p;
1214 int needmap = 0;
1215 int err;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001216
Sage Weil38d64532011-10-14 13:33:55 -07001217 dout("kick_requests %s\n", force_resend ? " (force resend)" : "");
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001218 mutex_lock(&osdc->request_mutex);
Sage Weil6194ea82012-07-30 16:19:28 -07001219 for (p = rb_first(&osdc->requests); p; ) {
Sage Weil6f6c7002011-01-17 20:34:08 -08001220 req = rb_entry(p, struct ceph_osd_request, r_node);
Sage Weil6194ea82012-07-30 16:19:28 -07001221 p = rb_next(p);
Alex Elderab60b162012-12-19 15:52:36 -06001222
1223 /*
1224 * For linger requests that have not yet been
1225 * registered, move them to the linger list; they'll
1226 * be sent to the osd in the loop below. Unregister
1227 * the request before re-registering it as a linger
1228 * request to ensure the __map_request() below
1229 * will decide it needs to be sent.
1230 */
1231 if (req->r_linger && list_empty(&req->r_linger_item)) {
1232 dout("%p tid %llu restart on osd%d\n",
1233 req, req->r_tid,
1234 req->r_osd ? req->r_osd->o_osd : -1);
1235 __unregister_request(osdc, req);
1236 __register_linger_request(osdc, req);
1237 continue;
1238 }
1239
Sage Weil38d64532011-10-14 13:33:55 -07001240 err = __map_request(osdc, req, force_resend);
Sage Weil6f6c7002011-01-17 20:34:08 -08001241 if (err < 0)
1242 continue; /* error */
1243 if (req->r_osd == NULL) {
1244 dout("%p tid %llu maps to no osd\n", req, req->r_tid);
1245 needmap++; /* request a newer map */
1246 } else if (err > 0) {
Sage Weil6194ea82012-07-30 16:19:28 -07001247 if (!req->r_linger) {
1248 dout("%p tid %llu requeued on osd%d\n", req,
1249 req->r_tid,
1250 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001251 req->r_flags |= CEPH_OSD_FLAG_RETRY;
Sage Weil6194ea82012-07-30 16:19:28 -07001252 }
1253 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001254 }
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001255
1256 list_for_each_entry_safe(req, nreq, &osdc->req_linger,
1257 r_linger_item) {
1258 dout("linger req=%p req->r_osd=%p\n", req, req->r_osd);
1259
Sage Weil38d64532011-10-14 13:33:55 -07001260 err = __map_request(osdc, req, force_resend);
Alex Elderab60b162012-12-19 15:52:36 -06001261 dout("__map_request returned %d\n", err);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001262 if (err == 0)
1263 continue; /* no change and no osd was specified */
1264 if (err < 0)
1265 continue; /* hrm! */
1266 if (req->r_osd == NULL) {
1267 dout("tid %llu maps to no valid osd\n", req->r_tid);
1268 needmap++; /* request a newer map */
1269 continue;
1270 }
1271
1272 dout("kicking lingering %p tid %llu osd%d\n", req, req->r_tid,
1273 req->r_osd ? req->r_osd->o_osd : -1);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001274 __register_request(osdc, req);
Alex Elderc89ce052012-12-06 07:22:04 -06001275 __unregister_linger_request(osdc, req);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001276 }
Sage Weilf24e9982009-10-06 11:31:10 -07001277 mutex_unlock(&osdc->request_mutex);
1278
1279 if (needmap) {
1280 dout("%d requests for down osds, need new map\n", needmap);
1281 ceph_monc_request_next_osdmap(&osdc->client->monc);
1282 }
Alex Eldere6d50f62012-12-26 14:31:40 -06001283 reset_changed_osds(osdc);
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001284}
Sage Weil6f6c7002011-01-17 20:34:08 -08001285
1286
Sage Weilf24e9982009-10-06 11:31:10 -07001287/*
1288 * Process updated osd map.
1289 *
1290 * The message contains any number of incremental and full maps, normally
1291 * indicating some sort of topology change in the cluster. Kick requests
1292 * off to different OSDs as needed.
1293 */
1294void ceph_osdc_handle_map(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1295{
1296 void *p, *end, *next;
1297 u32 nr_maps, maplen;
1298 u32 epoch;
1299 struct ceph_osdmap *newmap = NULL, *oldmap;
1300 int err;
1301 struct ceph_fsid fsid;
1302
1303 dout("handle_map have %u\n", osdc->osdmap ? osdc->osdmap->epoch : 0);
1304 p = msg->front.iov_base;
1305 end = p + msg->front.iov_len;
1306
1307 /* verify fsid */
1308 ceph_decode_need(&p, end, sizeof(fsid), bad);
1309 ceph_decode_copy(&p, &fsid, sizeof(fsid));
Sage Weil07433042009-11-18 16:50:41 -08001310 if (ceph_check_fsid(osdc->client, &fsid) < 0)
1311 return;
Sage Weilf24e9982009-10-06 11:31:10 -07001312
1313 down_write(&osdc->map_sem);
1314
1315 /* incremental maps */
1316 ceph_decode_32_safe(&p, end, nr_maps, bad);
1317 dout(" %d inc maps\n", nr_maps);
1318 while (nr_maps > 0) {
1319 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001320 epoch = ceph_decode_32(&p);
1321 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001322 ceph_decode_need(&p, end, maplen, bad);
1323 next = p + maplen;
1324 if (osdc->osdmap && osdc->osdmap->epoch+1 == epoch) {
1325 dout("applying incremental map %u len %d\n",
1326 epoch, maplen);
1327 newmap = osdmap_apply_incremental(&p, next,
1328 osdc->osdmap,
Alex Elder15d98822012-05-26 23:26:43 -05001329 &osdc->client->msgr);
Sage Weilf24e9982009-10-06 11:31:10 -07001330 if (IS_ERR(newmap)) {
1331 err = PTR_ERR(newmap);
1332 goto bad;
1333 }
Sage Weil30dc6382009-12-21 14:49:37 -08001334 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001335 if (newmap != osdc->osdmap) {
1336 ceph_osdmap_destroy(osdc->osdmap);
1337 osdc->osdmap = newmap;
1338 }
Sage Weil38d64532011-10-14 13:33:55 -07001339 kick_requests(osdc, 0);
Sage Weilf24e9982009-10-06 11:31:10 -07001340 } else {
1341 dout("ignoring incremental map %u len %d\n",
1342 epoch, maplen);
1343 }
1344 p = next;
1345 nr_maps--;
1346 }
1347 if (newmap)
1348 goto done;
1349
1350 /* full maps */
1351 ceph_decode_32_safe(&p, end, nr_maps, bad);
1352 dout(" %d full maps\n", nr_maps);
1353 while (nr_maps) {
1354 ceph_decode_need(&p, end, 2*sizeof(u32), bad);
Sage Weilc89136e2009-10-14 09:59:09 -07001355 epoch = ceph_decode_32(&p);
1356 maplen = ceph_decode_32(&p);
Sage Weilf24e9982009-10-06 11:31:10 -07001357 ceph_decode_need(&p, end, maplen, bad);
1358 if (nr_maps > 1) {
1359 dout("skipping non-latest full map %u len %d\n",
1360 epoch, maplen);
1361 } else if (osdc->osdmap && osdc->osdmap->epoch >= epoch) {
1362 dout("skipping full map %u len %d, "
1363 "older than our %u\n", epoch, maplen,
1364 osdc->osdmap->epoch);
1365 } else {
Sage Weil38d64532011-10-14 13:33:55 -07001366 int skipped_map = 0;
1367
Sage Weilf24e9982009-10-06 11:31:10 -07001368 dout("taking full map %u len %d\n", epoch, maplen);
1369 newmap = osdmap_decode(&p, p+maplen);
1370 if (IS_ERR(newmap)) {
1371 err = PTR_ERR(newmap);
1372 goto bad;
1373 }
Sage Weil30dc6382009-12-21 14:49:37 -08001374 BUG_ON(!newmap);
Sage Weilf24e9982009-10-06 11:31:10 -07001375 oldmap = osdc->osdmap;
1376 osdc->osdmap = newmap;
Sage Weil38d64532011-10-14 13:33:55 -07001377 if (oldmap) {
1378 if (oldmap->epoch + 1 < newmap->epoch)
1379 skipped_map = 1;
Sage Weilf24e9982009-10-06 11:31:10 -07001380 ceph_osdmap_destroy(oldmap);
Sage Weil38d64532011-10-14 13:33:55 -07001381 }
1382 kick_requests(osdc, skipped_map);
Sage Weilf24e9982009-10-06 11:31:10 -07001383 }
1384 p += maplen;
1385 nr_maps--;
1386 }
1387
1388done:
1389 downgrade_write(&osdc->map_sem);
1390 ceph_monc_got_osdmap(&osdc->client->monc, osdc->osdmap->epoch);
Sage Weilcd634fb2011-05-12 09:29:18 -07001391
1392 /*
1393 * subscribe to subsequent osdmap updates if full to ensure
1394 * we find out when we are no longer full and stop returning
1395 * ENOSPC.
1396 */
1397 if (ceph_osdmap_flag(osdc->osdmap, CEPH_OSDMAP_FULL))
1398 ceph_monc_request_next_osdmap(&osdc->client->monc);
1399
Sage Weil6f6c7002011-01-17 20:34:08 -08001400 send_queued(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001401 up_read(&osdc->map_sem);
Yehuda Sadeh03066f22010-07-27 13:11:08 -07001402 wake_up_all(&osdc->client->auth_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001403 return;
1404
1405bad:
1406 pr_err("osdc handle_map corrupt msg\n");
Sage Weil9ec7cab2009-12-14 15:13:47 -08001407 ceph_msg_dump(msg);
Sage Weilf24e9982009-10-06 11:31:10 -07001408 up_write(&osdc->map_sem);
1409 return;
1410}
1411
Sage Weilf24e9982009-10-06 11:31:10 -07001412/*
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001413 * watch/notify callback event infrastructure
1414 *
1415 * These callbacks are used both for watch and notify operations.
1416 */
1417static void __release_event(struct kref *kref)
1418{
1419 struct ceph_osd_event *event =
1420 container_of(kref, struct ceph_osd_event, kref);
1421
1422 dout("__release_event %p\n", event);
1423 kfree(event);
1424}
1425
1426static void get_event(struct ceph_osd_event *event)
1427{
1428 kref_get(&event->kref);
1429}
1430
1431void ceph_osdc_put_event(struct ceph_osd_event *event)
1432{
1433 kref_put(&event->kref, __release_event);
1434}
1435EXPORT_SYMBOL(ceph_osdc_put_event);
1436
1437static void __insert_event(struct ceph_osd_client *osdc,
1438 struct ceph_osd_event *new)
1439{
1440 struct rb_node **p = &osdc->event_tree.rb_node;
1441 struct rb_node *parent = NULL;
1442 struct ceph_osd_event *event = NULL;
1443
1444 while (*p) {
1445 parent = *p;
1446 event = rb_entry(parent, struct ceph_osd_event, node);
1447 if (new->cookie < event->cookie)
1448 p = &(*p)->rb_left;
1449 else if (new->cookie > event->cookie)
1450 p = &(*p)->rb_right;
1451 else
1452 BUG();
1453 }
1454
1455 rb_link_node(&new->node, parent, p);
1456 rb_insert_color(&new->node, &osdc->event_tree);
1457}
1458
1459static struct ceph_osd_event *__find_event(struct ceph_osd_client *osdc,
1460 u64 cookie)
1461{
1462 struct rb_node **p = &osdc->event_tree.rb_node;
1463 struct rb_node *parent = NULL;
1464 struct ceph_osd_event *event = NULL;
1465
1466 while (*p) {
1467 parent = *p;
1468 event = rb_entry(parent, struct ceph_osd_event, node);
1469 if (cookie < event->cookie)
1470 p = &(*p)->rb_left;
1471 else if (cookie > event->cookie)
1472 p = &(*p)->rb_right;
1473 else
1474 return event;
1475 }
1476 return NULL;
1477}
1478
1479static void __remove_event(struct ceph_osd_event *event)
1480{
1481 struct ceph_osd_client *osdc = event->osdc;
1482
1483 if (!RB_EMPTY_NODE(&event->node)) {
1484 dout("__remove_event removed %p\n", event);
1485 rb_erase(&event->node, &osdc->event_tree);
1486 ceph_osdc_put_event(event);
1487 } else {
1488 dout("__remove_event didn't remove %p\n", event);
1489 }
1490}
1491
1492int ceph_osdc_create_event(struct ceph_osd_client *osdc,
1493 void (*event_cb)(u64, u64, u8, void *),
1494 int one_shot, void *data,
1495 struct ceph_osd_event **pevent)
1496{
1497 struct ceph_osd_event *event;
1498
1499 event = kmalloc(sizeof(*event), GFP_NOIO);
1500 if (!event)
1501 return -ENOMEM;
1502
1503 dout("create_event %p\n", event);
1504 event->cb = event_cb;
1505 event->one_shot = one_shot;
1506 event->data = data;
1507 event->osdc = osdc;
1508 INIT_LIST_HEAD(&event->osd_node);
Alex Elder3ee52342012-12-17 12:23:48 -06001509 RB_CLEAR_NODE(&event->node);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001510 kref_init(&event->kref); /* one ref for us */
1511 kref_get(&event->kref); /* one ref for the caller */
1512 init_completion(&event->completion);
1513
1514 spin_lock(&osdc->event_lock);
1515 event->cookie = ++osdc->event_count;
1516 __insert_event(osdc, event);
1517 spin_unlock(&osdc->event_lock);
1518
1519 *pevent = event;
1520 return 0;
1521}
1522EXPORT_SYMBOL(ceph_osdc_create_event);
1523
1524void ceph_osdc_cancel_event(struct ceph_osd_event *event)
1525{
1526 struct ceph_osd_client *osdc = event->osdc;
1527
1528 dout("cancel_event %p\n", event);
1529 spin_lock(&osdc->event_lock);
1530 __remove_event(event);
1531 spin_unlock(&osdc->event_lock);
1532 ceph_osdc_put_event(event); /* caller's */
1533}
1534EXPORT_SYMBOL(ceph_osdc_cancel_event);
1535
1536
1537static void do_event_work(struct work_struct *work)
1538{
1539 struct ceph_osd_event_work *event_work =
1540 container_of(work, struct ceph_osd_event_work, work);
1541 struct ceph_osd_event *event = event_work->event;
1542 u64 ver = event_work->ver;
1543 u64 notify_id = event_work->notify_id;
1544 u8 opcode = event_work->opcode;
1545
1546 dout("do_event_work completing %p\n", event);
1547 event->cb(ver, notify_id, opcode, event->data);
1548 complete(&event->completion);
1549 dout("do_event_work completed %p\n", event);
1550 ceph_osdc_put_event(event);
1551 kfree(event_work);
1552}
1553
1554
1555/*
1556 * Process osd watch notifications
1557 */
1558void handle_watch_notify(struct ceph_osd_client *osdc, struct ceph_msg *msg)
1559{
1560 void *p, *end;
1561 u8 proto_ver;
1562 u64 cookie, ver, notify_id;
1563 u8 opcode;
1564 struct ceph_osd_event *event;
1565 struct ceph_osd_event_work *event_work;
1566
1567 p = msg->front.iov_base;
1568 end = p + msg->front.iov_len;
1569
1570 ceph_decode_8_safe(&p, end, proto_ver, bad);
1571 ceph_decode_8_safe(&p, end, opcode, bad);
1572 ceph_decode_64_safe(&p, end, cookie, bad);
1573 ceph_decode_64_safe(&p, end, ver, bad);
1574 ceph_decode_64_safe(&p, end, notify_id, bad);
1575
1576 spin_lock(&osdc->event_lock);
1577 event = __find_event(osdc, cookie);
1578 if (event) {
1579 get_event(event);
1580 if (event->one_shot)
1581 __remove_event(event);
1582 }
1583 spin_unlock(&osdc->event_lock);
1584 dout("handle_watch_notify cookie %lld ver %lld event %p\n",
1585 cookie, ver, event);
1586 if (event) {
1587 event_work = kmalloc(sizeof(*event_work), GFP_NOIO);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001588 if (!event_work) {
1589 dout("ERROR: could not allocate event_work\n");
1590 goto done_err;
1591 }
Mariusz Kozlowski6b0ae402011-03-26 19:29:34 +01001592 INIT_WORK(&event_work->work, do_event_work);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001593 event_work->event = event;
1594 event_work->ver = ver;
1595 event_work->notify_id = notify_id;
1596 event_work->opcode = opcode;
1597 if (!queue_work(osdc->notify_wq, &event_work->work)) {
1598 dout("WARNING: failed to queue notify event work\n");
1599 goto done_err;
1600 }
1601 }
1602
1603 return;
1604
1605done_err:
1606 complete(&event->completion);
1607 ceph_osdc_put_event(event);
1608 return;
1609
1610bad:
1611 pr_err("osdc handle_watch_notify corrupt msg\n");
1612 return;
1613}
1614
1615int ceph_osdc_wait_event(struct ceph_osd_event *event, unsigned long timeout)
1616{
1617 int err;
1618
1619 dout("wait_event %p\n", event);
1620 err = wait_for_completion_interruptible_timeout(&event->completion,
1621 timeout * HZ);
1622 ceph_osdc_put_event(event);
1623 if (err > 0)
1624 err = 0;
1625 dout("wait_event %p returns %d\n", event, err);
1626 return err;
1627}
1628EXPORT_SYMBOL(ceph_osdc_wait_event);
1629
1630/*
Sage Weilf24e9982009-10-06 11:31:10 -07001631 * Register request, send initial attempt.
1632 */
1633int ceph_osdc_start_request(struct ceph_osd_client *osdc,
1634 struct ceph_osd_request *req,
1635 bool nofail)
1636{
Sage Weilc1ea8822009-10-08 16:55:47 -07001637 int rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001638
1639 req->r_request->pages = req->r_pages;
1640 req->r_request->nr_pages = req->r_num_pages;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001641#ifdef CONFIG_BLOCK
1642 req->r_request->bio = req->r_bio;
1643#endif
Alex Elderc8858372012-11-13 21:11:15 -06001644 req->r_request->trail = &req->r_trail;
Sage Weilf24e9982009-10-06 11:31:10 -07001645
1646 register_request(osdc, req);
1647
1648 down_read(&osdc->map_sem);
1649 mutex_lock(&osdc->request_mutex);
Sage Weilc1ea8822009-10-08 16:55:47 -07001650 /*
1651 * a racing kick_requests() may have sent the message for us
1652 * while we dropped request_mutex above, so only send now if
1653 * the request still han't been touched yet.
1654 */
1655 if (req->r_sent == 0) {
Sage Weil38d64532011-10-14 13:33:55 -07001656 rc = __map_request(osdc, req, 0);
Sage Weil9d6fcb02011-05-12 15:48:16 -07001657 if (rc < 0) {
1658 if (nofail) {
1659 dout("osdc_start_request failed map, "
1660 " will retry %lld\n", req->r_tid);
1661 rc = 0;
1662 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001663 goto out_unlock;
Sage Weil9d6fcb02011-05-12 15:48:16 -07001664 }
Sage Weil6f6c7002011-01-17 20:34:08 -08001665 if (req->r_osd == NULL) {
1666 dout("send_request %p no up osds in pg\n", req);
1667 ceph_monc_request_next_osdmap(&osdc->client->monc);
1668 } else {
Sage Weil56e925b2012-01-03 12:34:34 -08001669 __send_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001670 }
Sage Weil56e925b2012-01-03 12:34:34 -08001671 rc = 0;
Sage Weilf24e9982009-10-06 11:31:10 -07001672 }
Dan Carpenter234af26f2011-03-29 06:25:59 +03001673
1674out_unlock:
Sage Weilf24e9982009-10-06 11:31:10 -07001675 mutex_unlock(&osdc->request_mutex);
1676 up_read(&osdc->map_sem);
1677 return rc;
1678}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001679EXPORT_SYMBOL(ceph_osdc_start_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001680
1681/*
1682 * wait for a request to complete
1683 */
1684int ceph_osdc_wait_request(struct ceph_osd_client *osdc,
1685 struct ceph_osd_request *req)
1686{
1687 int rc;
1688
1689 rc = wait_for_completion_interruptible(&req->r_completion);
1690 if (rc < 0) {
1691 mutex_lock(&osdc->request_mutex);
1692 __cancel_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001693 __unregister_request(osdc, req);
Sage Weilf24e9982009-10-06 11:31:10 -07001694 mutex_unlock(&osdc->request_mutex);
Sage Weil25845472011-06-03 09:37:09 -07001695 complete_request(req);
Sage Weil529cfcc2009-12-22 10:29:39 -08001696 dout("wait_request tid %llu canceled/timed out\n", req->r_tid);
Sage Weilf24e9982009-10-06 11:31:10 -07001697 return rc;
1698 }
1699
1700 dout("wait_request tid %llu result %d\n", req->r_tid, req->r_result);
1701 return req->r_result;
1702}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001703EXPORT_SYMBOL(ceph_osdc_wait_request);
Sage Weilf24e9982009-10-06 11:31:10 -07001704
1705/*
1706 * sync - wait for all in-flight requests to flush. avoid starvation.
1707 */
1708void ceph_osdc_sync(struct ceph_osd_client *osdc)
1709{
1710 struct ceph_osd_request *req;
1711 u64 last_tid, next_tid = 0;
1712
1713 mutex_lock(&osdc->request_mutex);
1714 last_tid = osdc->last_tid;
1715 while (1) {
1716 req = __lookup_request_ge(osdc, next_tid);
1717 if (!req)
1718 break;
1719 if (req->r_tid > last_tid)
1720 break;
1721
1722 next_tid = req->r_tid + 1;
1723 if ((req->r_flags & CEPH_OSD_FLAG_WRITE) == 0)
1724 continue;
1725
1726 ceph_osdc_get_request(req);
1727 mutex_unlock(&osdc->request_mutex);
1728 dout("sync waiting on tid %llu (last is %llu)\n",
1729 req->r_tid, last_tid);
1730 wait_for_completion(&req->r_safe_completion);
1731 mutex_lock(&osdc->request_mutex);
1732 ceph_osdc_put_request(req);
1733 }
1734 mutex_unlock(&osdc->request_mutex);
1735 dout("sync done (thru tid %llu)\n", last_tid);
1736}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001737EXPORT_SYMBOL(ceph_osdc_sync);
Sage Weilf24e9982009-10-06 11:31:10 -07001738
1739/*
1740 * init, shutdown
1741 */
1742int ceph_osdc_init(struct ceph_osd_client *osdc, struct ceph_client *client)
1743{
1744 int err;
1745
1746 dout("init\n");
1747 osdc->client = client;
1748 osdc->osdmap = NULL;
1749 init_rwsem(&osdc->map_sem);
1750 init_completion(&osdc->map_waiters);
1751 osdc->last_requested_map = 0;
1752 mutex_init(&osdc->request_mutex);
Sage Weilf24e9982009-10-06 11:31:10 -07001753 osdc->last_tid = 0;
1754 osdc->osds = RB_ROOT;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001755 INIT_LIST_HEAD(&osdc->osd_lru);
Sage Weilf24e9982009-10-06 11:31:10 -07001756 osdc->requests = RB_ROOT;
Yehuda Sadeh422d2cb2010-02-26 15:32:31 -08001757 INIT_LIST_HEAD(&osdc->req_lru);
Sage Weil6f6c7002011-01-17 20:34:08 -08001758 INIT_LIST_HEAD(&osdc->req_unsent);
1759 INIT_LIST_HEAD(&osdc->req_notarget);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001760 INIT_LIST_HEAD(&osdc->req_linger);
Sage Weilf24e9982009-10-06 11:31:10 -07001761 osdc->num_requests = 0;
1762 INIT_DELAYED_WORK(&osdc->timeout_work, handle_timeout);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001763 INIT_DELAYED_WORK(&osdc->osds_timeout_work, handle_osds_timeout);
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001764 spin_lock_init(&osdc->event_lock);
1765 osdc->event_tree = RB_ROOT;
1766 osdc->event_count = 0;
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001767
1768 schedule_delayed_work(&osdc->osds_timeout_work,
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001769 round_jiffies_relative(osdc->client->options->osd_idle_ttl * HZ));
Sage Weilf24e9982009-10-06 11:31:10 -07001770
Sage Weil5f44f142009-11-18 14:52:18 -08001771 err = -ENOMEM;
Sage Weilf24e9982009-10-06 11:31:10 -07001772 osdc->req_mempool = mempool_create_kmalloc_pool(10,
1773 sizeof(struct ceph_osd_request));
1774 if (!osdc->req_mempool)
Sage Weil5f44f142009-11-18 14:52:18 -08001775 goto out;
Sage Weilf24e9982009-10-06 11:31:10 -07001776
Sage Weild50b4092012-07-09 14:22:34 -07001777 err = ceph_msgpool_init(&osdc->msgpool_op, CEPH_MSG_OSD_OP,
1778 OSD_OP_FRONT_LEN, 10, true,
Sage Weil4f482802010-04-24 09:56:35 -07001779 "osd_op");
Sage Weilf24e9982009-10-06 11:31:10 -07001780 if (err < 0)
Sage Weil5f44f142009-11-18 14:52:18 -08001781 goto out_mempool;
Sage Weild50b4092012-07-09 14:22:34 -07001782 err = ceph_msgpool_init(&osdc->msgpool_op_reply, CEPH_MSG_OSD_OPREPLY,
Sage Weil4f482802010-04-24 09:56:35 -07001783 OSD_OPREPLY_FRONT_LEN, 10, true,
1784 "osd_op_reply");
Sage Weilc16e7862010-03-01 13:02:00 -08001785 if (err < 0)
1786 goto out_msgpool;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001787
1788 osdc->notify_wq = create_singlethread_workqueue("ceph-watch-notify");
1789 if (IS_ERR(osdc->notify_wq)) {
1790 err = PTR_ERR(osdc->notify_wq);
1791 osdc->notify_wq = NULL;
1792 goto out_msgpool;
1793 }
Sage Weilf24e9982009-10-06 11:31:10 -07001794 return 0;
Sage Weil5f44f142009-11-18 14:52:18 -08001795
Sage Weilc16e7862010-03-01 13:02:00 -08001796out_msgpool:
1797 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weil5f44f142009-11-18 14:52:18 -08001798out_mempool:
1799 mempool_destroy(osdc->req_mempool);
1800out:
1801 return err;
Sage Weilf24e9982009-10-06 11:31:10 -07001802}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001803EXPORT_SYMBOL(ceph_osdc_init);
Sage Weilf24e9982009-10-06 11:31:10 -07001804
1805void ceph_osdc_stop(struct ceph_osd_client *osdc)
1806{
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001807 flush_workqueue(osdc->notify_wq);
1808 destroy_workqueue(osdc->notify_wq);
Sage Weilf24e9982009-10-06 11:31:10 -07001809 cancel_delayed_work_sync(&osdc->timeout_work);
Yehuda Sadehf5a20412010-02-03 11:00:26 -08001810 cancel_delayed_work_sync(&osdc->osds_timeout_work);
Sage Weilf24e9982009-10-06 11:31:10 -07001811 if (osdc->osdmap) {
1812 ceph_osdmap_destroy(osdc->osdmap);
1813 osdc->osdmap = NULL;
1814 }
Sage Weilaca420b2011-08-31 14:45:53 -07001815 remove_all_osds(osdc);
Sage Weilf24e9982009-10-06 11:31:10 -07001816 mempool_destroy(osdc->req_mempool);
1817 ceph_msgpool_destroy(&osdc->msgpool_op);
Sage Weilc16e7862010-03-01 13:02:00 -08001818 ceph_msgpool_destroy(&osdc->msgpool_op_reply);
Sage Weilf24e9982009-10-06 11:31:10 -07001819}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001820EXPORT_SYMBOL(ceph_osdc_stop);
Sage Weilf24e9982009-10-06 11:31:10 -07001821
1822/*
1823 * Read some contiguous pages. If we cross a stripe boundary, shorten
1824 * *plen. Return number of bytes read, or error.
1825 */
1826int ceph_osdc_readpages(struct ceph_osd_client *osdc,
1827 struct ceph_vino vino, struct ceph_file_layout *layout,
1828 u64 off, u64 *plen,
1829 u32 truncate_seq, u64 truncate_size,
Sage Weilb7495fc2010-11-09 12:43:12 -08001830 struct page **pages, int num_pages, int page_align)
Sage Weilf24e9982009-10-06 11:31:10 -07001831{
1832 struct ceph_osd_request *req;
1833 int rc = 0;
1834
1835 dout("readpages on ino %llx.%llx on %llu~%llu\n", vino.ino,
1836 vino.snap, off, *plen);
1837 req = ceph_osdc_new_request(osdc, layout, vino, off, plen,
1838 CEPH_OSD_OP_READ, CEPH_OSD_FLAG_READ,
1839 NULL, 0, truncate_seq, truncate_size, NULL,
Sage Weilb7495fc2010-11-09 12:43:12 -08001840 false, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001841 if (IS_ERR(req))
1842 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001843
1844 /* it may be a short read due to an object boundary */
1845 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001846
Sage Weilb7495fc2010-11-09 12:43:12 -08001847 dout("readpages final extent is %llu~%llu (%d pages align %d)\n",
1848 off, *plen, req->r_num_pages, page_align);
Sage Weilf24e9982009-10-06 11:31:10 -07001849
1850 rc = ceph_osdc_start_request(osdc, req, false);
1851 if (!rc)
1852 rc = ceph_osdc_wait_request(osdc, req);
1853
1854 ceph_osdc_put_request(req);
1855 dout("readpages result %d\n", rc);
1856 return rc;
1857}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001858EXPORT_SYMBOL(ceph_osdc_readpages);
Sage Weilf24e9982009-10-06 11:31:10 -07001859
1860/*
1861 * do a synchronous write on N pages
1862 */
1863int ceph_osdc_writepages(struct ceph_osd_client *osdc, struct ceph_vino vino,
1864 struct ceph_file_layout *layout,
1865 struct ceph_snap_context *snapc,
1866 u64 off, u64 len,
1867 u32 truncate_seq, u64 truncate_size,
1868 struct timespec *mtime,
1869 struct page **pages, int num_pages,
Alex Elderfbf86852013-02-15 11:42:29 -06001870 int flags)
Sage Weilf24e9982009-10-06 11:31:10 -07001871{
1872 struct ceph_osd_request *req;
1873 int rc = 0;
Sage Weilb7495fc2010-11-09 12:43:12 -08001874 int page_align = off & ~PAGE_MASK;
Sage Weilf24e9982009-10-06 11:31:10 -07001875
1876 BUG_ON(vino.snap != CEPH_NOSNAP);
1877 req = ceph_osdc_new_request(osdc, layout, vino, off, &len,
1878 CEPH_OSD_OP_WRITE,
1879 flags | CEPH_OSD_FLAG_ONDISK |
1880 CEPH_OSD_FLAG_WRITE,
Alex Elderfbf86852013-02-15 11:42:29 -06001881 snapc, 0,
Sage Weilf24e9982009-10-06 11:31:10 -07001882 truncate_seq, truncate_size, mtime,
Alex Elder87f979d2013-02-15 11:42:29 -06001883 true, 1, page_align);
Sage Weil68162822012-09-24 21:01:02 -07001884 if (IS_ERR(req))
1885 return PTR_ERR(req);
Sage Weilf24e9982009-10-06 11:31:10 -07001886
1887 /* it may be a short write due to an object boundary */
1888 req->r_pages = pages;
Sage Weilf24e9982009-10-06 11:31:10 -07001889 dout("writepages %llu~%llu (%d pages)\n", off, len,
1890 req->r_num_pages);
1891
Alex Elder87f979d2013-02-15 11:42:29 -06001892 rc = ceph_osdc_start_request(osdc, req, true);
Sage Weilf24e9982009-10-06 11:31:10 -07001893 if (!rc)
1894 rc = ceph_osdc_wait_request(osdc, req);
1895
1896 ceph_osdc_put_request(req);
1897 if (rc == 0)
1898 rc = len;
1899 dout("writepages result %d\n", rc);
1900 return rc;
1901}
Yehuda Sadeh3d14c5d2010-04-06 15:14:15 -07001902EXPORT_SYMBOL(ceph_osdc_writepages);
Sage Weilf24e9982009-10-06 11:31:10 -07001903
1904/*
1905 * handle incoming message
1906 */
1907static void dispatch(struct ceph_connection *con, struct ceph_msg *msg)
1908{
1909 struct ceph_osd *osd = con->private;
Julia Lawall32c895e2009-11-21 16:53:16 +01001910 struct ceph_osd_client *osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001911 int type = le16_to_cpu(msg->hdr.type);
1912
1913 if (!osd)
Sage Weil4a32f932010-06-13 10:27:53 -07001914 goto out;
Julia Lawall32c895e2009-11-21 16:53:16 +01001915 osdc = osd->o_osdc;
Sage Weilf24e9982009-10-06 11:31:10 -07001916
1917 switch (type) {
1918 case CEPH_MSG_OSD_MAP:
1919 ceph_osdc_handle_map(osdc, msg);
1920 break;
1921 case CEPH_MSG_OSD_OPREPLY:
Sage Weil350b1c32009-12-22 10:45:45 -08001922 handle_reply(osdc, msg, con);
Sage Weilf24e9982009-10-06 11:31:10 -07001923 break;
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07001924 case CEPH_MSG_WATCH_NOTIFY:
1925 handle_watch_notify(osdc, msg);
1926 break;
Sage Weilf24e9982009-10-06 11:31:10 -07001927
1928 default:
1929 pr_err("received unknown message type %d %s\n", type,
1930 ceph_msg_type_name(type));
1931 }
Sage Weil4a32f932010-06-13 10:27:53 -07001932out:
Sage Weilf24e9982009-10-06 11:31:10 -07001933 ceph_msg_put(msg);
1934}
1935
Sage Weil5b3a4db2010-02-19 21:43:23 -08001936/*
Sage Weil21b667f2010-03-04 10:22:59 -08001937 * lookup and return message for incoming reply. set up reply message
1938 * pages.
Sage Weil5b3a4db2010-02-19 21:43:23 -08001939 */
1940static struct ceph_msg *get_reply(struct ceph_connection *con,
Yehuda Sadeh24504182010-01-08 13:58:34 -08001941 struct ceph_msg_header *hdr,
1942 int *skip)
Sage Weilf24e9982009-10-06 11:31:10 -07001943{
1944 struct ceph_osd *osd = con->private;
1945 struct ceph_osd_client *osdc = osd->o_osdc;
Yehuda Sadeh24504182010-01-08 13:58:34 -08001946 struct ceph_msg *m;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001947 struct ceph_osd_request *req;
Sage Weil5b3a4db2010-02-19 21:43:23 -08001948 int front = le32_to_cpu(hdr->front_len);
1949 int data_len = le32_to_cpu(hdr->data_len);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001950 u64 tid;
Sage Weilf24e9982009-10-06 11:31:10 -07001951
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001952 tid = le64_to_cpu(hdr->tid);
1953 mutex_lock(&osdc->request_mutex);
1954 req = __lookup_request(osdc, tid);
1955 if (!req) {
1956 *skip = 1;
1957 m = NULL;
Sage Weil756a16a2012-07-30 16:26:13 -07001958 dout("get_reply unknown tid %llu from osd%d\n", tid,
1959 osd->o_osd);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001960 goto out;
1961 }
Sage Weilc16e7862010-03-01 13:02:00 -08001962
1963 if (req->r_con_filling_msg) {
Alex Elder8921d112012-06-01 14:56:43 -05001964 dout("%s revoking msg %p from old con %p\n", __func__,
Sage Weilc16e7862010-03-01 13:02:00 -08001965 req->r_reply, req->r_con_filling_msg);
Alex Elder8921d112012-06-01 14:56:43 -05001966 ceph_msg_revoke_incoming(req->r_reply);
Sage Weil0d477662012-05-31 20:22:18 -07001967 req->r_con_filling_msg->ops->put(req->r_con_filling_msg);
Sage Weil6f46cb22010-03-24 21:30:19 -07001968 req->r_con_filling_msg = NULL;
Sage Weilf24e9982009-10-06 11:31:10 -07001969 }
Yehuda Sadeh24504182010-01-08 13:58:34 -08001970
Sage Weilc16e7862010-03-01 13:02:00 -08001971 if (front > req->r_reply->front.iov_len) {
1972 pr_warning("get_reply front %d > preallocated %d\n",
1973 front, (int)req->r_reply->front.iov_len);
Sage Weilb61c2762011-08-09 15:03:46 -07001974 m = ceph_msg_new(CEPH_MSG_OSD_OPREPLY, front, GFP_NOFS, false);
Sage Weila79832f2010-04-01 16:06:19 -07001975 if (!m)
Sage Weilc16e7862010-03-01 13:02:00 -08001976 goto out;
1977 ceph_msg_put(req->r_reply);
1978 req->r_reply = m;
1979 }
1980 m = ceph_msg_get(req->r_reply);
1981
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001982 if (data_len > 0) {
Sage Weilb7495fc2010-11-09 12:43:12 -08001983 int want = calc_pages_for(req->r_page_alignment, data_len);
Sage Weil21b667f2010-03-04 10:22:59 -08001984
Alex Elder9cbb1d72013-01-31 16:02:00 -06001985 if (req->r_pages && unlikely(req->r_num_pages < want)) {
Sage Weil9bb0ce22011-06-13 16:20:18 -07001986 pr_warning("tid %lld reply has %d bytes %d pages, we"
1987 " had only %d pages ready\n", tid, data_len,
1988 want, req->r_num_pages);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001989 *skip = 1;
1990 ceph_msg_put(m);
Sage Weila79832f2010-04-01 16:06:19 -07001991 m = NULL;
Sage Weil21b667f2010-03-04 10:22:59 -08001992 goto out;
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08001993 }
Sage Weil21b667f2010-03-04 10:22:59 -08001994 m->pages = req->r_pages;
1995 m->nr_pages = req->r_num_pages;
Sage Weilc5c6b192010-11-09 12:40:00 -08001996 m->page_alignment = req->r_page_alignment;
Yehuda Sadeh68b44762010-04-06 15:01:27 -07001997#ifdef CONFIG_BLOCK
1998 m->bio = req->r_bio;
1999#endif
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002000 }
Sage Weil5b3a4db2010-02-19 21:43:23 -08002001 *skip = 0;
Sage Weil0d477662012-05-31 20:22:18 -07002002 req->r_con_filling_msg = con->ops->get(con);
Sage Weilc16e7862010-03-01 13:02:00 -08002003 dout("get_reply tid %lld %p\n", tid, m);
Yehuda Sadeh0547a9b2010-01-11 14:47:13 -08002004
2005out:
2006 mutex_unlock(&osdc->request_mutex);
Yehuda Sadeh24504182010-01-08 13:58:34 -08002007 return m;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002008
2009}
2010
2011static struct ceph_msg *alloc_msg(struct ceph_connection *con,
2012 struct ceph_msg_header *hdr,
2013 int *skip)
2014{
2015 struct ceph_osd *osd = con->private;
2016 int type = le16_to_cpu(hdr->type);
2017 int front = le32_to_cpu(hdr->front_len);
2018
Alex Elder1c20f2d2012-06-04 14:43:32 -05002019 *skip = 0;
Sage Weil5b3a4db2010-02-19 21:43:23 -08002020 switch (type) {
2021 case CEPH_MSG_OSD_MAP:
Yehuda Sadeha40c4f12011-03-21 15:07:16 -07002022 case CEPH_MSG_WATCH_NOTIFY:
Sage Weilb61c2762011-08-09 15:03:46 -07002023 return ceph_msg_new(type, front, GFP_NOFS, false);
Sage Weil5b3a4db2010-02-19 21:43:23 -08002024 case CEPH_MSG_OSD_OPREPLY:
2025 return get_reply(con, hdr, skip);
2026 default:
2027 pr_info("alloc_msg unexpected msg type %d from osd%d\n", type,
2028 osd->o_osd);
2029 *skip = 1;
2030 return NULL;
2031 }
Sage Weilf24e9982009-10-06 11:31:10 -07002032}
2033
2034/*
2035 * Wrappers to refcount containing ceph_osd struct
2036 */
2037static struct ceph_connection *get_osd_con(struct ceph_connection *con)
2038{
2039 struct ceph_osd *osd = con->private;
2040 if (get_osd(osd))
2041 return con;
2042 return NULL;
2043}
2044
2045static void put_osd_con(struct ceph_connection *con)
2046{
2047 struct ceph_osd *osd = con->private;
2048 put_osd(osd);
2049}
2050
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002051/*
2052 * authentication
2053 */
Alex Eldera3530df2012-05-16 15:16:39 -05002054/*
2055 * Note: returned pointer is the address of a structure that's
2056 * managed separately. Caller must *not* attempt to free it.
2057 */
2058static struct ceph_auth_handshake *get_authorizer(struct ceph_connection *con,
Alex Elder8f43fb52012-05-16 15:16:39 -05002059 int *proto, int force_new)
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002060{
2061 struct ceph_osd *o = con->private;
2062 struct ceph_osd_client *osdc = o->o_osdc;
2063 struct ceph_auth_client *ac = osdc->client->monc.auth;
Alex Elder74f18692012-05-16 15:16:39 -05002064 struct ceph_auth_handshake *auth = &o->o_auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002065
Alex Elder74f18692012-05-16 15:16:39 -05002066 if (force_new && auth->authorizer) {
Alex Eldera2556512012-05-16 15:16:39 -05002067 if (ac->ops && ac->ops->destroy_authorizer)
2068 ac->ops->destroy_authorizer(ac, auth->authorizer);
Alex Elder74f18692012-05-16 15:16:39 -05002069 auth->authorizer = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002070 }
Alex Eldera2556512012-05-16 15:16:39 -05002071 if (!auth->authorizer && ac->ops && ac->ops->create_authorizer) {
Alex Eldera3530df2012-05-16 15:16:39 -05002072 int ret = ac->ops->create_authorizer(ac, CEPH_ENTITY_TYPE_OSD,
2073 auth);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002074 if (ret)
Alex Eldera3530df2012-05-16 15:16:39 -05002075 return ERR_PTR(ret);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002076 }
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002077 *proto = ac->protocol;
Alex Elder74f18692012-05-16 15:16:39 -05002078
Alex Eldera3530df2012-05-16 15:16:39 -05002079 return auth;
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002080}
2081
2082
2083static int verify_authorizer_reply(struct ceph_connection *con, int len)
2084{
2085 struct ceph_osd *o = con->private;
2086 struct ceph_osd_client *osdc = o->o_osdc;
2087 struct ceph_auth_client *ac = osdc->client->monc.auth;
2088
Alex Eldera2556512012-05-16 15:16:39 -05002089 /*
2090 * XXX If ac->ops or ac->ops->verify_authorizer_reply is null,
2091 * XXX which do we do: succeed or fail?
2092 */
Alex Elder6c4a1912012-05-16 15:16:38 -05002093 return ac->ops->verify_authorizer_reply(ac, o->o_auth.authorizer, len);
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002094}
2095
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002096static int invalidate_authorizer(struct ceph_connection *con)
2097{
2098 struct ceph_osd *o = con->private;
2099 struct ceph_osd_client *osdc = o->o_osdc;
2100 struct ceph_auth_client *ac = osdc->client->monc.auth;
2101
Alex Eldera2556512012-05-16 15:16:39 -05002102 if (ac->ops && ac->ops->invalidate_authorizer)
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002103 ac->ops->invalidate_authorizer(ac, CEPH_ENTITY_TYPE_OSD);
2104
2105 return ceph_monc_validate_auth(&osdc->client->monc);
2106}
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002107
Tobias Klauser9e327892010-05-20 10:40:19 +02002108static const struct ceph_connection_operations osd_con_ops = {
Sage Weilf24e9982009-10-06 11:31:10 -07002109 .get = get_osd_con,
2110 .put = put_osd_con,
2111 .dispatch = dispatch,
Sage Weil4e7a5dc2009-11-18 16:19:57 -08002112 .get_authorizer = get_authorizer,
2113 .verify_authorizer_reply = verify_authorizer_reply,
Sage Weil9bd2e6f2010-02-02 16:21:06 -08002114 .invalidate_authorizer = invalidate_authorizer,
Sage Weilf24e9982009-10-06 11:31:10 -07002115 .alloc_msg = alloc_msg,
Sage Weil81b024e2009-10-09 10:29:18 -07002116 .fault = osd_reset,
Sage Weilf24e9982009-10-06 11:31:10 -07002117};