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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/ptlrpc/llog_client.c
38 * remote api for llog - client side
40 * Author: Andreas Dilger <adilger@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_LOG
46 #include <libcfs/libcfs.h>
48 #include <liblustre.h>
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include <lustre_net.h>
54 #include <libcfs/list.h>
56 #define LLOG_CLIENT_ENTRY(ctxt, imp) do { \
57 cfs_mutex_lock(&ctxt->loc_mutex); \
58 if (ctxt->loc_imp) { \
59 imp = class_import_get(ctxt->loc_imp); \
61 CERROR("ctxt->loc_imp == NULL for context idx %d." \
62 "Unable to complete MDS/OSS recovery," \
63 "but I'll try again next time. Not fatal.\n", \
66 cfs_mutex_unlock(&ctxt->loc_mutex); \
69 cfs_mutex_unlock(&ctxt->loc_mutex); \
72 #define LLOG_CLIENT_EXIT(ctxt, imp) do { \
73 cfs_mutex_lock(&ctxt->loc_mutex); \
74 if (ctxt->loc_imp != imp) \
75 CWARN("loc_imp has changed from %p to %p\n", \
76 ctxt->loc_imp, imp); \
77 class_import_put(imp); \
78 cfs_mutex_unlock(&ctxt->loc_mutex); \
81 /* This is a callback from the llog_* functions.
82 * Assumes caller has already pushed us into the kernel context. */
83 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
84 struct llog_logid *logid, char *name)
86 struct obd_import *imp;
87 struct llogd_body *body;
88 struct llog_handle *handle;
89 struct ptlrpc_request *req = NULL;
93 LLOG_CLIENT_ENTRY(ctxt, imp);
95 handle = llog_alloc_handle();
100 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
102 GOTO(err_free, 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;
124 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
130 rc = ptlrpc_queue_wait(req);
134 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
136 GOTO(err_free, rc =-EFAULT);
138 handle->lgh_id = body->lgd_logid;
139 handle->lgh_ctxt = ctxt;
142 LLOG_CLIENT_EXIT(ctxt, imp);
143 ptlrpc_req_finished(req);
147 llog_free_handle(handle);
151 static int llog_client_destroy(struct llog_handle *loghandle)
153 struct obd_import *imp;
154 struct ptlrpc_request *req = NULL;
155 struct llogd_body *body;
159 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
160 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
162 LLOG_ORIGIN_HANDLE_DESTROY);
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_llh_flags = loghandle->lgh_hdr->llh_flags;
170 ptlrpc_request_set_replen(req);
171 rc = ptlrpc_queue_wait(req);
173 ptlrpc_req_finished(req);
175 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
180 static int llog_client_next_block(struct llog_handle *loghandle,
181 int *cur_idx, int next_idx,
182 __u64 *cur_offset, void *buf, int len)
184 struct obd_import *imp;
185 struct ptlrpc_request *req = NULL;
186 struct llogd_body *body;
191 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
192 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
194 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
196 GOTO(err_exit, rc =-ENOMEM);
198 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
199 body->lgd_logid = loghandle->lgh_id;
200 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
201 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
202 body->lgd_index = next_idx;
203 body->lgd_saved_index = *cur_idx;
205 body->lgd_cur_offset = *cur_offset;
207 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
208 ptlrpc_request_set_replen(req);
209 rc = ptlrpc_queue_wait(req);
213 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
215 GOTO(out, rc =-EFAULT);
217 /* The log records are swabbed as they are processed */
218 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
220 GOTO(out, rc =-EFAULT);
222 *cur_idx = body->lgd_saved_index;
223 *cur_offset = body->lgd_cur_offset;
225 memcpy(buf, ptr, len);
228 ptlrpc_req_finished(req);
230 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
234 static int llog_client_prev_block(struct llog_handle *loghandle,
235 int prev_idx, void *buf, int len)
237 struct obd_import *imp;
238 struct ptlrpc_request *req = NULL;
239 struct llogd_body *body;
244 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
245 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
247 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
249 GOTO(err_exit, rc = -ENOMEM);
251 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
252 body->lgd_logid = loghandle->lgh_id;
253 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
254 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
255 body->lgd_index = prev_idx;
258 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
259 ptlrpc_request_set_replen(req);
261 rc = ptlrpc_queue_wait(req);
265 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
267 GOTO(out, rc =-EFAULT);
269 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
271 GOTO(out, rc =-EFAULT);
273 memcpy(buf, ptr, len);
276 ptlrpc_req_finished(req);
278 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
282 static int llog_client_read_header(struct llog_handle *handle)
284 struct obd_import *imp;
285 struct ptlrpc_request *req = NULL;
286 struct llogd_body *body;
287 struct llog_log_hdr *hdr;
288 struct llog_rec_hdr *llh_hdr;
292 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
293 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
295 LLOG_ORIGIN_HANDLE_READ_HEADER);
297 GOTO(err_exit, rc = -ENOMEM);
299 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
300 body->lgd_logid = handle->lgh_id;
301 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
302 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
304 ptlrpc_request_set_replen(req);
305 rc = ptlrpc_queue_wait(req);
309 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
311 GOTO(out, rc =-EFAULT);
313 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
314 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.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 != LLOG_CHUNK_SIZE) {
323 CERROR("incorrectly sized log header: %#x "
325 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
326 CERROR("you may need to re-run lconf --write_conf.\n");
331 ptlrpc_req_finished(req);
333 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
337 static int llog_client_close(struct llog_handle *handle)
339 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
340 the servers all close the file at the end of every
346 struct llog_operations llog_client_ops = {
347 lop_next_block: llog_client_next_block,
348 lop_prev_block: llog_client_prev_block,
349 lop_read_header: llog_client_read_header,
350 lop_create: llog_client_create,
351 lop_destroy: llog_client_destroy,
352 lop_close: llog_client_close,