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 *body;
92 struct llog_handle *handle;
93 struct ptlrpc_request *req = NULL;
97 LLOG_CLIENT_ENTRY(ctxt, imp);
99 handle = llog_alloc_handle();
104 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
106 GOTO(err_free, rc = -ENOMEM);
109 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
112 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
113 LLOG_ORIGIN_HANDLE_CREATE);
115 ptlrpc_request_free(req);
118 ptlrpc_request_set_replen(req);
120 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
122 body->lgd_logid = *logid;
123 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
127 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
133 rc = ptlrpc_queue_wait(req);
137 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
139 GOTO(err_free, rc =-EFAULT);
141 handle->lgh_id = body->lgd_logid;
142 handle->lgh_ctxt = ctxt;
145 LLOG_CLIENT_EXIT(ctxt, imp);
146 ptlrpc_req_finished(req);
149 llog_free_handle(handle);
153 static int llog_client_destroy(struct llog_handle *loghandle)
155 struct obd_import *imp;
156 struct ptlrpc_request *req = NULL;
157 struct llogd_body *body;
161 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
162 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
164 LLOG_ORIGIN_HANDLE_DESTROY);
166 GOTO(err_exit, rc =-ENOMEM);
168 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
169 body->lgd_logid = loghandle->lgh_id;
170 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
172 ptlrpc_request_set_replen(req);
173 rc = ptlrpc_queue_wait(req);
175 ptlrpc_req_finished(req);
177 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
182 static int llog_client_next_block(struct llog_handle *loghandle,
183 int *cur_idx, int next_idx,
184 __u64 *cur_offset, void *buf, int len)
186 struct obd_import *imp;
187 struct ptlrpc_request *req = NULL;
188 struct llogd_body *body;
193 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
194 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
196 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
198 GOTO(err_exit, rc =-ENOMEM);
200 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
201 body->lgd_logid = loghandle->lgh_id;
202 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
203 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
204 body->lgd_index = next_idx;
205 body->lgd_saved_index = *cur_idx;
207 body->lgd_cur_offset = *cur_offset;
209 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
210 ptlrpc_request_set_replen(req);
211 rc = ptlrpc_queue_wait(req);
215 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
217 GOTO(out, rc =-EFAULT);
219 /* The log records are swabbed as they are processed */
220 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
222 GOTO(out, rc =-EFAULT);
224 *cur_idx = body->lgd_saved_index;
225 *cur_offset = body->lgd_cur_offset;
227 memcpy(buf, ptr, len);
230 ptlrpc_req_finished(req);
232 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
236 static int llog_client_prev_block(struct llog_handle *loghandle,
237 int prev_idx, void *buf, int len)
239 struct obd_import *imp;
240 struct ptlrpc_request *req = NULL;
241 struct llogd_body *body;
246 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
247 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
249 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
251 GOTO(err_exit, rc = -ENOMEM);
253 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
254 body->lgd_logid = loghandle->lgh_id;
255 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
256 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
257 body->lgd_index = prev_idx;
260 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
261 ptlrpc_request_set_replen(req);
263 rc = ptlrpc_queue_wait(req);
267 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
269 GOTO(out, rc =-EFAULT);
271 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
273 GOTO(out, rc =-EFAULT);
275 memcpy(buf, ptr, len);
278 ptlrpc_req_finished(req);
280 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
284 static int llog_client_read_header(struct llog_handle *handle)
286 struct obd_import *imp;
287 struct ptlrpc_request *req = NULL;
288 struct llogd_body *body;
289 struct llog_log_hdr *hdr;
290 struct llog_rec_hdr *llh_hdr;
294 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
295 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
297 LLOG_ORIGIN_HANDLE_READ_HEADER);
299 GOTO(err_exit, rc = -ENOMEM);
301 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
302 body->lgd_logid = handle->lgh_id;
303 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
304 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
306 ptlrpc_request_set_replen(req);
307 rc = ptlrpc_queue_wait(req);
311 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
313 GOTO(out, rc =-EFAULT);
315 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
316 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
319 llh_hdr = &handle->lgh_hdr->llh_hdr;
320 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
321 CERROR("bad log header magic: %#x (expecting %#x)\n",
322 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
324 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
325 CERROR("incorrectly sized log header: %#x "
327 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
328 CERROR("you may need to re-run lconf --write_conf.\n");
333 ptlrpc_req_finished(req);
335 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
339 static int llog_client_close(struct llog_handle *handle)
341 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
342 the servers all close the file at the end of every
348 struct llog_operations llog_client_ops = {
349 lop_next_block: llog_client_next_block,
350 lop_prev_block: llog_client_prev_block,
351 lop_read_header: llog_client_read_header,
352 lop_create: llog_client_create,
353 lop_destroy: llog_client_destroy,
354 lop_close: llog_client_close,