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\n", \
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);
119 ptlrpc_request_set_replen(req);
121 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
123 body->lgd_logid = *logid;
124 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
128 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
134 rc = ptlrpc_queue_wait(req);
138 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
140 GOTO(err_free, rc =-EFAULT);
142 handle->lgh_id = body->lgd_logid;
143 handle->lgh_ctxt = ctxt;
146 LLOG_CLIENT_EXIT(ctxt, imp);
147 ptlrpc_req_finished(req);
150 llog_free_handle(handle);
154 static int llog_client_destroy(struct llog_handle *loghandle)
156 struct obd_import *imp;
157 struct ptlrpc_request *req = NULL;
158 struct llogd_body *body;
162 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
163 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
165 LLOG_ORIGIN_HANDLE_DESTROY);
167 GOTO(err_exit, rc =-ENOMEM);
169 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
170 body->lgd_logid = loghandle->lgh_id;
171 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
173 ptlrpc_request_set_replen(req);
174 rc = ptlrpc_queue_wait(req);
176 ptlrpc_req_finished(req);
178 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
183 static int llog_client_next_block(struct llog_handle *loghandle,
184 int *cur_idx, int next_idx,
185 __u64 *cur_offset, void *buf, int len)
187 struct obd_import *imp;
188 struct ptlrpc_request *req = NULL;
189 struct llogd_body *body;
194 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
195 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
197 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
199 GOTO(err_exit, rc =-ENOMEM);
201 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
202 body->lgd_logid = loghandle->lgh_id;
203 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
204 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
205 body->lgd_index = next_idx;
206 body->lgd_saved_index = *cur_idx;
208 body->lgd_cur_offset = *cur_offset;
210 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
211 ptlrpc_request_set_replen(req);
212 rc = ptlrpc_queue_wait(req);
216 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
218 GOTO(out, rc =-EFAULT);
220 /* The log records are swabbed as they are processed */
221 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
223 GOTO(out, rc =-EFAULT);
225 *cur_idx = body->lgd_saved_index;
226 *cur_offset = body->lgd_cur_offset;
228 memcpy(buf, ptr, len);
231 ptlrpc_req_finished(req);
233 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
237 static int llog_client_prev_block(struct llog_handle *loghandle,
238 int prev_idx, void *buf, int len)
240 struct obd_import *imp;
241 struct ptlrpc_request *req = NULL;
242 struct llogd_body *body;
247 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
248 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
250 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
252 GOTO(err_exit, rc = -ENOMEM);
254 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
255 body->lgd_logid = loghandle->lgh_id;
256 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
257 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
258 body->lgd_index = prev_idx;
261 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
262 ptlrpc_request_set_replen(req);
264 rc = ptlrpc_queue_wait(req);
268 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
270 GOTO(out, rc =-EFAULT);
272 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
274 GOTO(out, rc =-EFAULT);
276 memcpy(buf, ptr, len);
279 ptlrpc_req_finished(req);
281 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
285 static int llog_client_read_header(struct llog_handle *handle)
287 struct obd_import *imp;
288 struct ptlrpc_request *req = NULL;
289 struct llogd_body *body;
290 struct llog_log_hdr *hdr;
291 struct llog_rec_hdr *llh_hdr;
295 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
296 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
298 LLOG_ORIGIN_HANDLE_READ_HEADER);
300 GOTO(err_exit, rc = -ENOMEM);
302 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
303 body->lgd_logid = handle->lgh_id;
304 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
305 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
307 ptlrpc_request_set_replen(req);
308 rc = ptlrpc_queue_wait(req);
312 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
314 GOTO(out, rc =-EFAULT);
316 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
317 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
320 llh_hdr = &handle->lgh_hdr->llh_hdr;
321 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
322 CERROR("bad log header magic: %#x (expecting %#x)\n",
323 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
325 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
326 CERROR("incorrectly sized log header: %#x "
328 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
329 CERROR("you may need to re-run lconf --write_conf.\n");
334 ptlrpc_req_finished(req);
336 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
340 static int llog_client_close(struct llog_handle *handle)
342 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
343 the servers all close the file at the end of every
349 struct llog_operations llog_client_ops = {
350 lop_next_block: llog_client_next_block,
351 lop_prev_block: llog_client_prev_block,
352 lop_read_header: llog_client_read_header,
353 lop_create: llog_client_create,
354 lop_destroy: llog_client_destroy,
355 lop_close: llog_client_close,