Whamcloud - gitweb
land b1_5 onto HEAD
[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  *  Copyright (C) 2001-2004 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  *  remote api for llog - client side
26  *
27  */
28
29 #define DEBUG_SUBSYSTEM S_LOG
30
31 #ifndef EXPORT_SYMTAB
32 #define EXPORT_SYMTAB
33 #endif
34
35 #ifdef __KERNEL__
36 #include <libcfs/libcfs.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <obd_class.h>
42 #include <lustre_log.h>
43 #include <lustre_net.h>
44 #include <libcfs/list.h>
45
46 /* This is a callback from the llog_* functions.
47  * Assumes caller has already pushed us into the kernel context. */
48 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
49                               struct llog_logid *logid, char *name)
50 {
51         struct obd_import *imp;
52         struct llogd_body req_body;
53         struct llogd_body *body;
54         struct llog_handle *handle;
55         struct ptlrpc_request *req = NULL;
56         int size[3] = { sizeof(struct ptlrpc_body), sizeof(req_body) };
57         char *bufs[3] = { NULL, (char*)&req_body };
58         int bufcount = 2;
59         int rc;
60         ENTRY;
61
62         if (ctxt->loc_imp == NULL) {
63                 /* This used to be an assert; bug 6200 */
64                 CERROR("ctxt->loc_imp == NULL for context idx %d.  Unable to "
65                        "complete MDS/OSS recovery, but I'll try again next "
66                        "time.  Not fatal.\n", ctxt->loc_idx);
67                 RETURN(-EINVAL);
68         }
69         imp = ctxt->loc_imp;
70
71         handle = llog_alloc_handle();
72         if (handle == NULL)
73                 RETURN(-ENOMEM);
74         *res = handle;
75
76         memset(&req_body, 0, sizeof(req_body));
77         if (logid)
78                 req_body.lgd_logid = *logid;
79         req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
80
81         if (name) {
82                 size[bufcount] = strlen(name) + 1;
83                 bufs[bufcount] = name;
84                 bufcount++;
85         }
86
87         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
88                               LLOG_ORIGIN_HANDLE_CREATE, bufcount, size, bufs);
89         if (!req)
90                 GOTO(err_free, rc = -ENOMEM);
91
92         ptlrpc_req_set_repsize(req, 2, size);
93         rc = ptlrpc_queue_wait(req);
94         if (rc)
95                 GOTO(err_free, rc);
96
97         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
98                                  lustre_swab_llogd_body);
99         if (body == NULL) {
100                 CERROR ("Can't unpack llogd_body\n");
101                 GOTO(err_free, rc =-EFAULT);
102         }
103
104         handle->lgh_id = body->lgd_logid;
105         handle->lgh_ctxt = ctxt;
106
107 out:
108         if (req)
109                 ptlrpc_req_finished(req);
110         RETURN(rc);
111
112 err_free:
113         llog_free_handle(handle);
114         goto out;
115 }
116
117 static int llog_client_destroy(struct llog_handle *loghandle)
118 {
119         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
120         struct ptlrpc_request *req = NULL;
121         struct llogd_body *body;
122         int size[] = { sizeof(struct ptlrpc_body), sizeof(*body) };
123         int rc;
124         ENTRY;
125
126         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION, 
127                               LLOG_ORIGIN_HANDLE_DESTROY, 2, size, NULL);
128         if (!req)
129                 RETURN(-ENOMEM);
130
131         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
132         body->lgd_logid = loghandle->lgh_id;
133         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
134
135         ptlrpc_req_set_repsize(req, 2, size);
136         rc = ptlrpc_queue_wait(req);
137         
138         ptlrpc_req_finished(req);
139         RETURN(rc);
140 }
141
142
143 static int llog_client_next_block(struct llog_handle *loghandle,
144                                   int *cur_idx, int next_idx,
145                                   __u64 *cur_offset, void *buf, int len)
146 {
147         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
148         struct ptlrpc_request *req = NULL;
149         struct llogd_body *body;
150         void * ptr;
151         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
152         int rc;
153         ENTRY;
154
155         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
156                               LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 2, size, NULL);
157         if (!req)
158                 GOTO(out, rc = -ENOMEM);
159
160         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
161         body->lgd_logid = loghandle->lgh_id;
162         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
163         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
164         body->lgd_index = next_idx;
165         body->lgd_saved_index = *cur_idx;
166         body->lgd_len = len;
167         body->lgd_cur_offset = *cur_offset;
168
169         size[REPLY_REC_OFF + 1] = len;
170         ptlrpc_req_set_repsize(req, 3, size);
171         rc = ptlrpc_queue_wait(req);
172         if (rc)
173                 GOTO(out, rc);
174
175         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
176                                  lustre_swab_llogd_body);
177         if (body == NULL) {
178                 CERROR ("Can't unpack llogd_body\n");
179                 GOTO(out, rc =-EFAULT);
180         }
181
182         /* The log records are swabbed as they are processed */
183         ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
184         if (ptr == NULL) {
185                 CERROR ("Can't unpack bitmap\n");
186                 GOTO(out, rc =-EFAULT);
187         }
188
189         *cur_idx = body->lgd_saved_index;
190         *cur_offset = body->lgd_cur_offset;
191
192         memcpy(buf, ptr, len);
193
194 out:
195         if (req)
196                 ptlrpc_req_finished(req);
197         RETURN(rc);
198 }
199
200 static int llog_client_prev_block(struct llog_handle *loghandle,
201                                   int prev_idx, void *buf, int len)
202 {
203         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
204         struct ptlrpc_request *req = NULL;
205         struct llogd_body *body;
206         void * ptr;
207         int size[3] = { sizeof(struct ptlrpc_body), sizeof(*body) };
208         int rc;
209         ENTRY;
210
211         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
212                               LLOG_ORIGIN_HANDLE_PREV_BLOCK, 2, size, NULL);
213         if (!req)
214                 GOTO(out, rc = -ENOMEM);
215
216         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
217         body->lgd_logid = loghandle->lgh_id;
218         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
219         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
220         body->lgd_index = prev_idx;
221         body->lgd_len = len;
222
223         size[REPLY_REC_OFF + 1] = len;
224         ptlrpc_req_set_repsize(req, 3, size);
225         rc = ptlrpc_queue_wait(req);
226         if (rc)
227                 GOTO(out, rc);
228
229         body = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*body),
230                                  lustre_swab_llogd_body);
231         if (body == NULL) {
232                 CERROR ("Can't unpack llogd_body\n");
233                 GOTO(out, rc =-EFAULT);
234         }
235
236         ptr = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF + 1, len);
237         if (ptr == NULL) {
238                 CERROR ("Can't unpack bitmap\n");
239                 GOTO(out, rc =-EFAULT);
240         }
241
242         memcpy(buf, ptr, len);
243
244 out:
245         if (req)
246                 ptlrpc_req_finished(req);
247         RETURN(rc);
248 }
249
250 static int llog_client_read_header(struct llog_handle *handle)
251 {
252         struct obd_import *imp = handle->lgh_ctxt->loc_imp;
253         struct ptlrpc_request *req = NULL;
254         struct llogd_body *body;
255         struct llog_log_hdr *hdr;
256         struct llog_rec_hdr *llh_hdr;
257         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*body) };
258         int repsize[2] = { sizeof(struct ptlrpc_body), sizeof(*hdr) };
259         int rc;
260         ENTRY;
261
262         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
263                               LLOG_ORIGIN_HANDLE_READ_HEADER, 2, size, NULL);
264         if (!req)
265                 GOTO(out, rc = -ENOMEM);
266
267         body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF, sizeof(*body));
268         body->lgd_logid = handle->lgh_id;
269         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
270         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
271
272         ptlrpc_req_set_repsize(req, 2, repsize);
273         rc = ptlrpc_queue_wait(req);
274         if (rc)
275                 GOTO(out, rc);
276
277         hdr = lustre_swab_repbuf(req, REPLY_REC_OFF, sizeof(*hdr),
278                                  lustre_swab_llog_hdr);
279         if (hdr == NULL) {
280                 CERROR ("Can't unpack llog_hdr\n");
281                 GOTO(out, rc =-EFAULT);
282         }
283
284         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
285         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
286
287         /* sanity checks */
288         llh_hdr = &handle->lgh_hdr->llh_hdr;
289         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
290                 CERROR("bad log header magic: %#x (expecting %#x)\n",
291                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
292                 rc = -EIO;
293         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
294                 CERROR("incorrectly sized log header: %#x "
295                        "(expecting %#x)\n",
296                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
297                 CERROR("you may need to re-run lconf --write_conf.\n");
298                 rc = -EIO;
299         }
300
301 out:
302         if (req)
303                 ptlrpc_req_finished(req);
304         RETURN(rc);
305 }
306
307 static int llog_client_close(struct llog_handle *handle)
308 {
309         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
310            the servers all close the file at the end of every
311            other LLOG_ RPC. */
312         return(0);
313 }
314
315
316 struct llog_operations llog_client_ops = {
317         lop_next_block:  llog_client_next_block,
318         lop_prev_block:  llog_client_prev_block,
319         lop_read_header: llog_client_read_header,
320         lop_create:      llog_client_create,
321         lop_destroy:     llog_client_destroy,
322         lop_close:       llog_client_close,
323 };