Whamcloud - gitweb
b=21681 suppress bogus transno error
[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 req_body;
92         struct llogd_body *body;
93         struct llog_handle *handle;
94         struct ptlrpc_request *req = NULL;
95         __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(req_body) };
96         char *bufs[3] = { NULL, (char*)&req_body };
97         int bufcount = 2;
98         int rc;
99         ENTRY;
100
101         LLOG_CLIENT_ENTRY(ctxt, imp);
102
103         handle = llog_alloc_handle();
104         if (handle == NULL)
105                 GOTO(out, rc = -ENOMEM);
106         *res = handle;
107
108         memset(&req_body, 0, sizeof(req_body));
109         if (logid)
110                 req_body.lgd_logid = *logid;
111         req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
112
113         if (name) {
114                 size[bufcount] = strlen(name) + 1;
115                 bufs[bufcount] = name;
116                 bufcount++;
117         }
118
119         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
120                               LLOG_ORIGIN_HANDLE_CREATE, bufcount, size, bufs);
121         if (!req)
122                 GOTO(err_free, rc = -ENOMEM);
123
124         ptlrpc_req_set_repsize(req, 2, size);
125         rc = ptlrpc_queue_wait(req);
126         if (rc)
127                 GOTO(err_free, rc);
128
129         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
130                                  lustre_swab_llogd_body);
131         if (body == NULL) {
132                 CERROR ("Can't unpack llogd_body\n");
133                 GOTO(err_free, rc =-EFAULT);
134         }
135
136         handle->lgh_id = body->lgd_logid;
137         handle->lgh_ctxt = ctxt;
138
139 out:
140         if (req)
141                 ptlrpc_req_finished(req);
142         LLOG_CLIENT_EXIT(ctxt, imp);
143         RETURN(rc);
144
145 err_free:
146         llog_free_handle(handle);
147         goto out;
148 }
149
150 static int llog_client_destroy(struct llog_handle *loghandle)
151 {
152         struct obd_import     *imp;
153         struct ptlrpc_request *req = NULL;
154         struct llogd_body *body;
155         __u32 size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
156         int rc;
157         ENTRY;
158
159         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
160         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION, 
161                               LLOG_ORIGIN_HANDLE_DESTROY, 2, size, NULL);
162         if (!req)
163                 GOTO(out, rc = -ENOMEM);
164
165         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
166         body->lgd_logid = loghandle->lgh_id;
167         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
168
169         ptlrpc_req_set_repsize(req, 2, size);
170         rc = ptlrpc_queue_wait(req);
171         
172         ptlrpc_req_finished(req);
173 out:
174         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
175         RETURN(rc);
176 }
177
178
179 static int llog_client_next_block(struct llog_handle *loghandle,
180                                   int *cur_idx, int next_idx,
181                                   __u64 *cur_offset, void *buf, int len)
182 {
183         struct obd_import *imp;
184         struct ptlrpc_request *req = NULL;
185         struct llogd_body *body;
186         void * ptr;
187         __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
188         int rc;
189         ENTRY;
190
191         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
192         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
193                               LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 2, size, NULL);
194         if (!req)
195                 GOTO(out, rc =-ENOMEM);
196
197         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
198         body->lgd_logid = loghandle->lgh_id;
199         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
200         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
201         body->lgd_index = next_idx;
202         body->lgd_saved_index = *cur_idx;
203         body->lgd_len = len;
204         body->lgd_cur_offset = *cur_offset;
205
206         size[REPLY_REC_OFF + 1] = len;
207         ptlrpc_req_set_repsize(req, 3, size);
208         rc = ptlrpc_queue_wait(req);
209         if (rc)
210                 GOTO(out, rc);
211
212         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
213                                  lustre_swab_llogd_body);
214         if (body == NULL) {
215                 CERROR ("Can't unpack llogd_body\n");
216                 GOTO(out, rc =-EFAULT);
217         }
218
219         /* The log records are swabbed as they are processed */
220         ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
221         if (ptr == NULL) {
222                 CERROR ("Can't unpack bitmap\n");
223                 GOTO(out, rc =-EFAULT);
224         }
225
226         *cur_idx = body->lgd_saved_index;
227         *cur_offset = body->lgd_cur_offset;
228
229         memcpy(buf, ptr, len);
230
231 out:
232         if (req)
233                 ptlrpc_req_finished(req);
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         __u32 size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
246         int rc;
247         ENTRY;
248
249         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
250         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
251                               LLOG_ORIGIN_HANDLE_PREV_BLOCK, 2, size, NULL);
252         if (!req)
253                 GOTO(out, rc = -ENOMEM);
254
255         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*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         size[REPLY_REC_OFF + 1] = len;
263         ptlrpc_req_set_repsize(req, 3, size);
264         rc = ptlrpc_queue_wait(req);
265         if (rc)
266                 GOTO(out, rc);
267
268         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
269                                  lustre_swab_llogd_body);
270         if (body == NULL) {
271                 CERROR ("Can't unpack llogd_body\n");
272                 GOTO(out, rc =-EFAULT);
273         }
274
275         ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
276         if (ptr == NULL) {
277                 CERROR ("Can't unpack bitmap\n");
278                 GOTO(out, rc =-EFAULT);
279         }
280
281         memcpy(buf, ptr, len);
282
283 out:
284         if (req)
285                 ptlrpc_req_finished(req);
286         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
287         RETURN(rc);
288 }
289
290 static int llog_client_read_header(struct llog_handle *handle)
291 {
292         struct obd_import *imp;
293         struct ptlrpc_request *req = NULL;
294         struct llogd_body *body;
295         struct llog_log_hdr *hdr;
296         struct llog_rec_hdr *llh_hdr;
297         __u32 size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
298         __u32 repsize[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
299         int rc;
300         ENTRY;
301
302         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
303         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
304                               LLOG_ORIGIN_HANDLE_READ_HEADER, 2, size, NULL);
305         if (!req)
306                 GOTO(out, rc = -ENOMEM);
307
308         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
309         body->lgd_logid = handle->lgh_id;
310         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
311         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
312
313         ptlrpc_req_set_repsize(req, 2, repsize);
314         rc = ptlrpc_queue_wait(req);
315         if (rc)
316                 GOTO(out, rc);
317
318         hdr = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*hdr),
319                                  lustre_swab_llog_hdr);
320         if (hdr == NULL) {
321                 CERROR ("Can't unpack llog_hdr\n");
322                 GOTO(out, rc =-EFAULT);
323         }
324
325         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
326         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
327
328         /* sanity checks */
329         llh_hdr = &handle->lgh_hdr->llh_hdr;
330         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
331                 CERROR("bad log header magic: %#x (expecting %#x)\n",
332                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
333                 rc = -EIO;
334         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
335                 CERROR("incorrectly sized log header: %#x "
336                        "(expecting %#x)\n",
337                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
338                 CERROR("you may need to re-run lconf --write_conf.\n");
339                 rc = -EIO;
340         }
341
342 out:
343         if (req)
344                 ptlrpc_req_finished(req);
345         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
346         RETURN(rc);
347 }
348
349 static int llog_client_close(struct llog_handle *handle)
350 {
351         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
352            the servers all close the file at the end of every
353            other LLOG_ RPC. */
354         return(0);
355 }
356
357
358 struct llog_operations llog_client_ops = {
359         lop_next_block:  llog_client_next_block,
360         lop_prev_block:  llog_client_prev_block,
361         lop_read_header: llog_client_read_header,
362         lop_create:      llog_client_create,
363         lop_destroy:     llog_client_destroy,
364         lop_close:       llog_client_close,
365 };