1 /*
2 * GPL HEADER START
3 *
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 *
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.
9 *
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).
15 *
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
19 *
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
22 * have any questions.
23 *
24 * GPL HEADER END
25 */
26 /*
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
29 *
30 * Copyright (c) 2012, Intel Corporation.
31 */
32 /*
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
35 *
36 * lustre/ptlrpc/llog_client.c
37 *
38 * remote api for llog - client side
39 *
40 * Author: Andreas Dilger <adilger@clusterfs.com>
41 */
42
43 #define DEBUG_SUBSYSTEM S_LOG
44
45 #include "../../include/linux/libcfs/libcfs.h"
46
47 #include "../include/obd_class.h"
48 #include "../include/lustre_log.h"
49 #include "../include/lustre_net.h"
50 #include <linux/list.h>
51
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); \
56 } else { \
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", \
60 ctxt->loc_idx); \
61 imp = NULL; \
62 mutex_unlock(&ctxt->loc_mutex); \
63 return (-EINVAL); \
64 } \
65 mutex_unlock(&ctxt->loc_mutex); \
66 } while (0)
67
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); \
75 } while (0)
76
77 /* This is a callback from the llog_* functions.
78 * Assumes caller has already pushed us into the kernel context. */
llog_client_open(const struct lu_env * env,struct llog_handle * lgh,struct llog_logid * logid,char * name,enum llog_open_param open_param)79 static int llog_client_open(const struct lu_env *env,
80 struct llog_handle *lgh, struct llog_logid *logid,
81 char *name, enum llog_open_param open_param)
82 {
83 struct obd_import *imp;
84 struct llogd_body *body;
85 struct llog_ctxt *ctxt = lgh->lgh_ctxt;
86 struct ptlrpc_request *req = NULL;
87 int rc;
88
89 LLOG_CLIENT_ENTRY(ctxt, imp);
90
91 /* client cannot create llog */
92 LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
93 LASSERT(lgh);
94
95 req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
96 if (req == NULL) {
97 rc = -ENOMEM;
98 goto out;
99 }
100
101 if (name)
102 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
103 strlen(name) + 1);
104
105 rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
106 LLOG_ORIGIN_HANDLE_CREATE);
107 if (rc) {
108 ptlrpc_request_free(req);
109 req = NULL;
110 goto out;
111 }
112 ptlrpc_request_set_replen(req);
113
114 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
115 if (logid)
116 body->lgd_logid = *logid;
117 body->lgd_ctxt_idx = ctxt->loc_idx - 1;
118
119 if (name) {
120 char *tmp;
121
122 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
123 strlen(name) + 1);
124 LASSERT(tmp);
125 strcpy(tmp, name);
126 }
127
128 rc = ptlrpc_queue_wait(req);
129 if (rc)
130 goto out;
131
132 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
133 if (body == NULL) {
134 rc = -EFAULT;
135 goto out;
136 }
137
138 lgh->lgh_id = body->lgd_logid;
139 lgh->lgh_ctxt = ctxt;
140 out:
141 LLOG_CLIENT_EXIT(ctxt, imp);
142 ptlrpc_req_finished(req);
143 return rc;
144 }
145
llog_client_next_block(const struct lu_env * env,struct llog_handle * loghandle,int * cur_idx,int next_idx,__u64 * cur_offset,void * buf,int len)146 static int llog_client_next_block(const struct lu_env *env,
147 struct llog_handle *loghandle,
148 int *cur_idx, int next_idx,
149 __u64 *cur_offset, void *buf, int len)
150 {
151 struct obd_import *imp;
152 struct ptlrpc_request *req = NULL;
153 struct llogd_body *body;
154 void *ptr;
155 int rc;
156
157 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
158 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
159 LUSTRE_LOG_VERSION,
160 LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
161 if (req == NULL) {
162 rc = -ENOMEM;
163 goto err_exit;
164 }
165
166 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
167 body->lgd_logid = loghandle->lgh_id;
168 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
169 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
170 body->lgd_index = next_idx;
171 body->lgd_saved_index = *cur_idx;
172 body->lgd_len = len;
173 body->lgd_cur_offset = *cur_offset;
174
175 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
176 ptlrpc_request_set_replen(req);
177 rc = ptlrpc_queue_wait(req);
178 if (rc)
179 goto out;
180
181 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
182 if (body == NULL) {
183 rc = -EFAULT;
184 goto out;
185 }
186
187 /* The log records are swabbed as they are processed */
188 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
189 if (ptr == NULL) {
190 rc = -EFAULT;
191 goto out;
192 }
193
194 *cur_idx = body->lgd_saved_index;
195 *cur_offset = body->lgd_cur_offset;
196
197 memcpy(buf, ptr, len);
198 out:
199 ptlrpc_req_finished(req);
200 err_exit:
201 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
202 return rc;
203 }
204
llog_client_prev_block(const struct lu_env * env,struct llog_handle * loghandle,int prev_idx,void * buf,int len)205 static int llog_client_prev_block(const struct lu_env *env,
206 struct llog_handle *loghandle,
207 int prev_idx, void *buf, int len)
208 {
209 struct obd_import *imp;
210 struct ptlrpc_request *req = NULL;
211 struct llogd_body *body;
212 void *ptr;
213 int rc;
214
215 LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
216 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
217 LUSTRE_LOG_VERSION,
218 LLOG_ORIGIN_HANDLE_PREV_BLOCK);
219 if (req == NULL) {
220 rc = -ENOMEM;
221 goto err_exit;
222 }
223
224 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
225 body->lgd_logid = loghandle->lgh_id;
226 body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
227 body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
228 body->lgd_index = prev_idx;
229 body->lgd_len = len;
230
231 req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
232 ptlrpc_request_set_replen(req);
233
234 rc = ptlrpc_queue_wait(req);
235 if (rc)
236 goto out;
237
238 body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
239 if (body == NULL) {
240 rc = -EFAULT;
241 goto out;
242 }
243
244 ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
245 if (ptr == NULL) {
246 rc = -EFAULT;
247 goto out;
248 }
249
250 memcpy(buf, ptr, len);
251 out:
252 ptlrpc_req_finished(req);
253 err_exit:
254 LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
255 return rc;
256 }
257
llog_client_read_header(const struct lu_env * env,struct llog_handle * handle)258 static int llog_client_read_header(const struct lu_env *env,
259 struct llog_handle *handle)
260 {
261 struct obd_import *imp;
262 struct ptlrpc_request *req = NULL;
263 struct llogd_body *body;
264 struct llog_log_hdr *hdr;
265 struct llog_rec_hdr *llh_hdr;
266 int rc;
267
268 LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
269 req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
270 LUSTRE_LOG_VERSION,
271 LLOG_ORIGIN_HANDLE_READ_HEADER);
272 if (req == NULL) {
273 rc = -ENOMEM;
274 goto err_exit;
275 }
276
277 body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
278 body->lgd_logid = handle->lgh_id;
279 body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
280 body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
281
282 ptlrpc_request_set_replen(req);
283 rc = ptlrpc_queue_wait(req);
284 if (rc)
285 goto out;
286
287 hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
288 if (hdr == NULL) {
289 rc = -EFAULT;
290 goto out;
291 }
292
293 memcpy(handle->lgh_hdr, hdr, sizeof(*hdr));
294 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
295
296 /* sanity checks */
297 llh_hdr = &handle->lgh_hdr->llh_hdr;
298 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
299 CERROR("bad log header magic: %#x (expecting %#x)\n",
300 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
301 rc = -EIO;
302 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
303 CERROR("incorrectly sized log header: %#x (expecting %#x)\n",
304 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
305 CERROR("you may need to re-run lconf --write_conf.\n");
306 rc = -EIO;
307 }
308 out:
309 ptlrpc_req_finished(req);
310 err_exit:
311 LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
312 return rc;
313 }
314
llog_client_close(const struct lu_env * env,struct llog_handle * handle)315 static int llog_client_close(const struct lu_env *env,
316 struct llog_handle *handle)
317 {
318 /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
319 the servers all close the file at the end of every
320 other LLOG_ RPC. */
321 return 0;
322 }
323
324 struct llog_operations llog_client_ops = {
325 .lop_next_block = llog_client_next_block,
326 .lop_prev_block = llog_client_prev_block,
327 .lop_read_header = llog_client_read_header,
328 .lop_open = llog_client_open,
329 .lop_close = llog_client_close,
330 };
331 EXPORT_SYMBOL(llog_client_ops);
332