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/
31 * lustre/ptlrpc/llog_client.c
33 * remote api for llog - client side
35 * Author: Andreas Dilger <adilger@clusterfs.com>
38 #define DEBUG_SUBSYSTEM S_LOG
40 #include <linux/list.h>
41 #include <libcfs/libcfs.h>
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <lustre_net.h>
47 #include "ptlrpc_internal.h"
49 static struct obd_import *llog_client_entry(struct llog_ctxt *ctxt)
51 struct obd_import *imp = ERR_PTR(-EINVAL);
53 mutex_lock(&ctxt->loc_mutex);
55 imp = class_import_get(ctxt->loc_imp);
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));
61 mutex_unlock(&ctxt->loc_mutex);
65 static void llog_client_exit(struct llog_ctxt *ctxt, struct obd_import *imp)
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",
72 class_import_put(imp);
73 mutex_unlock(&ctxt->loc_mutex);
77 * This is a callback from the llog_* functions.
78 * Assumes caller has already pushed us into the kernel context.
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)
84 struct obd_import *imp;
85 struct llogd_body *body;
86 struct llog_ctxt *ctxt = lgh->lgh_ctxt;
87 struct ptlrpc_request *req = NULL;
92 imp = llog_client_entry(ctxt);
96 /* client cannot create llog */
97 LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
100 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
102 GOTO(out, rc = -ENOMEM);
105 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
108 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
109 LLOG_ORIGIN_HANDLE_CREATE);
111 ptlrpc_request_free(req);
115 ptlrpc_request_set_replen(req);
117 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
119 body->lgd_logid = *logid;
120 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
125 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
133 rc = ptlrpc_queue_wait(req);
137 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
139 GOTO(out, rc = -EFAULT);
141 lgh->lgh_id = body->lgd_logid;
142 lgh->lgh_ctxt = ctxt;
145 llog_client_exit(ctxt, imp);
146 ptlrpc_req_finished(req);
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)
155 struct obd_import *imp;
156 struct ptlrpc_request *req = NULL;
157 struct llogd_body *body;
163 imp = llog_client_entry(loghandle->lgh_ctxt);
167 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
169 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
171 GOTO(err_exit, rc = -ENOMEM);
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;
180 body->lgd_cur_offset = *cur_offset;
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);
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
195 if (!req->rq_repmsg ||
196 lustre_msg_get_status(req->rq_repmsg) != -EIO)
202 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
204 GOTO(out, rc = -EFAULT);
206 *cur_idx = body->lgd_saved_index;
207 *cur_offset = body->lgd_cur_offset;
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 memcpy(buf, ptr, len);
220 ptlrpc_req_finished(req);
222 llog_client_exit(loghandle->lgh_ctxt, imp);
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)
230 struct obd_import *imp;
231 struct ptlrpc_request *req = NULL;
232 struct llogd_body *body;
238 imp = llog_client_entry(loghandle->lgh_ctxt);
242 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
244 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
246 GOTO(err_exit, rc = -ENOMEM);
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;
255 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
256 ptlrpc_request_set_replen(req);
258 rc = ptlrpc_queue_wait(req);
262 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
264 GOTO(out, rc = -EFAULT);
266 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
268 GOTO(out, rc = -EFAULT);
270 memcpy(buf, ptr, len);
273 ptlrpc_req_finished(req);
275 llog_client_exit(loghandle->lgh_ctxt, imp);
279 static int llog_client_read_header(const struct lu_env *env,
280 struct llog_handle *handle)
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;
291 imp = llog_client_entry(handle->lgh_ctxt);
295 req = ptlrpc_request_alloc_pack(imp,
296 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
298 LLOG_ORIGIN_HANDLE_READ_HEADER);
300 GOTO(err_exit, rc = -ENOMEM);
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;
307 ptlrpc_request_set_replen(req);
308 rc = ptlrpc_queue_wait(req);
312 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
314 GOTO(out, rc = -EFAULT);
316 if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
317 GOTO(out, rc = -EFAULT);
319 memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
320 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
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);
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",
335 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
336 CERROR("you may need to re-run lconf --write_conf.\n");
341 ptlrpc_req_finished(req);
343 llog_client_exit(handle->lgh_ctxt, imp);
347 static int llog_client_close(const struct lu_env *env,
348 struct llog_handle *handle)
351 * this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
352 * the servers all close the file at the end of every
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,
365 EXPORT_SYMBOL(llog_client_ops);