4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * lustre/ptlrpc/llog_client.c
34 * remote api for llog - client side
36 * Author: Andreas Dilger <adilger@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LOG
41 #include <linux/list.h>
42 #include <libcfs/libcfs.h>
44 #include <obd_class.h>
45 #include <lustre_log.h>
46 #include <lustre_net.h>
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); \
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", \
58 mutex_unlock(&ctxt->loc_mutex); \
61 mutex_unlock(&ctxt->loc_mutex); \
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); \
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)
79 struct obd_import *imp;
80 struct llogd_body *body;
81 struct llog_ctxt *ctxt = lgh->lgh_ctxt;
82 struct ptlrpc_request *req = NULL;
86 LLOG_CLIENT_ENTRY(ctxt, imp);
88 /* client cannot create llog */
89 LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
92 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
94 GOTO(out, rc = -ENOMEM);
97 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
100 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
101 LLOG_ORIGIN_HANDLE_CREATE);
103 ptlrpc_request_free(req);
107 ptlrpc_request_set_replen(req);
109 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
111 body->lgd_logid = *logid;
112 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
116 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
122 rc = ptlrpc_queue_wait(req);
126 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
128 GOTO(out, rc = -EFAULT);
130 lgh->lgh_id = body->lgd_logid;
131 lgh->lgh_ctxt = ctxt;
134 LLOG_CLIENT_EXIT(ctxt, imp);
135 ptlrpc_req_finished(req);
139 static int llog_client_destroy(const struct lu_env *env,
140 struct llog_handle *loghandle,
143 struct obd_import *imp;
144 struct ptlrpc_request *req = NULL;
145 struct llogd_body *body;
149 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
150 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
152 LLOG_ORIGIN_HANDLE_DESTROY);
154 GOTO(err_exit, rc =-ENOMEM);
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;
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);
164 ptlrpc_request_set_replen(req);
165 rc = ptlrpc_queue_wait(req);
167 ptlrpc_req_finished(req);
169 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
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)
179 struct obd_import *imp;
180 struct ptlrpc_request *req = NULL;
181 struct llogd_body *body;
186 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
187 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
189 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
191 GOTO(err_exit, rc =-ENOMEM);
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;
200 body->lgd_cur_offset = *cur_offset;
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);
208 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
210 GOTO(out, rc =-EFAULT);
212 /* The log records are swabbed as they are processed */
213 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
215 GOTO(out, rc =-EFAULT);
217 *cur_idx = body->lgd_saved_index;
218 *cur_offset = body->lgd_cur_offset;
220 memcpy(buf, ptr, len);
223 ptlrpc_req_finished(req);
225 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
229 static int llog_client_prev_block(const struct lu_env *env,
230 struct llog_handle *loghandle,
231 int prev_idx, void *buf, int len)
233 struct obd_import *imp;
234 struct ptlrpc_request *req = NULL;
235 struct llogd_body *body;
240 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
241 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
243 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
245 GOTO(err_exit, rc = -ENOMEM);
247 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
248 body->lgd_logid = loghandle->lgh_id;
249 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
250 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
251 body->lgd_index = prev_idx;
254 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
255 ptlrpc_request_set_replen(req);
257 rc = ptlrpc_queue_wait(req);
261 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
263 GOTO(out, rc =-EFAULT);
265 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
267 GOTO(out, rc =-EFAULT);
269 memcpy(buf, ptr, len);
272 ptlrpc_req_finished(req);
274 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
278 static int llog_client_read_header(const struct lu_env *env,
279 struct llog_handle *handle)
281 struct obd_import *imp;
282 struct ptlrpc_request *req = NULL;
283 struct llogd_body *body;
284 struct llog_log_hdr *hdr;
285 struct llog_rec_hdr *llh_hdr;
289 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
290 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
292 LLOG_ORIGIN_HANDLE_READ_HEADER);
294 GOTO(err_exit, rc = -ENOMEM);
296 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
297 body->lgd_logid = handle->lgh_id;
298 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
299 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
301 ptlrpc_request_set_replen(req);
302 rc = ptlrpc_queue_wait(req);
306 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
308 GOTO(out, rc =-EFAULT);
310 if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
311 GOTO(out, rc = -EFAULT);
313 memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
314 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
317 llh_hdr = &handle->lgh_hdr->llh_hdr;
318 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
319 CERROR("bad log header magic: %#x (expecting %#x)\n",
320 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
322 } else if (llh_hdr->lrh_len !=
323 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len ||
324 (llh_hdr->lrh_len & (llh_hdr->lrh_len - 1)) != 0 ||
325 llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
326 llh_hdr->lrh_len > handle->lgh_hdr_size) {
327 CERROR("incorrectly sized log header: %#x, "
328 "expecting %#x (power of two > 8192)\n",
330 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
331 CERROR("you may need to re-run lconf --write_conf.\n");
336 ptlrpc_req_finished(req);
338 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
342 static int llog_client_close(const struct lu_env *env,
343 struct llog_handle *handle)
345 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
346 the servers all close the file at the end of every
351 struct llog_operations llog_client_ops = {
352 .lop_next_block = llog_client_next_block,
353 .lop_prev_block = llog_client_prev_block,
354 .lop_read_header = llog_client_read_header,
355 .lop_open = llog_client_open,
356 .lop_destroy = llog_client_destroy,
357 .lop_close = llog_client_close,
359 EXPORT_SYMBOL(llog_client_ops);