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