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.
32 * Copyright (c) 2012, Whamcloud, Inc.
35 * This file is part of Lustre, http://www.lustre.org/
36 * Lustre is a trademark of Sun Microsystems, Inc.
38 * lustre/ptlrpc/llog_client.c
40 * remote api for llog - client side
42 * Author: Andreas Dilger <adilger@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_LOG
52 #include <libcfs/libcfs.h>
54 #include <liblustre.h>
57 #include <obd_class.h>
58 #include <lustre_log.h>
59 #include <lustre_net.h>
60 #include <libcfs/list.h>
62 #define LLOG_CLIENT_ENTRY(ctxt, imp) do { \
63 cfs_mutex_down(&ctxt->loc_sem); \
64 if (ctxt->loc_imp) { \
65 imp = class_import_get(ctxt->loc_imp); \
67 CERROR("ctxt->loc_imp == NULL for context idx %d." \
68 "Unable to complete MDS/OSS recovery," \
69 "but I'll try again next time. Not fatal.\n", \
72 cfs_mutex_up(&ctxt->loc_sem); \
75 cfs_mutex_up(&ctxt->loc_sem); \
78 #define LLOG_CLIENT_EXIT(ctxt, imp) do { \
79 cfs_mutex_down(&ctxt->loc_sem); \
80 if (ctxt->loc_imp != imp) \
81 CWARN("loc_imp has changed from %p to %p\n", \
82 ctxt->loc_imp, imp); \
83 class_import_put(imp); \
84 cfs_mutex_up(&ctxt->loc_sem); \
87 /* This is a callback from the llog_* functions.
88 * Assumes caller has already pushed us into the kernel context. */
89 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
90 struct llog_logid *logid, char *name)
92 struct obd_import *imp;
93 struct llogd_body *body;
94 struct llog_handle *handle;
95 struct ptlrpc_request *req = NULL;
99 LLOG_CLIENT_ENTRY(ctxt, imp);
101 handle = llog_alloc_handle();
106 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
108 GOTO(err_free, rc = -ENOMEM);
111 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
114 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
115 LLOG_ORIGIN_HANDLE_CREATE);
117 ptlrpc_request_free(req);
121 ptlrpc_request_set_replen(req);
123 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
125 body->lgd_logid = *logid;
126 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
130 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
136 rc = ptlrpc_queue_wait(req);
140 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
142 GOTO(err_free, rc =-EFAULT);
144 handle->lgh_id = body->lgd_logid;
145 handle->lgh_ctxt = ctxt;
148 LLOG_CLIENT_EXIT(ctxt, imp);
149 ptlrpc_req_finished(req);
153 llog_free_handle(handle);
157 static int llog_client_destroy(struct llog_handle *loghandle)
159 struct obd_import *imp;
160 struct ptlrpc_request *req = NULL;
161 struct llogd_body *body;
165 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
166 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
168 LLOG_ORIGIN_HANDLE_DESTROY);
170 GOTO(err_exit, rc =-ENOMEM);
172 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
173 body->lgd_logid = loghandle->lgh_id;
174 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
176 ptlrpc_request_set_replen(req);
177 rc = ptlrpc_queue_wait(req);
179 ptlrpc_req_finished(req);
181 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
186 static int llog_client_next_block(struct llog_handle *loghandle,
187 int *cur_idx, int next_idx,
188 __u64 *cur_offset, void *buf, int len)
190 struct obd_import *imp;
191 struct ptlrpc_request *req = NULL;
192 struct llogd_body *body;
197 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
198 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
200 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
202 GOTO(err_exit, rc =-ENOMEM);
204 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
205 body->lgd_logid = loghandle->lgh_id;
206 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
207 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
208 body->lgd_index = next_idx;
209 body->lgd_saved_index = *cur_idx;
211 body->lgd_cur_offset = *cur_offset;
213 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
214 ptlrpc_request_set_replen(req);
215 rc = ptlrpc_queue_wait(req);
219 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
221 GOTO(out, rc =-EFAULT);
223 /* The log records are swabbed as they are processed */
224 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
226 GOTO(out, rc =-EFAULT);
228 *cur_idx = body->lgd_saved_index;
229 *cur_offset = body->lgd_cur_offset;
231 memcpy(buf, ptr, len);
234 ptlrpc_req_finished(req);
236 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
240 static int llog_client_prev_block(struct llog_handle *loghandle,
241 int prev_idx, void *buf, int len)
243 struct obd_import *imp;
244 struct ptlrpc_request *req = NULL;
245 struct llogd_body *body;
250 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
251 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
253 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
255 GOTO(err_exit, rc = -ENOMEM);
257 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
258 body->lgd_logid = loghandle->lgh_id;
259 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
260 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
261 body->lgd_index = prev_idx;
264 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
265 ptlrpc_request_set_replen(req);
267 rc = ptlrpc_queue_wait(req);
271 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
273 GOTO(out, rc =-EFAULT);
275 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
277 GOTO(out, rc =-EFAULT);
279 memcpy(buf, ptr, len);
282 ptlrpc_req_finished(req);
284 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
288 static int llog_client_read_header(struct llog_handle *handle)
290 struct obd_import *imp;
291 struct ptlrpc_request *req = NULL;
292 struct llogd_body *body;
293 struct llog_log_hdr *hdr;
294 struct llog_rec_hdr *llh_hdr;
298 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
299 req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
301 LLOG_ORIGIN_HANDLE_READ_HEADER);
303 GOTO(err_exit, rc = -ENOMEM);
305 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
306 body->lgd_logid = handle->lgh_id;
307 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
308 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
310 ptlrpc_request_set_replen(req);
311 rc = ptlrpc_queue_wait(req);
315 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
317 GOTO(out, rc =-EFAULT);
319 memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
320 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
323 llh_hdr = &handle->lgh_hdr->llh_hdr;
324 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
325 CERROR("bad log header magic: %#x (expecting %#x)\n",
326 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
328 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
329 CERROR("incorrectly sized log header: %#x "
331 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
332 CERROR("you may need to re-run lconf --write_conf.\n");
337 ptlrpc_req_finished(req);
339 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
343 static int llog_client_close(struct llog_handle *handle)
345 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
346 the servers all close the file at the end of every
352 struct llog_operations llog_client_ops = {
353 lop_next_block: llog_client_next_block,
354 lop_prev_block: llog_client_prev_block,
355 lop_read_header: llog_client_read_header,
356 lop_create: llog_client_create,
357 lop_destroy: llog_client_destroy,
358 lop_close: llog_client_close,