1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2004 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of the Lustre file system, http://www.lustre.org
8 * Lustre is a trademark of Cluster File Systems, Inc.
10 * You may have signed or agreed to another license before downloading
11 * this software. If so, you are bound by the terms and conditions
12 * of that agreement, and the following does not apply to you. See the
13 * LICENSE file included with this distribution for more information.
15 * If you did not agree to a different license, then this copy of Lustre
16 * is open source software; you can redistribute it and/or modify it
17 * under the terms of version 2 of the GNU General Public License as
18 * published by the Free Software Foundation.
20 * In either case, Lustre is distributed in the hope that it will be
21 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * license text for more details.
25 * remote api for llog - client side
29 #define DEBUG_SUBSYSTEM S_LOG
36 #include <libcfs/libcfs.h>
38 #include <liblustre.h>
41 #include <obd_class.h>
42 #include <lustre_log.h>
43 #include <lustre_net.h>
44 #include <libcfs/list.h>
46 /* This is a callback from the llog_* functions.
47 * Assumes caller has already pushed us into the kernel context. */
48 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
49 struct llog_logid *logid, char *name)
51 struct obd_import *imp;
52 struct llogd_body *body;
53 struct llog_handle *handle;
54 struct ptlrpc_request *req = NULL;
58 if (ctxt->loc_imp == NULL) {
59 /* This used to be an assert; bug 6200 */
60 CERROR("ctxt->loc_imp == NULL for context idx %d. Unable to "
61 "complete MDS/OSS recovery, but I'll try again next "
62 "time. Not fatal.\n", ctxt->loc_idx);
67 handle = llog_alloc_handle();
72 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
74 GOTO(err_free, rc = -ENOMEM);
77 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
80 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
81 LLOG_ORIGIN_HANDLE_CREATE);
83 ptlrpc_request_free(req);
86 ptlrpc_request_set_replen(req);
88 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
90 body->lgd_logid = *logid;
91 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
95 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
101 rc = ptlrpc_queue_wait(req);
105 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
107 GOTO(err_free, rc =-EFAULT);
109 handle->lgh_id = body->lgd_logid;
110 handle->lgh_ctxt = ctxt;
113 ptlrpc_req_finished(req);
116 llog_free_handle(handle);
120 static int llog_client_destroy(struct llog_handle *loghandle)
122 struct ptlrpc_request *req = NULL;
123 struct llogd_body *body;
127 req = ptlrpc_request_alloc_pack(loghandle->lgh_ctxt->loc_imp,
128 &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
130 LLOG_ORIGIN_HANDLE_DESTROY);
134 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
135 body->lgd_logid = loghandle->lgh_id;
136 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
138 ptlrpc_request_set_replen(req);
139 rc = ptlrpc_queue_wait(req);
141 ptlrpc_req_finished(req);
146 static int llog_client_next_block(struct llog_handle *loghandle,
147 int *cur_idx, int next_idx,
148 __u64 *cur_offset, void *buf, int len)
150 struct ptlrpc_request *req = NULL;
151 struct llogd_body *body;
156 req = ptlrpc_request_alloc_pack(loghandle->lgh_ctxt->loc_imp,
157 &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
159 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
163 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
164 body->lgd_logid = loghandle->lgh_id;
165 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
166 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
167 body->lgd_index = next_idx;
168 body->lgd_saved_index = *cur_idx;
170 body->lgd_cur_offset = *cur_offset;
172 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
173 ptlrpc_request_set_replen(req);
174 rc = ptlrpc_queue_wait(req);
178 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
180 GOTO(out, rc =-EFAULT);
182 /* The log records are swabbed as they are processed */
183 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
185 GOTO(out, rc =-EFAULT);
187 *cur_idx = body->lgd_saved_index;
188 *cur_offset = body->lgd_cur_offset;
190 memcpy(buf, ptr, len);
193 ptlrpc_req_finished(req);
197 static int llog_client_prev_block(struct llog_handle *loghandle,
198 int prev_idx, void *buf, int len)
200 struct ptlrpc_request *req = NULL;
201 struct llogd_body *body;
206 req = ptlrpc_request_alloc_pack(loghandle->lgh_ctxt->loc_imp,
207 &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
209 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
213 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
214 body->lgd_logid = loghandle->lgh_id;
215 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
216 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
217 body->lgd_index = prev_idx;
220 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
221 ptlrpc_request_set_replen(req);
223 rc = ptlrpc_queue_wait(req);
227 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
229 GOTO(out, rc =-EFAULT);
231 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
233 GOTO(out, rc =-EFAULT);
235 memcpy(buf, ptr, len);
238 ptlrpc_req_finished(req);
242 static int llog_client_read_header(struct llog_handle *handle)
244 struct ptlrpc_request *req = NULL;
245 struct llogd_body *body;
246 struct llog_log_hdr *hdr;
247 struct llog_rec_hdr *llh_hdr;
251 req = ptlrpc_request_alloc_pack(handle->lgh_ctxt->loc_imp,
252 &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
254 LLOG_ORIGIN_HANDLE_READ_HEADER);
258 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
259 body->lgd_logid = handle->lgh_id;
260 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
261 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
263 ptlrpc_request_set_replen(req);
264 rc = ptlrpc_queue_wait(req);
268 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
270 GOTO(out, rc =-EFAULT);
272 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
273 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
276 llh_hdr = &handle->lgh_hdr->llh_hdr;
277 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
278 CERROR("bad log header magic: %#x (expecting %#x)\n",
279 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
281 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
282 CERROR("incorrectly sized log header: %#x "
284 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
285 CERROR("you may need to re-run lconf --write_conf.\n");
290 ptlrpc_req_finished(req);
294 static int llog_client_close(struct llog_handle *handle)
296 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
297 the servers all close the file at the end of every
303 struct llog_operations llog_client_ops = {
304 lop_next_block: llog_client_next_block,
305 lop_prev_block: llog_client_prev_block,
306 lop_read_header: llog_client_read_header,
307 lop_create: llog_client_create,
308 lop_destroy: llog_client_destroy,
309 lop_close: llog_client_close,