4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2015, Intel Corporation.
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
45 #include "../../include/linux/libcfs/libcfs.h"
47 #include "../include/obd_class.h"
48 #include "../include/lustre_log.h"
49 #include "../include/lustre_net.h"
50 #include <linux/list.h>
52 #define LLOG_CLIENT_ENTRY(ctxt, imp) do { \
53 mutex_lock(&ctxt->loc_mutex); \
54 if (ctxt->loc_imp) { \
55 imp = class_import_get(ctxt->loc_imp); \
57 CERROR("ctxt->loc_imp == NULL for context idx %d." \
58 "Unable to complete MDS/OSS recovery," \
59 "but I'll try again next time. Not fatal.\n", \
62 mutex_unlock(&ctxt->loc_mutex); \
65 mutex_unlock(&ctxt->loc_mutex); \
68 #define LLOG_CLIENT_EXIT(ctxt, imp) do { \
69 mutex_lock(&ctxt->loc_mutex); \
70 if (ctxt->loc_imp != imp) \
71 CWARN("loc_imp has changed from %p to %p\n", \
72 ctxt->loc_imp, imp); \
73 class_import_put(imp); \
74 mutex_unlock(&ctxt->loc_mutex); \
77 /* This is a callback from the llog_* functions.
78 * Assumes caller has already pushed us into the kernel context.
80 static int llog_client_open(const struct lu_env
*env
,
81 struct llog_handle
*lgh
, struct llog_logid
*logid
,
82 char *name
, enum llog_open_param open_param
)
84 struct obd_import
*imp
;
85 struct llogd_body
*body
;
86 struct llog_ctxt
*ctxt
= lgh
->lgh_ctxt
;
87 struct ptlrpc_request
*req
= NULL
;
90 LLOG_CLIENT_ENTRY(ctxt
, imp
);
92 /* client cannot create llog */
93 LASSERTF(open_param
!= LLOG_OPEN_NEW
, "%#x\n", open_param
);
96 req
= ptlrpc_request_alloc(imp
, &RQF_LLOG_ORIGIN_HANDLE_CREATE
);
103 req_capsule_set_size(&req
->rq_pill
, &RMF_NAME
, RCL_CLIENT
,
106 rc
= ptlrpc_request_pack(req
, LUSTRE_LOG_VERSION
,
107 LLOG_ORIGIN_HANDLE_CREATE
);
109 ptlrpc_request_free(req
);
113 ptlrpc_request_set_replen(req
);
115 body
= req_capsule_client_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
117 body
->lgd_logid
= *logid
;
118 body
->lgd_ctxt_idx
= ctxt
->loc_idx
- 1;
123 tmp
= req_capsule_client_sized_get(&req
->rq_pill
, &RMF_NAME
,
129 rc
= ptlrpc_queue_wait(req
);
133 body
= req_capsule_server_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
139 lgh
->lgh_id
= body
->lgd_logid
;
140 lgh
->lgh_ctxt
= ctxt
;
142 LLOG_CLIENT_EXIT(ctxt
, imp
);
143 ptlrpc_req_finished(req
);
147 static int llog_client_next_block(const struct lu_env
*env
,
148 struct llog_handle
*loghandle
,
149 int *cur_idx
, int next_idx
,
150 __u64
*cur_offset
, void *buf
, int len
)
152 struct obd_import
*imp
;
153 struct ptlrpc_request
*req
= NULL
;
154 struct llogd_body
*body
;
158 LLOG_CLIENT_ENTRY(loghandle
->lgh_ctxt
, imp
);
159 req
= ptlrpc_request_alloc_pack(imp
, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK
,
161 LLOG_ORIGIN_HANDLE_NEXT_BLOCK
);
167 body
= req_capsule_client_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
168 body
->lgd_logid
= loghandle
->lgh_id
;
169 body
->lgd_ctxt_idx
= loghandle
->lgh_ctxt
->loc_idx
- 1;
170 body
->lgd_llh_flags
= loghandle
->lgh_hdr
->llh_flags
;
171 body
->lgd_index
= next_idx
;
172 body
->lgd_saved_index
= *cur_idx
;
174 body
->lgd_cur_offset
= *cur_offset
;
176 req_capsule_set_size(&req
->rq_pill
, &RMF_EADATA
, RCL_SERVER
, len
);
177 ptlrpc_request_set_replen(req
);
178 rc
= ptlrpc_queue_wait(req
);
182 body
= req_capsule_server_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
188 /* The log records are swabbed as they are processed */
189 ptr
= req_capsule_server_get(&req
->rq_pill
, &RMF_EADATA
);
195 *cur_idx
= body
->lgd_saved_index
;
196 *cur_offset
= body
->lgd_cur_offset
;
198 memcpy(buf
, ptr
, len
);
200 ptlrpc_req_finished(req
);
202 LLOG_CLIENT_EXIT(loghandle
->lgh_ctxt
, imp
);
206 static int llog_client_prev_block(const struct lu_env
*env
,
207 struct llog_handle
*loghandle
,
208 int prev_idx
, void *buf
, int len
)
210 struct obd_import
*imp
;
211 struct ptlrpc_request
*req
= NULL
;
212 struct llogd_body
*body
;
216 LLOG_CLIENT_ENTRY(loghandle
->lgh_ctxt
, imp
);
217 req
= ptlrpc_request_alloc_pack(imp
, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK
,
219 LLOG_ORIGIN_HANDLE_PREV_BLOCK
);
225 body
= req_capsule_client_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
226 body
->lgd_logid
= loghandle
->lgh_id
;
227 body
->lgd_ctxt_idx
= loghandle
->lgh_ctxt
->loc_idx
- 1;
228 body
->lgd_llh_flags
= loghandle
->lgh_hdr
->llh_flags
;
229 body
->lgd_index
= prev_idx
;
232 req_capsule_set_size(&req
->rq_pill
, &RMF_EADATA
, RCL_SERVER
, len
);
233 ptlrpc_request_set_replen(req
);
235 rc
= ptlrpc_queue_wait(req
);
239 body
= req_capsule_server_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
245 ptr
= req_capsule_server_get(&req
->rq_pill
, &RMF_EADATA
);
251 memcpy(buf
, ptr
, len
);
253 ptlrpc_req_finished(req
);
255 LLOG_CLIENT_EXIT(loghandle
->lgh_ctxt
, imp
);
259 static int llog_client_read_header(const struct lu_env
*env
,
260 struct llog_handle
*handle
)
262 struct obd_import
*imp
;
263 struct ptlrpc_request
*req
= NULL
;
264 struct llogd_body
*body
;
265 struct llog_log_hdr
*hdr
;
266 struct llog_rec_hdr
*llh_hdr
;
269 LLOG_CLIENT_ENTRY(handle
->lgh_ctxt
, imp
);
270 req
= ptlrpc_request_alloc_pack(imp
, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER
,
272 LLOG_ORIGIN_HANDLE_READ_HEADER
);
278 body
= req_capsule_client_get(&req
->rq_pill
, &RMF_LLOGD_BODY
);
279 body
->lgd_logid
= handle
->lgh_id
;
280 body
->lgd_ctxt_idx
= handle
->lgh_ctxt
->loc_idx
- 1;
281 body
->lgd_llh_flags
= handle
->lgh_hdr
->llh_flags
;
283 ptlrpc_request_set_replen(req
);
284 rc
= ptlrpc_queue_wait(req
);
288 hdr
= req_capsule_server_get(&req
->rq_pill
, &RMF_LLOG_LOG_HDR
);
294 memcpy(handle
->lgh_hdr
, hdr
, sizeof(*hdr
));
295 handle
->lgh_last_idx
= handle
->lgh_hdr
->llh_tail
.lrt_index
;
298 llh_hdr
= &handle
->lgh_hdr
->llh_hdr
;
299 if (llh_hdr
->lrh_type
!= LLOG_HDR_MAGIC
) {
300 CERROR("bad log header magic: %#x (expecting %#x)\n",
301 llh_hdr
->lrh_type
, LLOG_HDR_MAGIC
);
303 } else if (llh_hdr
->lrh_len
!= LLOG_CHUNK_SIZE
) {
304 CERROR("incorrectly sized log header: %#x (expecting %#x)\n",
305 llh_hdr
->lrh_len
, LLOG_CHUNK_SIZE
);
306 CERROR("you may need to re-run lconf --write_conf.\n");
310 ptlrpc_req_finished(req
);
312 LLOG_CLIENT_EXIT(handle
->lgh_ctxt
, imp
);
316 static int llog_client_close(const struct lu_env
*env
,
317 struct llog_handle
*handle
)
319 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
320 * the servers all close the file at the end of every
326 struct llog_operations llog_client_ops
= {
327 .lop_next_block
= llog_client_next_block
,
328 .lop_prev_block
= llog_client_prev_block
,
329 .lop_read_header
= llog_client_read_header
,
330 .lop_open
= llog_client_open
,
331 .lop_close
= llog_client_close
,
333 EXPORT_SYMBOL(llog_client_ops
);