1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
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
50 #include <libcfs/libcfs.h>
52 #include <liblustre.h>
55 #include <obd_class.h>
56 #include <lustre_log.h>
57 #include <lustre_net.h>
58 #include <libcfs/list.h>
60 #define LLOG_CLIENT_ENTRY(ctxt, imp) do { \
61 mutex_down(&ctxt->loc_sem); \
62 if (ctxt->loc_imp) { \
63 imp = class_import_get(ctxt->loc_imp); \
65 CERROR("ctxt->loc_imp == NULL for context idx %d." \
66 "Unable to complete MDS/OSS recovery," \
67 "but I'll try again next time. Not fatal.\n", \
70 mutex_up(&ctxt->loc_sem); \
73 mutex_up(&ctxt->loc_sem); \
76 #define LLOG_CLIENT_EXIT(ctxt, imp) do { \
77 mutex_down(&ctxt->loc_sem); \
78 if (ctxt->loc_imp != imp) \
79 CWARN("loc_imp has changed from %p to %p", \
80 ctxt->loc_imp, imp); \
81 class_import_put(imp); \
82 mutex_up(&ctxt->loc_sem); \
85 /* This is a callback from the llog_* functions.
86 * Assumes caller has already pushed us into the kernel context. */
87 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
88 struct llog_logid *logid, char *name)
90 struct obd_import *imp;
91 struct llogd_body req_body;
92 struct llogd_body *body;
93 struct llog_handle *handle;
94 struct ptlrpc_request *req = NULL;
95 __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(req_body) };
96 char *bufs[3] = { NULL, (char*)&req_body };
101 LLOG_CLIENT_ENTRY(ctxt, imp);
103 handle = llog_alloc_handle();
105 GOTO(out, rc = -ENOMEM);
108 memset(&req_body, 0, sizeof(req_body));
110 req_body.lgd_logid = *logid;
111 req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
114 size[bufcount] = strlen(name) + 1;
115 bufs[bufcount] = name;
119 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
120 LLOG_ORIGIN_HANDLE_CREATE, bufcount, size, bufs);
122 GOTO(err_free, rc = -ENOMEM);
124 ptlrpc_req_set_repsize(req, 2, size);
125 rc = ptlrpc_queue_wait(req);
129 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
130 lustre_swab_llogd_body);
132 CERROR ("Can't unpack llogd_body\n");
133 GOTO(err_free, rc =-EFAULT);
136 handle->lgh_id = body->lgd_logid;
137 handle->lgh_ctxt = ctxt;
141 ptlrpc_req_finished(req);
142 LLOG_CLIENT_EXIT(ctxt, imp);
146 llog_free_handle(handle);
150 static int llog_client_destroy(struct llog_handle *loghandle)
152 struct obd_import *imp;
153 struct ptlrpc_request *req = NULL;
154 struct llogd_body *body;
155 __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
159 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
160 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
161 LLOG_ORIGIN_HANDLE_DESTROY, 2, size, NULL);
163 GOTO(out, rc = -ENOMEM);
165 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
166 body->lgd_logid = loghandle->lgh_id;
167 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
169 ptlrpc_req_set_repsize(req, 2, size);
170 rc = ptlrpc_queue_wait(req);
172 ptlrpc_req_finished(req);
174 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
179 static int llog_client_next_block(struct llog_handle *loghandle,
180 int *cur_idx, int next_idx,
181 __u64 *cur_offset, void *buf, int len)
183 struct obd_import *imp;
184 struct ptlrpc_request *req = NULL;
185 struct llogd_body *body;
187 __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
191 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
192 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
193 LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 2, size, NULL);
195 GOTO(out, rc =-ENOMEM);
197 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
198 body->lgd_logid = loghandle->lgh_id;
199 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
200 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
201 body->lgd_index = next_idx;
202 body->lgd_saved_index = *cur_idx;
204 body->lgd_cur_offset = *cur_offset;
206 size[REPLY_REC_OFF + 1] = len;
207 ptlrpc_req_set_repsize(req, 3, size);
208 rc = ptlrpc_queue_wait(req);
212 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
213 lustre_swab_llogd_body);
215 CERROR ("Can't unpack llogd_body\n");
216 GOTO(out, rc =-EFAULT);
219 /* The log records are swabbed as they are processed */
220 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
222 CERROR ("Can't unpack bitmap\n");
223 GOTO(out, rc =-EFAULT);
226 *cur_idx = body->lgd_saved_index;
227 *cur_offset = body->lgd_cur_offset;
229 memcpy(buf, ptr, len);
233 ptlrpc_req_finished(req);
234 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
238 static int llog_client_prev_block(struct llog_handle *loghandle,
239 int prev_idx, void *buf, int len)
241 struct obd_import *imp;
242 struct ptlrpc_request *req = NULL;
243 struct llogd_body *body;
245 __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
249 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
250 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
251 LLOG_ORIGIN_HANDLE_PREV_BLOCK, 2, size, NULL);
253 GOTO(out, rc = -ENOMEM);
255 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
256 body->lgd_logid = loghandle->lgh_id;
257 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
258 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
259 body->lgd_index = prev_idx;
262 size[REPLY_REC_OFF + 1] = len;
263 ptlrpc_req_set_repsize(req, 3, size);
264 rc = ptlrpc_queue_wait(req);
268 body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
269 lustre_swab_llogd_body);
271 CERROR ("Can't unpack llogd_body\n");
272 GOTO(out, rc =-EFAULT);
275 ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
277 CERROR ("Can't unpack bitmap\n");
278 GOTO(out, rc =-EFAULT);
281 memcpy(buf, ptr, len);
285 ptlrpc_req_finished(req);
286 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
290 static int llog_client_read_header(struct llog_handle *handle)
292 struct obd_import *imp;
293 struct ptlrpc_request *req = NULL;
294 struct llogd_body *body;
295 struct llog_log_hdr *hdr;
296 struct llog_rec_hdr *llh_hdr;
297 __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
298 __u32 repsize[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
302 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
303 req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
304 LLOG_ORIGIN_HANDLE_READ_HEADER, 2, size, NULL);
306 GOTO(out, rc = -ENOMEM);
308 body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
309 body->lgd_logid = handle->lgh_id;
310 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
311 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
313 ptlrpc_req_set_repsize(req, 2, repsize);
314 rc = ptlrpc_queue_wait(req);
318 hdr = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*hdr),
319 lustre_swab_llog_hdr);
321 CERROR ("Can't unpack llog_hdr\n");
322 GOTO(out, rc =-EFAULT);
325 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
326 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
329 llh_hdr = &handle->lgh_hdr->llh_hdr;
330 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
331 CERROR("bad log header magic: %#x (expecting %#x)\n",
332 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
334 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
335 CERROR("incorrectly sized log header: %#x "
337 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
338 CERROR("you may need to re-run lconf --write_conf.\n");
344 ptlrpc_req_finished(req);
345 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
349 static int llog_client_close(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 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_create: llog_client_create,
363 lop_destroy: llog_client_destroy,
364 lop_close: llog_client_close,