Whamcloud - gitweb
b=20595
[fs/lustre-release.git] / lustre / ptlrpc / llog_client.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
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         mutex_down(&ctxt->loc_sem);                                   \
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                 mutex_up(&ctxt->loc_sem);                             \
71                 return (-EINVAL);                                     \
72         }                                                             \
73         mutex_up(&ctxt->loc_sem);                                     \
74 } while(0)
75
76 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
77         mutex_down(&ctxt->loc_sem);                                   \
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         mutex_up(&ctxt->loc_sem);                                     \
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         llog_free_handle(handle);
151         goto out;
152 }
153
154 static int llog_client_destroy(struct llog_handle *loghandle)
155 {
156         struct obd_import     *imp;
157         struct ptlrpc_request *req = NULL;
158         struct llogd_body     *body;
159         int                    rc;
160         ENTRY;
161
162         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
163         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
164                                         LUSTRE_LOG_VERSION,
165                                         LLOG_ORIGIN_HANDLE_DESTROY);
166         if (req == NULL)
167                 GOTO(err_exit, rc =-ENOMEM);
168
169         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
170         body->lgd_logid = loghandle->lgh_id;
171         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
172
173         ptlrpc_request_set_replen(req);
174         rc = ptlrpc_queue_wait(req);
175         
176         ptlrpc_req_finished(req);
177 err_exit:
178         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
179         RETURN(rc);
180 }
181
182
183 static int llog_client_next_block(struct llog_handle *loghandle,
184                                   int *cur_idx, int next_idx,
185                                   __u64 *cur_offset, void *buf, int len)
186 {
187         struct obd_import     *imp;
188         struct ptlrpc_request *req = NULL;
189         struct llogd_body     *body;
190         void                  *ptr;
191         int                    rc;
192         ENTRY;
193
194         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
195         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
196                                         LUSTRE_LOG_VERSION,
197                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
198         if (req == NULL)
199                 GOTO(err_exit, rc =-ENOMEM);
200                 
201         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
202         body->lgd_logid = loghandle->lgh_id;
203         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
204         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
205         body->lgd_index = next_idx;
206         body->lgd_saved_index = *cur_idx;
207         body->lgd_len = len;
208         body->lgd_cur_offset = *cur_offset;
209
210         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
211         ptlrpc_request_set_replen(req);
212         rc = ptlrpc_queue_wait(req);
213         if (rc)
214                 GOTO(out, rc);
215
216         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
217         if (body == NULL)
218                 GOTO(out, rc =-EFAULT);
219
220         /* The log records are swabbed as they are processed */
221         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
222         if (ptr == NULL)
223                 GOTO(out, rc =-EFAULT);
224
225         *cur_idx = body->lgd_saved_index;
226         *cur_offset = body->lgd_cur_offset;
227
228         memcpy(buf, ptr, len);
229         EXIT;
230 out:
231         ptlrpc_req_finished(req);
232 err_exit:
233         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
234         return rc;
235 }
236
237 static int llog_client_prev_block(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(struct llog_handle *handle)
286 {
287         struct obd_import     *imp;
288         struct ptlrpc_request *req = NULL;
289         struct llogd_body     *body;
290         struct llog_log_hdr   *hdr;
291         struct llog_rec_hdr   *llh_hdr;
292         int                    rc;
293         ENTRY;
294
295         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
296         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
297                                         LUSTRE_LOG_VERSION,
298                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
299         if (req == NULL)
300                 GOTO(err_exit, rc = -ENOMEM);
301
302         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
303         body->lgd_logid = handle->lgh_id;
304         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
305         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
306
307         ptlrpc_request_set_replen(req);
308         rc = ptlrpc_queue_wait(req);
309         if (rc)
310                 GOTO(out, rc);
311
312         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
313         if (hdr == NULL)
314                 GOTO(out, rc =-EFAULT);
315
316         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
317         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
318
319         /* sanity checks */
320         llh_hdr = &handle->lgh_hdr->llh_hdr;
321         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
322                 CERROR("bad log header magic: %#x (expecting %#x)\n",
323                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
324                 rc = -EIO;
325         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
326                 CERROR("incorrectly sized log header: %#x "
327                        "(expecting %#x)\n",
328                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
329                 CERROR("you may need to re-run lconf --write_conf.\n");
330                 rc = -EIO;
331         }
332         EXIT;
333 out:
334         ptlrpc_req_finished(req);
335 err_exit:
336         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
337         return rc;
338 }
339
340 static int llog_client_close(struct llog_handle *handle)
341 {
342         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
343            the servers all close the file at the end of every
344            other LLOG_ RPC. */
345         return(0);
346 }
347
348
349 struct llog_operations llog_client_ops = {
350         lop_next_block:  llog_client_next_block,
351         lop_prev_block:  llog_client_prev_block,
352         lop_read_header: llog_client_read_header,
353         lop_create:      llog_client_create,
354         lop_destroy:     llog_client_destroy,
355         lop_close:       llog_client_close,
356 };