Whamcloud - gitweb
LU-10267 llog: fix EOF handling in llog_client_next_block()
[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  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/llog_client.c
33  *
34  * remote api for llog - client side
35  *
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_LOG
40
41 #include <linux/list.h>
42 #include <libcfs/libcfs.h>
43
44 #include <obd_class.h>
45 #include <lustre_log.h>
46 #include <lustre_net.h>
47
48 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                             \
49         mutex_lock(&ctxt->loc_mutex);                             \
50         if (ctxt->loc_imp) {                                          \
51                 imp = class_import_get(ctxt->loc_imp);                \
52         } else {                                                      \
53                 CERROR("ctxt->loc_imp == NULL for context idx %d."    \
54                        "Unable to complete MDS/OSS recovery,"         \
55                        "but I'll try again next time.  Not fatal.\n", \
56                        ctxt->loc_idx);                                \
57                 imp = NULL;                                           \
58                 mutex_unlock(&ctxt->loc_mutex);                   \
59                 return (-EINVAL);                                     \
60         }                                                             \
61         mutex_unlock(&ctxt->loc_mutex);                           \
62 } while(0)
63
64 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
65         mutex_lock(&ctxt->loc_mutex);                             \
66         if (ctxt->loc_imp != imp)                                     \
67                 CWARN("loc_imp has changed from %p to %p\n",          \
68                        ctxt->loc_imp, imp);                           \
69         class_import_put(imp);                                        \
70         mutex_unlock(&ctxt->loc_mutex);                           \
71 } while(0)
72
73 /* This is a callback from the llog_* functions.
74  * Assumes caller has already pushed us into the kernel context. */
75 static int llog_client_open(const struct lu_env *env,
76                             struct llog_handle *lgh, struct llog_logid *logid,
77                             char *name, enum llog_open_param open_param)
78 {
79         struct obd_import     *imp;
80         struct llogd_body     *body;
81         struct llog_ctxt      *ctxt = lgh->lgh_ctxt;
82         struct ptlrpc_request *req = NULL;
83         int                    rc;
84         ENTRY;
85
86         LLOG_CLIENT_ENTRY(ctxt, imp);
87
88         /* client cannot create llog */
89         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
90         LASSERT(lgh);
91
92         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
93         if (req == NULL)
94                 GOTO(out, rc = -ENOMEM);
95
96         if (name)
97                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
98                                      strlen(name) + 1);
99
100         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
101                                  LLOG_ORIGIN_HANDLE_CREATE);
102         if (rc) {
103                 ptlrpc_request_free(req);
104                 req = NULL;
105                 GOTO(out, rc);
106         }
107         ptlrpc_request_set_replen(req);
108
109         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
110         if (logid)
111                 body->lgd_logid = *logid;
112         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
113
114         if (name) {
115                 char *tmp;
116                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
117                                                    strlen(name) + 1);
118                 LASSERT(tmp);
119                 strcpy(tmp, name);
120         }
121
122         rc = ptlrpc_queue_wait(req);
123         if (rc)
124                 GOTO(out, rc);
125
126         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
127         if (body == NULL)
128                 GOTO(out, rc = -EFAULT);
129
130         lgh->lgh_id = body->lgd_logid;
131         lgh->lgh_ctxt = ctxt;
132         EXIT;
133 out:
134         LLOG_CLIENT_EXIT(ctxt, imp);
135         ptlrpc_req_finished(req);
136         return rc;
137 }
138
139 static int llog_client_destroy(const struct lu_env *env,
140                                struct llog_handle *loghandle,
141                                struct thandle *th)
142 {
143         struct obd_import     *imp;
144         struct ptlrpc_request *req = NULL;
145         struct llogd_body     *body;
146         int                    rc;
147         ENTRY;
148
149         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
150         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
151                                         LUSTRE_LOG_VERSION,
152                                         LLOG_ORIGIN_HANDLE_DESTROY);
153         if (req == NULL)
154                 GOTO(err_exit, rc =-ENOMEM);
155
156         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
157         body->lgd_logid = loghandle->lgh_id;
158         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
159
160         if (!(body->lgd_llh_flags & LLOG_F_IS_PLAIN))
161                 CERROR("%s: wrong llog flags %x\n", imp->imp_obd->obd_name,
162                        body->lgd_llh_flags);
163
164         ptlrpc_request_set_replen(req);
165         rc = ptlrpc_queue_wait(req);
166
167         ptlrpc_req_finished(req);
168 err_exit:
169         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
170         RETURN(rc);
171 }
172
173
174 static int llog_client_next_block(const struct lu_env *env,
175                                   struct llog_handle *loghandle,
176                                   int *cur_idx, int next_idx,
177                                   __u64 *cur_offset, void *buf, int len)
178 {
179         struct obd_import     *imp;
180         struct ptlrpc_request *req = NULL;
181         struct llogd_body     *body;
182         void                  *ptr;
183         int                    rc;
184         ENTRY;
185
186         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
187         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
188                                         LUSTRE_LOG_VERSION,
189                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
190         if (req == NULL)
191                 GOTO(err_exit, rc =-ENOMEM);
192
193         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
194         body->lgd_logid = loghandle->lgh_id;
195         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
196         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
197         body->lgd_index = next_idx;
198         body->lgd_saved_index = *cur_idx;
199         body->lgd_len = len;
200         body->lgd_cur_offset = *cur_offset;
201
202         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
203         ptlrpc_request_set_replen(req);
204         rc = ptlrpc_queue_wait(req);
205         /* -EIO has a special meaning here. If llog_osd_next_block()
206          * reaches the end of the log without finding the desired
207          * record then it updates *cur_offset and *cur_idx and returns
208          * -EIO. In llog_process_thread() we use this to detect
209          * EOF. But we must be careful to distinguish between -EIO
210          * coming from llog_osd_next_block() and -EIO coming from
211          * ptlrpc or below. */
212         if (rc == -EIO) {
213                 if (req->rq_repmsg == NULL ||
214                     lustre_msg_get_status(req->rq_repmsg) != -EIO)
215                         GOTO(out, rc);
216         } else if (rc < 0) {
217                 GOTO(out, rc);
218         }
219
220         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
221         if (body == NULL)
222                 GOTO(out, rc = -EFAULT);
223
224         *cur_idx = body->lgd_saved_index;
225         *cur_offset = body->lgd_cur_offset;
226
227         if (rc < 0)
228                 GOTO(out, rc);
229
230         /* The log records are swabbed as they are processed */
231         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
232         if (ptr == NULL)
233                 GOTO(out, rc =-EFAULT);
234
235         memcpy(buf, ptr, len);
236         EXIT;
237 out:
238         ptlrpc_req_finished(req);
239 err_exit:
240         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
241         return rc;
242 }
243
244 static int llog_client_prev_block(const struct lu_env *env,
245                                   struct llog_handle *loghandle,
246                                   int prev_idx, void *buf, int len)
247 {
248         struct obd_import     *imp;
249         struct ptlrpc_request *req = NULL;
250         struct llogd_body     *body;
251         void                  *ptr;
252         int                    rc;
253         ENTRY;
254
255         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
256         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
257                                         LUSTRE_LOG_VERSION,
258                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
259         if (req == NULL)
260                 GOTO(err_exit, rc = -ENOMEM);
261
262         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
263         body->lgd_logid = loghandle->lgh_id;
264         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
265         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
266         body->lgd_index = prev_idx;
267         body->lgd_len = len;
268
269         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
270         ptlrpc_request_set_replen(req);
271
272         rc = ptlrpc_queue_wait(req);
273         if (rc)
274                 GOTO(out, rc);
275
276         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
277         if (body == NULL)
278                 GOTO(out, rc =-EFAULT);
279
280         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
281         if (ptr == NULL)
282                 GOTO(out, rc =-EFAULT);
283
284         memcpy(buf, ptr, len);
285         EXIT;
286 out:
287         ptlrpc_req_finished(req);
288 err_exit:
289         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
290         return rc;
291 }
292
293 static int llog_client_read_header(const struct lu_env *env,
294                                    struct llog_handle *handle)
295 {
296         struct obd_import     *imp;
297         struct ptlrpc_request *req = NULL;
298         struct llogd_body     *body;
299         struct llog_log_hdr   *hdr;
300         struct llog_rec_hdr   *llh_hdr;
301         int                    rc;
302         ENTRY;
303
304         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
305         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
306                                         LUSTRE_LOG_VERSION,
307                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
308         if (req == NULL)
309                 GOTO(err_exit, rc = -ENOMEM);
310
311         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
312         body->lgd_logid = handle->lgh_id;
313         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
314         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
315
316         ptlrpc_request_set_replen(req);
317         rc = ptlrpc_queue_wait(req);
318         if (rc)
319                 GOTO(out, rc);
320
321         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
322         if (hdr == NULL)
323                 GOTO(out, rc =-EFAULT);
324
325         if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
326                 GOTO(out, rc = -EFAULT);
327
328         memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
329         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
330
331         /* sanity checks */
332         llh_hdr = &handle->lgh_hdr->llh_hdr;
333         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
334                 CERROR("bad log header magic: %#x (expecting %#x)\n",
335                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
336                 rc = -EIO;
337         } else if (llh_hdr->lrh_len !=
338                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len ||
339                    (llh_hdr->lrh_len & (llh_hdr->lrh_len - 1)) != 0 ||
340                    llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
341                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
342                 CERROR("incorrectly sized log header: %#x, "
343                        "expecting %#x (power of two > 8192)\n",
344                        llh_hdr->lrh_len,
345                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
346                 CERROR("you may need to re-run lconf --write_conf.\n");
347                 rc = -EIO;
348         }
349         EXIT;
350 out:
351         ptlrpc_req_finished(req);
352 err_exit:
353         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
354         return rc;
355 }
356
357 static int llog_client_close(const struct lu_env *env,
358                              struct llog_handle *handle)
359 {
360         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
361            the servers all close the file at the end of every
362            other LLOG_ RPC. */
363         return(0);
364 }
365
366 struct llog_operations llog_client_ops = {
367         .lop_next_block         = llog_client_next_block,
368         .lop_prev_block         = llog_client_prev_block,
369         .lop_read_header        = llog_client_read_header,
370         .lop_open               = llog_client_open,
371         .lop_destroy            = llog_client_destroy,
372         .lop_close              = llog_client_close,
373 };
374 EXPORT_SYMBOL(llog_client_ops);