Whamcloud - gitweb
LU-12567 ptlrpc: handle reply and resend reorder
[fs/lustre-release.git] / lustre / ptlrpc / llog_client.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/ptlrpc/llog_client.c
32  *
33  * remote api for llog - client side
34  *
35  * Author: Andreas Dilger <adilger@clusterfs.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_LOG
39
40 #include <linux/list.h>
41 #include <libcfs/libcfs.h>
42
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <lustre_net.h>
46
47 #include "ptlrpc_internal.h"
48
49 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                             \
50         mutex_lock(&ctxt->loc_mutex);                                 \
51         if (ctxt->loc_imp) {                                          \
52                 imp = class_import_get(ctxt->loc_imp);                \
53         } else {                                                      \
54                 CERROR("ctxt->loc_imp == NULL for context idx %d."    \
55                        "Unable to complete MDS/OSS recovery,"         \
56                        "but I'll try again next time. Not fatal.\n", \
57                        ctxt->loc_idx);                                \
58                 imp = NULL;                                           \
59                 mutex_unlock(&ctxt->loc_mutex);                       \
60                 return -EINVAL;                                       \
61         }                                                             \
62         mutex_unlock(&ctxt->loc_mutex);                               \
63 } while (0)
64
65 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
66         mutex_lock(&ctxt->loc_mutex);                                 \
67         if (ctxt->loc_imp != imp)                                     \
68                 CWARN("loc_imp has changed from %p to %p\n",          \
69                       ctxt->loc_imp, imp);                            \
70         class_import_put(imp);                                        \
71         mutex_unlock(&ctxt->loc_mutex);                               \
72 } while (0)
73
74 /*
75  * This is a callback from the llog_* functions.
76  * Assumes caller has already pushed us into the kernel context.
77  */
78 static int llog_client_open(const struct lu_env *env,
79                             struct llog_handle *lgh, struct llog_logid *logid,
80                             char *name, enum llog_open_param open_param)
81 {
82         struct obd_import *imp;
83         struct llogd_body *body;
84         struct llog_ctxt *ctxt = lgh->lgh_ctxt;
85         struct ptlrpc_request *req = NULL;
86         int rc;
87
88         ENTRY;
89
90         LLOG_CLIENT_ENTRY(ctxt, imp);
91
92         /* client cannot create llog */
93         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
94         LASSERT(lgh);
95
96         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
97         if (!req)
98                 GOTO(out, rc = -ENOMEM);
99
100         if (name)
101                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
102                                      strlen(name) + 1);
103
104         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
105                                  LLOG_ORIGIN_HANDLE_CREATE);
106         if (rc) {
107                 ptlrpc_request_free(req);
108                 req = NULL;
109                 GOTO(out, rc);
110         }
111         ptlrpc_request_set_replen(req);
112
113         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
114         if (logid)
115                 body->lgd_logid = *logid;
116         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
117
118         if (name) {
119                 char *tmp;
120
121                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
122                                                    strlen(name) + 1);
123                 LASSERT(tmp);
124                 strcpy(tmp, name);
125
126                 do_pack_body(req);
127         }
128
129         rc = ptlrpc_queue_wait(req);
130         if (rc)
131                 GOTO(out, rc);
132
133         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
134         if (!body)
135                 GOTO(out, rc = -EFAULT);
136
137         lgh->lgh_id = body->lgd_logid;
138         lgh->lgh_ctxt = ctxt;
139         EXIT;
140 out:
141         LLOG_CLIENT_EXIT(ctxt, imp);
142         ptlrpc_req_finished(req);
143         return rc;
144 }
145
146 static int llog_client_next_block(const struct lu_env *env,
147                                   struct llog_handle *loghandle,
148                                   int *cur_idx, int next_idx,
149                                   __u64 *cur_offset, void *buf, int len)
150 {
151         struct obd_import *imp;
152         struct ptlrpc_request *req = NULL;
153         struct llogd_body *body;
154         void *ptr;
155         int rc;
156
157         ENTRY;
158
159         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
160         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
161                                         LUSTRE_LOG_VERSION,
162                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
163         if (!req)
164                 GOTO(err_exit, rc = -ENOMEM);
165
166         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
167         body->lgd_logid = loghandle->lgh_id;
168         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
169         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
170         body->lgd_index = next_idx;
171         body->lgd_saved_index = *cur_idx;
172         body->lgd_len = len;
173         body->lgd_cur_offset = *cur_offset;
174
175         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
176         ptlrpc_request_set_replen(req);
177         rc = ptlrpc_queue_wait(req);
178         /*
179          * -EIO has a special meaning here. If llog_osd_next_block()
180          * reaches the end of the log without finding the desired
181          * record then it updates *cur_offset and *cur_idx and returns
182          * -EIO. In llog_process_thread() we use this to detect
183          * EOF. But we must be careful to distinguish between -EIO
184          * coming from llog_osd_next_block() and -EIO coming from
185          * ptlrpc or below.
186          */
187         if (rc == -EIO) {
188                 if (!req->rq_repmsg ||
189                     lustre_msg_get_status(req->rq_repmsg) != -EIO)
190                         GOTO(out, rc);
191         } else if (rc < 0) {
192                 GOTO(out, rc);
193         }
194
195         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
196         if (!body)
197                 GOTO(out, rc = -EFAULT);
198
199         *cur_idx = body->lgd_saved_index;
200         *cur_offset = body->lgd_cur_offset;
201
202         if (rc < 0)
203                 GOTO(out, rc);
204
205         /* The log records are swabbed as they are processed */
206         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
207         if (!ptr)
208                 GOTO(out, rc = -EFAULT);
209
210         memcpy(buf, ptr, len);
211         EXIT;
212 out:
213         ptlrpc_req_finished(req);
214 err_exit:
215         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
216         return rc;
217 }
218
219 static int llog_client_prev_block(const struct lu_env *env,
220                                   struct llog_handle *loghandle,
221                                   int prev_idx, void *buf, int len)
222 {
223         struct obd_import *imp;
224         struct ptlrpc_request *req = NULL;
225         struct llogd_body *body;
226         void *ptr;
227         int rc;
228
229         ENTRY;
230
231         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
232         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
233                                         LUSTRE_LOG_VERSION,
234                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
235         if (!req)
236                 GOTO(err_exit, rc = -ENOMEM);
237
238         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
239         body->lgd_logid = loghandle->lgh_id;
240         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
241         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
242         body->lgd_index = prev_idx;
243         body->lgd_len = len;
244
245         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
246         ptlrpc_request_set_replen(req);
247
248         rc = ptlrpc_queue_wait(req);
249         if (rc)
250                 GOTO(out, rc);
251
252         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
253         if (!body)
254                 GOTO(out, rc = -EFAULT);
255
256         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
257         if (!ptr)
258                 GOTO(out, rc = -EFAULT);
259
260         memcpy(buf, ptr, len);
261         EXIT;
262 out:
263         ptlrpc_req_finished(req);
264 err_exit:
265         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
266         return rc;
267 }
268
269 static int llog_client_read_header(const struct lu_env *env,
270                                    struct llog_handle *handle)
271 {
272         struct obd_import *imp;
273         struct ptlrpc_request *req = NULL;
274         struct llogd_body *body;
275         struct llog_log_hdr *hdr;
276         struct llog_rec_hdr *llh_hdr;
277         int rc;
278
279         ENTRY;
280
281         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
282         req = ptlrpc_request_alloc_pack(imp,
283                                         &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
284                                         LUSTRE_LOG_VERSION,
285                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
286         if (!req)
287                 GOTO(err_exit, rc = -ENOMEM);
288
289         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
290         body->lgd_logid = handle->lgh_id;
291         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
292         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
293
294         ptlrpc_request_set_replen(req);
295         rc = ptlrpc_queue_wait(req);
296         if (rc)
297                 GOTO(out, rc);
298
299         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
300         if (!hdr)
301                 GOTO(out, rc = -EFAULT);
302
303         if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
304                 GOTO(out, rc = -EFAULT);
305
306         memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
307         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
308
309         /* sanity checks */
310         llh_hdr = &handle->lgh_hdr->llh_hdr;
311         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
312                 CERROR("bad log header magic: %#x (expecting %#x)\n",
313                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
314                 rc = -EIO;
315         } else if (llh_hdr->lrh_len !=
316                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len ||
317                    (llh_hdr->lrh_len & (llh_hdr->lrh_len - 1)) != 0 ||
318                    llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
319                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
320                 CERROR("incorrectly sized log header: %#x, expecting %#x (power of two > 8192)\n",
321                        llh_hdr->lrh_len,
322                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
323                 CERROR("you may need to re-run lconf --write_conf.\n");
324                 rc = -EIO;
325         }
326         EXIT;
327 out:
328         ptlrpc_req_finished(req);
329 err_exit:
330         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
331         return rc;
332 }
333
334 static int llog_client_close(const struct lu_env *env,
335                              struct llog_handle *handle)
336 {
337         /*
338          * this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
339          * the servers all close the file at the end of every
340          * other LLOG_ RPC.
341          */
342         return 0;
343 }
344
345 const struct llog_operations llog_client_ops = {
346         .lop_next_block         = llog_client_next_block,
347         .lop_prev_block         = llog_client_prev_block,
348         .lop_read_header        = llog_client_read_header,
349         .lop_open               = llog_client_open,
350         .lop_close              = llog_client_close,
351 };
352 EXPORT_SYMBOL(llog_client_ops);