Whamcloud - gitweb
LU-2446 build: Update Whamcloud copyright messages for Intel
[fs/lustre-release.git] / lustre / ptlrpc / llog_client.c
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 #ifdef __KERNEL__
46 #include <libcfs/libcfs.h>
47 #else
48 #include <liblustre.h>
49 #endif
50
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include <lustre_net.h>
54 #include <libcfs/list.h>
55
56 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                             \
57         mutex_lock(&ctxt->loc_mutex);                             \
58         if (ctxt->loc_imp) {                                          \
59                 imp = class_import_get(ctxt->loc_imp);                \
60         } else {                                                      \
61                 CERROR("ctxt->loc_imp == NULL for context idx %d."    \
62                        "Unable to complete MDS/OSS recovery,"         \
63                        "but I'll try again next time.  Not fatal.\n", \
64                        ctxt->loc_idx);                                \
65                 imp = NULL;                                           \
66                 mutex_unlock(&ctxt->loc_mutex);                   \
67                 return (-EINVAL);                                     \
68         }                                                             \
69         mutex_unlock(&ctxt->loc_mutex);                           \
70 } while(0)
71
72 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
73         mutex_lock(&ctxt->loc_mutex);                             \
74         if (ctxt->loc_imp != imp)                                     \
75                 CWARN("loc_imp has changed from %p to %p\n",          \
76                        ctxt->loc_imp, imp);                           \
77         class_import_put(imp);                                        \
78         mutex_unlock(&ctxt->loc_mutex);                           \
79 } while(0)
80
81 /* This is a callback from the llog_* functions.
82  * Assumes caller has already pushed us into the kernel context. */
83 static int llog_client_open(const struct lu_env *env,
84                             struct llog_handle *lgh, struct llog_logid *logid,
85                             char *name, enum llog_open_param open_param)
86 {
87         struct obd_import     *imp;
88         struct llogd_body     *body;
89         struct llog_ctxt      *ctxt = lgh->lgh_ctxt;
90         struct ptlrpc_request *req = NULL;
91         int                    rc;
92         ENTRY;
93
94         LLOG_CLIENT_ENTRY(ctxt, imp);
95
96         /* client cannot create llog */
97         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
98         LASSERT(lgh);
99
100         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
101         if (req == NULL)
102                 GOTO(out, rc = -ENOMEM);
103
104         if (name)
105                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
106                                      strlen(name) + 1);
107
108         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
109                                  LLOG_ORIGIN_HANDLE_CREATE);
110         if (rc) {
111                 ptlrpc_request_free(req);
112                 req = NULL;
113                 GOTO(out, rc);
114         }
115         ptlrpc_request_set_replen(req);
116
117         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
118         if (logid)
119                 body->lgd_logid = *logid;
120         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
121
122         if (name) {
123                 char *tmp;
124                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
125                                                    strlen(name) + 1);
126                 LASSERT(tmp);
127                 strcpy(tmp, name);
128         }
129
130         rc = ptlrpc_queue_wait(req);
131         if (rc)
132                 GOTO(out, rc);
133
134         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
135         if (body == NULL)
136                 GOTO(out, rc = -EFAULT);
137
138         lgh->lgh_id = body->lgd_logid;
139         lgh->lgh_ctxt = ctxt;
140         EXIT;
141 out:
142         LLOG_CLIENT_EXIT(ctxt, imp);
143         ptlrpc_req_finished(req);
144         return rc;
145 }
146
147 static int llog_client_destroy(const struct lu_env *env,
148                                struct llog_handle *loghandle)
149 {
150         struct obd_import     *imp;
151         struct ptlrpc_request *req = NULL;
152         struct llogd_body     *body;
153         int                    rc;
154         ENTRY;
155
156         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
157         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
158                                         LUSTRE_LOG_VERSION,
159                                         LLOG_ORIGIN_HANDLE_DESTROY);
160         if (req == NULL)
161                 GOTO(err_exit, rc =-ENOMEM);
162
163         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
164         body->lgd_logid = loghandle->lgh_id;
165         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
166
167         if (!(body->lgd_llh_flags & LLOG_F_IS_PLAIN))
168                 CERROR("%s: wrong llog flags %x\n", imp->imp_obd->obd_name,
169                        body->lgd_llh_flags);
170
171         ptlrpc_request_set_replen(req);
172         rc = ptlrpc_queue_wait(req);
173
174         ptlrpc_req_finished(req);
175 err_exit:
176         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
177         RETURN(rc);
178 }
179
180
181 static int llog_client_next_block(const struct lu_env *env,
182                                   struct llog_handle *loghandle,
183                                   int *cur_idx, int next_idx,
184                                   __u64 *cur_offset, void *buf, int len)
185 {
186         struct obd_import     *imp;
187         struct ptlrpc_request *req = NULL;
188         struct llogd_body     *body;
189         void                  *ptr;
190         int                    rc;
191         ENTRY;
192
193         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
194         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
195                                         LUSTRE_LOG_VERSION,
196                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
197         if (req == NULL)
198                 GOTO(err_exit, rc =-ENOMEM);
199
200         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
201         body->lgd_logid = loghandle->lgh_id;
202         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
203         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
204         body->lgd_index = next_idx;
205         body->lgd_saved_index = *cur_idx;
206         body->lgd_len = len;
207         body->lgd_cur_offset = *cur_offset;
208
209         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
210         ptlrpc_request_set_replen(req);
211         rc = ptlrpc_queue_wait(req);
212         if (rc)
213                 GOTO(out, rc);
214
215         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
216         if (body == NULL)
217                 GOTO(out, rc =-EFAULT);
218
219         /* The log records are swabbed as they are processed */
220         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
221         if (ptr == NULL)
222                 GOTO(out, rc =-EFAULT);
223
224         *cur_idx = body->lgd_saved_index;
225         *cur_offset = body->lgd_cur_offset;
226
227         memcpy(buf, ptr, len);
228         EXIT;
229 out:
230         ptlrpc_req_finished(req);
231 err_exit:
232         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
233         return rc;
234 }
235
236 static int llog_client_prev_block(const struct lu_env *env,
237                                   struct llog_handle *loghandle,
238                                   int prev_idx, void *buf, int len)
239 {
240         struct obd_import     *imp;
241         struct ptlrpc_request *req = NULL;
242         struct llogd_body     *body;
243         void                  *ptr;
244         int                    rc;
245         ENTRY;
246
247         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
248         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
249                                         LUSTRE_LOG_VERSION,
250                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
251         if (req == NULL)
252                 GOTO(err_exit, rc = -ENOMEM);
253
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;
259         body->lgd_len = len;
260
261         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
262         ptlrpc_request_set_replen(req);
263
264         rc = ptlrpc_queue_wait(req);
265         if (rc)
266                 GOTO(out, rc);
267
268         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
269         if (body == NULL)
270                 GOTO(out, rc =-EFAULT);
271
272         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
273         if (ptr == NULL)
274                 GOTO(out, rc =-EFAULT);
275
276         memcpy(buf, ptr, len);
277         EXIT;
278 out:
279         ptlrpc_req_finished(req);
280 err_exit:
281         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
282         return rc;
283 }
284
285 static int llog_client_read_header(const struct lu_env *env,
286                                    struct llog_handle *handle)
287 {
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;
293         int                    rc;
294         ENTRY;
295
296         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
297         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
298                                         LUSTRE_LOG_VERSION,
299                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
300         if (req == NULL)
301                 GOTO(err_exit, rc = -ENOMEM);
302
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;
307
308         ptlrpc_request_set_replen(req);
309         rc = ptlrpc_queue_wait(req);
310         if (rc)
311                 GOTO(out, rc);
312
313         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
314         if (hdr == NULL)
315                 GOTO(out, rc =-EFAULT);
316
317         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
318         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
319
320         /* sanity checks */
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);
325                 rc = -EIO;
326         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
327                 CERROR("incorrectly sized log header: %#x "
328                        "(expecting %#x)\n",
329                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
330                 CERROR("you may need to re-run lconf --write_conf.\n");
331                 rc = -EIO;
332         }
333         EXIT;
334 out:
335         ptlrpc_req_finished(req);
336 err_exit:
337         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
338         return rc;
339 }
340
341 static int llog_client_close(const struct lu_env *env,
342                              struct llog_handle *handle)
343 {
344         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
345            the servers all close the file at the end of every
346            other LLOG_ RPC. */
347         return(0);
348 }
349
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_open               = llog_client_open,
355         .lop_destroy            = llog_client_destroy,
356         .lop_close              = llog_client_close,
357 };
358 EXPORT_SYMBOL(llog_client_ops);