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 #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); \
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", \
59 mutex_unlock(&ctxt->loc_mutex); \
62 mutex_unlock(&ctxt->loc_mutex); \
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); \
75 * This is a callback from the llog_* functions.
76 * Assumes caller has already pushed us into the kernel context.
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)
82 struct obd_import *imp;
83 struct llogd_body *body;
84 struct llog_ctxt *ctxt = lgh->lgh_ctxt;
85 struct ptlrpc_request *req = NULL;
90 LLOG_CLIENT_ENTRY(ctxt, imp);
92 /* client cannot create llog */
93 LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
96 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
98 GOTO(out, rc = -ENOMEM);
101 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
104 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
105 LLOG_ORIGIN_HANDLE_CREATE);
107 ptlrpc_request_free(req);
111 ptlrpc_request_set_replen(req);
113 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
115 body->lgd_logid = *logid;
116 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
121 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
129 rc = ptlrpc_queue_wait(req);
133 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
135 GOTO(out, rc = -EFAULT);
137 lgh->lgh_id = body->lgd_logid;
138 lgh->lgh_ctxt = ctxt;
141 LLOG_CLIENT_EXIT(ctxt, imp);
142 ptlrpc_req_finished(req);
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)
151 struct obd_import *imp;
152 struct ptlrpc_request *req = NULL;
153 struct llogd_body *body;
159 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
160 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
162 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
164 GOTO(err_exit, rc = -ENOMEM);
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;
173 body->lgd_cur_offset = *cur_offset;
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);
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
188 if (!req->rq_repmsg ||
189 lustre_msg_get_status(req->rq_repmsg) != -EIO)
195 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
197 GOTO(out, rc = -EFAULT);
199 *cur_idx = body->lgd_saved_index;
200 *cur_offset = body->lgd_cur_offset;
205 /* The log records are swabbed as they are processed */
206 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
208 GOTO(out, rc = -EFAULT);
210 memcpy(buf, ptr, len);
213 ptlrpc_req_finished(req);
215 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
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)
223 struct obd_import *imp;
224 struct ptlrpc_request *req = NULL;
225 struct llogd_body *body;
231 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
232 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
234 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
236 GOTO(err_exit, rc = -ENOMEM);
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;
245 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
246 ptlrpc_request_set_replen(req);
248 rc = ptlrpc_queue_wait(req);
252 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
254 GOTO(out, rc = -EFAULT);
256 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
258 GOTO(out, rc = -EFAULT);
260 memcpy(buf, ptr, len);
263 ptlrpc_req_finished(req);
265 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
269 static int llog_client_read_header(const struct lu_env *env,
270 struct llog_handle *handle)
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;
281 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
282 req = ptlrpc_request_alloc_pack(imp,
283 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
285 LLOG_ORIGIN_HANDLE_READ_HEADER);
287 GOTO(err_exit, rc = -ENOMEM);
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;
294 ptlrpc_request_set_replen(req);
295 rc = ptlrpc_queue_wait(req);
299 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
301 GOTO(out, rc = -EFAULT);
303 if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
304 GOTO(out, rc = -EFAULT);
306 memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
307 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
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);
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",
322 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
323 CERROR("you may need to re-run lconf --write_conf.\n");
328 ptlrpc_req_finished(req);
330 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
334 static int llog_client_close(const struct lu_env *env,
335 struct llog_handle *handle)
338 * this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
339 * the servers all close the file at the end of every
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,
352 EXPORT_SYMBOL(llog_client_ops);