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 (c) 2003, 2010, Oracle and/or its affiliates. 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 cfs_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 cfs_mutex_up(&ctxt->loc_sem); \
73 cfs_mutex_up(&ctxt->loc_sem); \
76 #define LLOG_CLIENT_EXIT(ctxt, imp) do { \
77 cfs_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 cfs_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);
151 llog_free_handle(handle);
155 static int llog_client_destroy(struct llog_handle *loghandle)
157 struct obd_import *imp;
158 struct ptlrpc_request *req = NULL;
159 struct llogd_body *body;
163 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
164 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
166 LLOG_ORIGIN_HANDLE_DESTROY);
168 GOTO(err_exit, rc =-ENOMEM);
170 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
171 body->lgd_logid = loghandle->lgh_id;
172 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
174 ptlrpc_request_set_replen(req);
175 rc = ptlrpc_queue_wait(req);
177 ptlrpc_req_finished(req);
179 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
184 static int llog_client_next_block(struct llog_handle *loghandle,
185 int *cur_idx, int next_idx,
186 __u64 *cur_offset, void *buf, int len)
188 struct obd_import *imp;
189 struct ptlrpc_request *req = NULL;
190 struct llogd_body *body;
195 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
196 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
198 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
200 GOTO(err_exit, rc =-ENOMEM);
202 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
203 body->lgd_logid = loghandle->lgh_id;
204 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
205 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
206 body->lgd_index = next_idx;
207 body->lgd_saved_index = *cur_idx;
209 body->lgd_cur_offset = *cur_offset;
211 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
212 ptlrpc_request_set_replen(req);
213 rc = ptlrpc_queue_wait(req);
217 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
219 GOTO(out, rc =-EFAULT);
221 /* The log records are swabbed as they are processed */
222 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
224 GOTO(out, rc =-EFAULT);
226 *cur_idx = body->lgd_saved_index;
227 *cur_offset = body->lgd_cur_offset;
229 memcpy(buf, ptr, len);
232 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;
248 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
249 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
251 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
253 GOTO(err_exit, rc = -ENOMEM);
255 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_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 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
263 ptlrpc_request_set_replen(req);
265 rc = ptlrpc_queue_wait(req);
269 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
271 GOTO(out, rc =-EFAULT);
273 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
275 GOTO(out, rc =-EFAULT);
277 memcpy(buf, ptr, len);
280 ptlrpc_req_finished(req);
282 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
286 static int llog_client_read_header(struct llog_handle *handle)
288 struct obd_import *imp;
289 struct ptlrpc_request *req = NULL;
290 struct llogd_body *body;
291 struct llog_log_hdr *hdr;
292 struct llog_rec_hdr *llh_hdr;
296 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
297 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
299 LLOG_ORIGIN_HANDLE_READ_HEADER);
301 GOTO(err_exit, rc = -ENOMEM);
303 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
304 body->lgd_logid = handle->lgh_id;
305 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
306 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
308 ptlrpc_request_set_replen(req);
309 rc = ptlrpc_queue_wait(req);
313 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
315 GOTO(out, rc =-EFAULT);
317 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
318 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
321 llh_hdr = &handle->lgh_hdr->llh_hdr;
322 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
323 CERROR("bad log header magic: %#x (expecting %#x)\n",
324 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
326 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
327 CERROR("incorrectly sized log header: %#x "
329 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
330 CERROR("you may need to re-run lconf --write_conf.\n");
335 ptlrpc_req_finished(req);
337 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
341 static int llog_client_close(struct llog_handle *handle)
343 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
344 the servers all close the file at the end of every
350 struct llog_operations llog_client_ops = {
351 lop_next_block: llog_client_next_block,
352 lop_prev_block: llog_client_prev_block,
353 lop_read_header: llog_client_read_header,
354 lop_create: llog_client_create,
355 lop_destroy: llog_client_destroy,
356 lop_close: llog_client_close,