Whamcloud - gitweb
b=16098
[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", \
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                 GOTO(err_free, rc);
117         }
118         ptlrpc_request_set_replen(req);
119
120         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
121         if (logid)
122                 body->lgd_logid = *logid;
123         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
124
125         if (name) {
126                 char *tmp;
127                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
128                                                    strlen(name) + 1);
129                 LASSERT(tmp);
130                 strcpy(tmp, name);
131         }
132
133         rc = ptlrpc_queue_wait(req);
134         if (rc)
135                 GOTO(err_free, rc);
136
137         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
138         if (body == NULL)
139                 GOTO(err_free, rc =-EFAULT);
140
141         handle->lgh_id = body->lgd_logid;
142         handle->lgh_ctxt = ctxt;
143         EXIT;
144 out:
145         LLOG_CLIENT_EXIT(ctxt, imp);
146         ptlrpc_req_finished(req);
147         return rc;
148 err_free:
149         llog_free_handle(handle);
150         goto out;
151 }
152
153 static int llog_client_destroy(struct llog_handle *loghandle)
154 {
155         struct obd_import     *imp;
156         struct ptlrpc_request *req = NULL;
157         struct llogd_body     *body;
158         int                    rc;
159         ENTRY;
160
161         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
162         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
163                                         LUSTRE_LOG_VERSION,
164                                         LLOG_ORIGIN_HANDLE_DESTROY);
165         if (req == NULL)
166                 GOTO(err_exit, rc =-ENOMEM);
167
168         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
169         body->lgd_logid = loghandle->lgh_id;
170         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
171
172         ptlrpc_request_set_replen(req);
173         rc = ptlrpc_queue_wait(req);
174         
175         ptlrpc_req_finished(req);
176 err_exit:
177         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
178         RETURN(rc);
179 }
180
181
182 static int llog_client_next_block(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(struct llog_handle *loghandle,
237                                   int prev_idx, void *buf, int len)
238 {
239         struct obd_import     *imp;
240         struct ptlrpc_request *req = NULL;
241         struct llogd_body     *body;
242         void                  *ptr;
243         int                    rc;
244         ENTRY;
245
246         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
247         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
248                                         LUSTRE_LOG_VERSION,
249                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
250         if (req == NULL)
251                 GOTO(err_exit, rc = -ENOMEM);
252
253         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
254         body->lgd_logid = loghandle->lgh_id;
255         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
256         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
257         body->lgd_index = prev_idx;
258         body->lgd_len = len;
259
260         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
261         ptlrpc_request_set_replen(req);
262
263         rc = ptlrpc_queue_wait(req);
264         if (rc)
265                 GOTO(out, rc);
266
267         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
268         if (body == NULL)
269                 GOTO(out, rc =-EFAULT);
270
271         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
272         if (ptr == NULL)
273                 GOTO(out, rc =-EFAULT);
274
275         memcpy(buf, ptr, len);
276         EXIT;
277 out:
278         ptlrpc_req_finished(req);
279 err_exit:
280         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
281         return rc;
282 }
283
284 static int llog_client_read_header(struct llog_handle *handle)
285 {
286         struct obd_import     *imp;
287         struct ptlrpc_request *req = NULL;
288         struct llogd_body     *body;
289         struct llog_log_hdr   *hdr;
290         struct llog_rec_hdr   *llh_hdr;
291         int                    rc;
292         ENTRY;
293
294         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
295         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
296                                         LUSTRE_LOG_VERSION,
297                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
298         if (req == NULL)
299                 GOTO(err_exit, rc = -ENOMEM);
300
301         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
302         body->lgd_logid = handle->lgh_id;
303         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
304         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
305
306         ptlrpc_request_set_replen(req);
307         rc = ptlrpc_queue_wait(req);
308         if (rc)
309                 GOTO(out, rc);
310
311         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
312         if (hdr == NULL)
313                 GOTO(out, rc =-EFAULT);
314
315         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
316         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
317
318         /* sanity checks */
319         llh_hdr = &handle->lgh_hdr->llh_hdr;
320         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
321                 CERROR("bad log header magic: %#x (expecting %#x)\n",
322                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
323                 rc = -EIO;
324         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
325                 CERROR("incorrectly sized log header: %#x "
326                        "(expecting %#x)\n",
327                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
328                 CERROR("you may need to re-run lconf --write_conf.\n");
329                 rc = -EIO;
330         }
331         EXIT;
332 out:
333         ptlrpc_req_finished(req);
334 err_exit:
335         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
336         return rc;
337 }
338
339 static int llog_client_close(struct llog_handle *handle)
340 {
341         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
342            the servers all close the file at the end of every
343            other LLOG_ RPC. */
344         return(0);
345 }
346
347
348 struct llog_operations llog_client_ops = {
349         lop_next_block:  llog_client_next_block,
350         lop_prev_block:  llog_client_prev_block,
351         lop_read_header: llog_client_read_header,
352         lop_create:      llog_client_create,
353         lop_destroy:     llog_client_destroy,
354         lop_close:       llog_client_close,
355 };