Whamcloud - gitweb
LU-1347 build: remove the vim/emacs modelines
[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, Whamcloud, Inc.
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 #ifndef EXPORT_SYMTAB
46 #define EXPORT_SYMTAB
47 #endif
48
49 #ifdef __KERNEL__
50 #include <libcfs/libcfs.h>
51 #else
52 #include <liblustre.h>
53 #endif
54
55 #include <obd_class.h>
56 #include <lustre_log.h>
57 #include <lustre_net.h>
58 #include <libcfs/list.h>
59
60 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                             \
61         cfs_mutex_lock(&ctxt->loc_mutex);                             \
62         if (ctxt->loc_imp) {                                          \
63                 imp = class_import_get(ctxt->loc_imp);                \
64         } else {                                                      \
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", \
68                        ctxt->loc_idx);                                \
69                 imp = NULL;                                           \
70                 cfs_mutex_unlock(&ctxt->loc_mutex);                   \
71                 return (-EINVAL);                                     \
72         }                                                             \
73         cfs_mutex_unlock(&ctxt->loc_mutex);                           \
74 } while(0)
75
76 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
77         cfs_mutex_lock(&ctxt->loc_mutex);                             \
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_unlock(&ctxt->loc_mutex);                           \
83 } while(0)
84
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)
89 {
90         struct obd_import     *imp;
91         struct llogd_body     *body;
92         struct llog_handle    *handle;
93         struct ptlrpc_request *req = NULL;
94         int                    rc;
95         ENTRY;
96
97         LLOG_CLIENT_ENTRY(ctxt, imp);
98
99         handle = llog_alloc_handle();
100         if (handle == NULL)
101                 RETURN(-ENOMEM);
102         *res = handle;
103
104         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
105         if (req == NULL)
106                 GOTO(err_free, rc = -ENOMEM);
107
108         if (name)
109                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
110                                      strlen(name) + 1);
111
112         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
113                                  LLOG_ORIGIN_HANDLE_CREATE);
114         if (rc) {
115                 ptlrpc_request_free(req);
116                 req = NULL;
117                 GOTO(err_free, rc);
118         }
119         ptlrpc_request_set_replen(req);
120
121         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
122         if (logid)
123                 body->lgd_logid = *logid;
124         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
125
126         if (name) {
127                 char *tmp;
128                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
129                                                    strlen(name) + 1);
130                 LASSERT(tmp);
131                 strcpy(tmp, name);
132         }
133
134         rc = ptlrpc_queue_wait(req);
135         if (rc)
136                 GOTO(err_free, rc);
137
138         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
139         if (body == NULL)
140                 GOTO(err_free, rc =-EFAULT);
141
142         handle->lgh_id = body->lgd_logid;
143         handle->lgh_ctxt = ctxt;
144         EXIT;
145 out:
146         LLOG_CLIENT_EXIT(ctxt, imp);
147         ptlrpc_req_finished(req);
148         return rc;
149 err_free:
150         *res = NULL;
151         llog_free_handle(handle);
152         goto out;
153 }
154
155 static int llog_client_destroy(struct llog_handle *loghandle)
156 {
157         struct obd_import     *imp;
158         struct ptlrpc_request *req = NULL;
159         struct llogd_body     *body;
160         int                    rc;
161         ENTRY;
162
163         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
164         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
165                                         LUSTRE_LOG_VERSION,
166                                         LLOG_ORIGIN_HANDLE_DESTROY);
167         if (req == NULL)
168                 GOTO(err_exit, rc =-ENOMEM);
169
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;
173
174         ptlrpc_request_set_replen(req);
175         rc = ptlrpc_queue_wait(req);
176         
177         ptlrpc_req_finished(req);
178 err_exit:
179         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
180         RETURN(rc);
181 }
182
183
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)
187 {
188         struct obd_import     *imp;
189         struct ptlrpc_request *req = NULL;
190         struct llogd_body     *body;
191         void                  *ptr;
192         int                    rc;
193         ENTRY;
194
195         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
196         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
197                                         LUSTRE_LOG_VERSION,
198                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
199         if (req == NULL)
200                 GOTO(err_exit, rc =-ENOMEM);
201                 
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;
208         body->lgd_len = len;
209         body->lgd_cur_offset = *cur_offset;
210
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);
214         if (rc)
215                 GOTO(out, rc);
216
217         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
218         if (body == NULL)
219                 GOTO(out, rc =-EFAULT);
220
221         /* The log records are swabbed as they are processed */
222         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
223         if (ptr == NULL)
224                 GOTO(out, rc =-EFAULT);
225
226         *cur_idx = body->lgd_saved_index;
227         *cur_offset = body->lgd_cur_offset;
228
229         memcpy(buf, ptr, len);
230         EXIT;
231 out:
232         ptlrpc_req_finished(req);
233 err_exit:
234         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
235         return rc;
236 }
237
238 static int llog_client_prev_block(struct llog_handle *loghandle,
239                                   int prev_idx, void *buf, int len)
240 {
241         struct obd_import     *imp;
242         struct ptlrpc_request *req = NULL;
243         struct llogd_body     *body;
244         void                  *ptr;
245         int                    rc;
246         ENTRY;
247
248         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
249         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
250                                         LUSTRE_LOG_VERSION,
251                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
252         if (req == NULL)
253                 GOTO(err_exit, rc = -ENOMEM);
254
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;
260         body->lgd_len = len;
261
262         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
263         ptlrpc_request_set_replen(req);
264
265         rc = ptlrpc_queue_wait(req);
266         if (rc)
267                 GOTO(out, rc);
268
269         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
270         if (body == NULL)
271                 GOTO(out, rc =-EFAULT);
272
273         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
274         if (ptr == NULL)
275                 GOTO(out, rc =-EFAULT);
276
277         memcpy(buf, ptr, len);
278         EXIT;
279 out:
280         ptlrpc_req_finished(req);
281 err_exit:
282         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
283         return rc;
284 }
285
286 static int llog_client_read_header(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(struct llog_handle *handle)
342 {
343         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
344            the servers all close the file at the end of every
345            other LLOG_ RPC. */
346         return(0);
347 }
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_create:      llog_client_create,
355         lop_destroy:     llog_client_destroy,
356         lop_close:       llog_client_close,
357 };