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 req_body;
53 struct llogd_body *body;
54 struct llog_handle *handle;
55 struct ptlrpc_request *req = NULL;
56 int size[3] = { sizeof(struct ptlrpc_body), sizeof(req_body) };
57 char *bufs[3] = { NULL, (char*)&req_body };
62 if (ctxt->loc_imp == NULL) {
63 /* This used to be an assert; bug 6200 */
64 CERROR("ctxt->loc_imp == NULL for context idx %d. Unable to "
65 "complete MDS/OSS recovery, but I'll try again next "
66 "time. Not fatal.\n", ctxt->loc_idx);
71 handle = llog_alloc_handle();
76 memset(&req_body, 0, sizeof(req_body));
78 req_body.lgd_logid = *logid;
79 req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
82 size[bufcount] = strlen(name) + 1;
83 bufs[bufcount] = name;
87 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
88 LLOG_ORIGIN_HANDLE_CREATE, bufcount, size, bufs);
90 GOTO(err_free, rc = -ENOMEM);
92 ptlrpc_req_set_repsize(req, 2, size);
93 rc = ptlrpc_queue_wait(req);
97 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
98 lustre_swab_llogd_body);
100 CERROR ("Can't unpack llogd_body\n");
101 GOTO(err_free, rc =-EFAULT);
104 handle->lgh_id = body->lgd_logid;
105 handle->lgh_ctxt = ctxt;
109 ptlrpc_req_finished(req);
113 llog_free_handle(handle);
117 static int llog_client_destroy(struct llog_handle *loghandle)
119 struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
120 struct ptlrpc_request *req = NULL;
121 struct llogd_body *body;
122 int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
126 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
127 LLOG_ORIGIN_HANDLE_DESTROY, 2, size, NULL);
131 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
132 body->lgd_logid = loghandle->lgh_id;
133 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
135 ptlrpc_req_set_repsize(req, 2, size);
136 rc = ptlrpc_queue_wait(req);
138 ptlrpc_req_finished(req);
143 static int llog_client_next_block(struct llog_handle *loghandle,
144 int *cur_idx, int next_idx,
145 __u64 *cur_offset, void *buf, int len)
147 struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
148 struct ptlrpc_request *req = NULL;
149 struct llogd_body *body;
151 int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
155 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
156 LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 2, size, NULL);
158 GOTO(out, rc = -ENOMEM);
160 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
161 body->lgd_logid = loghandle->lgh_id;
162 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
163 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
164 body->lgd_index = next_idx;
165 body->lgd_saved_index = *cur_idx;
167 body->lgd_cur_offset = *cur_offset;
169 size[REPLY_REC_OFF + 1] = len;
170 ptlrpc_req_set_repsize(req, 3, size);
171 rc = ptlrpc_queue_wait(req);
175 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
176 lustre_swab_llogd_body);
178 CERROR ("Can't unpack llogd_body\n");
179 GOTO(out, rc =-EFAULT);
182 /* The log records are swabbed as they are processed */
183 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
185 CERROR ("Can't unpack bitmap\n");
186 GOTO(out, rc =-EFAULT);
189 *cur_idx = body->lgd_saved_index;
190 *cur_offset = body->lgd_cur_offset;
192 memcpy(buf, ptr, len);
196 ptlrpc_req_finished(req);
200 static int llog_client_prev_block(struct llog_handle *loghandle,
201 int prev_idx, void *buf, int len)
203 struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
204 struct ptlrpc_request *req = NULL;
205 struct llogd_body *body;
207 int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
211 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
212 LLOG_ORIGIN_HANDLE_PREV_BLOCK, 2, size, NULL);
214 GOTO(out, rc = -ENOMEM);
216 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
217 body->lgd_logid = loghandle->lgh_id;
218 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
219 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
220 body->lgd_index = prev_idx;
223 size[REPLY_REC_OFF + 1] = len;
224 ptlrpc_req_set_repsize(req, 3, size);
225 rc = ptlrpc_queue_wait(req);
229 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
230 lustre_swab_llogd_body);
232 CERROR ("Can't unpack llogd_body\n");
233 GOTO(out, rc =-EFAULT);
236 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
238 CERROR ("Can't unpack bitmap\n");
239 GOTO(out, rc =-EFAULT);
242 memcpy(buf, ptr, len);
246 ptlrpc_req_finished(req);
250 static int llog_client_read_header(struct llog_handle *handle)
252 struct obd_import *imp = handle->lgh_ctxt->loc_imp;
253 struct ptlrpc_request *req = NULL;
254 struct llogd_body *body;
255 struct llog_log_hdr *hdr;
256 struct llog_rec_hdr *llh_hdr;
257 int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
258 int repsize[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
262 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
263 LLOG_ORIGIN_HANDLE_READ_HEADER, 2, size, NULL);
265 GOTO(out, rc = -ENOMEM);
267 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
268 body->lgd_logid = handle->lgh_id;
269 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
270 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
272 ptlrpc_req_set_repsize(req, 2, repsize);
273 rc = ptlrpc_queue_wait(req);
277 hdr = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*hdr),
278 lustre_swab_llog_hdr);
280 CERROR ("Can't unpack llog_hdr\n");
281 GOTO(out, rc =-EFAULT);
284 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
285 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
288 llh_hdr = &handle->lgh_hdr->llh_hdr;
289 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
290 CERROR("bad log header magic: %#x (expecting %#x)\n",
291 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
293 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
294 CERROR("incorrectly sized log header: %#x "
296 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
297 CERROR("you may need to re-run lconf --write_conf.\n");
303 ptlrpc_req_finished(req);
307 static int llog_client_close(struct llog_handle *handle)
309 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
310 the servers all close the file at the end of every
316 struct llog_operations llog_client_ops = {
317 lop_next_block: llog_client_next_block,
318 lop_prev_block: llog_client_prev_block,
319 lop_read_header: llog_client_read_header,
320 lop_create: llog_client_create,
321 lop_destroy: llog_client_destroy,
322 lop_close: llog_client_close,