Whamcloud - gitweb
land b_ost_amd 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 Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  *  remote api for llog - client side
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_LOG
27
28 #ifndef EXPORT_SYMTAB
29 #define EXPORT_SYMTAB
30 #endif
31
32 #ifdef __KERNEL__
33 #include <linux/fs.h>
34 #else
35 #include <liblustre.h>
36 #endif
37
38 #include <linux/obd_class.h>
39 #include <linux/lustre_log.h>
40 #include <linux/lustre_net.h>
41 #include <portals/list.h>
42
43 /* This is a callback from the llog_* functions.
44  * Assumes caller has already pushed us into the kernel context. */
45 static int llog_client_open(struct llog_ctxt *ctxt, struct llog_handle **res,
46                             struct llog_logid *logid, char *name, int flags)
47 {
48         struct obd_import *imp;
49         struct llogd_body req_body;
50         struct llogd_body *body;
51         struct llog_handle *handle;
52         struct ptlrpc_request *req = NULL;
53         int size[2] = {sizeof(req_body)};
54         char *tmp[2] = {(char*) &req_body};
55         int bufcount = 1, repsize[] = {sizeof (req_body)}, rc;
56         ENTRY;
57
58         LASSERT(ctxt->loc_imp);
59         imp = ctxt->loc_imp;
60
61         handle = llog_alloc_handle();
62         if (handle == NULL)
63                 RETURN(-ENOMEM);
64         *res = handle;
65
66         memset(&req_body, 0, sizeof(req_body));
67         if (logid)
68                 req_body.lgd_logid = *logid;
69         req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
70         req_body.lgd_llh_flags = flags;
71
72         if (name) {
73                 size[bufcount] = strlen(name) + 1;
74                 tmp[bufcount] = name;
75                 bufcount++;
76         }
77
78         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_OPEN, bufcount, size,tmp);
79         if (!req)
80                 GOTO(err_free, rc = -ENOMEM);
81
82         req->rq_replen = lustre_msg_size(1, repsize);
83         rc = ptlrpc_queue_wait(req);
84         if (rc)
85                 GOTO(err_free, rc);
86
87         body = lustre_swab_repbuf(req, 0, sizeof(*body),
88                                  lustre_swab_llogd_body);
89         if (body == NULL) {
90                 CERROR ("Can't unpack llogd_body\n");
91                 GOTO(err_free, rc =-EFAULT);
92         }
93
94         handle->lgh_id = body->lgd_logid;
95         handle->lgh_ctxt = ctxt;
96
97 out:
98         if (req)
99                 ptlrpc_req_finished(req);
100         RETURN(rc);
101
102 err_free:
103         llog_free_handle(handle);
104         goto out;
105 }
106
107 static int llog_client_prev_block(struct llog_handle *loghandle,
108                                   int prev_idx, void *buf, int len)
109 {
110         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
111         struct ptlrpc_request *req = NULL;
112         struct llogd_body *body;
113         void * ptr;
114         int size = sizeof(*body);
115         int repsize[2] = {sizeof (*body)};
116         int rc;
117         ENTRY;
118                                                                                                                              
119         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_PREV_BLOCK, 1,&size,NULL);
120         if (!req)
121                 GOTO(out, rc = -ENOMEM);
122                                                                                                                              
123         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
124         body->lgd_logid = loghandle->lgh_id;
125         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
126         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
127         body->lgd_index = prev_idx;
128         body->lgd_len = len;
129         repsize[1] = len;
130                                                                                                                              
131         req->rq_replen = lustre_msg_size(2, repsize);
132         rc = ptlrpc_queue_wait(req);
133         if (rc)
134                 GOTO(out, rc);
135                                                                                                                              
136         body = lustre_swab_repbuf(req, 0, sizeof(*body),
137                                  lustre_swab_llogd_body);
138         if (body == NULL) {
139                 CERROR ("Can't unpack llogd_body\n");
140                 GOTO(out, rc =-EFAULT);
141         }
142                                                                                                                              
143         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
144         if (ptr == NULL) {
145                 CERROR ("Can't unpack bitmap\n");
146                 GOTO(out, rc =-EFAULT);
147         }
148                                                                                                                              
149         memcpy(buf, ptr, len);
150                                                                                                                              
151 out:
152         if (req)
153                 ptlrpc_req_finished(req);
154         RETURN(rc);
155 }
156
157 static int llog_client_next_block(struct llog_handle *loghandle,
158                                   int *cur_idx, int next_idx,
159                                   __u64 *cur_offset, void *buf, int len)
160 {
161         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
162         struct ptlrpc_request *req = NULL;
163         struct llogd_body *body;
164         void * ptr;
165         int size = sizeof(*body);
166         int repsize[2] = {sizeof (*body)};
167         int rc;
168         ENTRY;
169
170         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 1,&size,NULL);
171         if (!req)
172                 GOTO(out, rc = -ENOMEM);
173
174         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
175         body->lgd_logid = loghandle->lgh_id;
176         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
177         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
178         body->lgd_index = next_idx;
179         body->lgd_saved_index = *cur_idx;
180         body->lgd_len = len;
181         body->lgd_cur_offset = *cur_offset;
182         repsize[1] = len;
183
184         req->rq_replen = lustre_msg_size(2, repsize);
185         rc = ptlrpc_queue_wait(req);
186         if (rc)
187                 GOTO(out, rc);
188
189         body = lustre_swab_repbuf(req, 0, sizeof(*body),
190                                  lustre_swab_llogd_body);
191         if (body == NULL) {
192                 CERROR ("Can't unpack llogd_body\n");
193                 GOTO(out, rc =-EFAULT);
194         }
195
196         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
197         if (ptr == NULL) {
198                 CERROR ("Can't unpack bitmap\n");
199                 GOTO(out, rc =-EFAULT);
200         }
201
202         *cur_idx = body->lgd_saved_index;
203         *cur_offset = body->lgd_cur_offset;
204
205         memcpy(buf, ptr, len);
206
207 out:
208         if (req)
209                 ptlrpc_req_finished(req);
210         RETURN(rc);
211 }
212
213
214 static int llog_client_read_header(struct llog_handle *handle)
215 {
216         struct obd_import *imp = handle->lgh_ctxt->loc_imp;
217         struct ptlrpc_request *req = NULL;
218         struct llogd_body *body;
219         struct llog_log_hdr *hdr;
220         struct llog_rec_hdr *llh_hdr;
221         int size = sizeof(*body);
222         int repsize = sizeof (*hdr);
223         int rc;
224         ENTRY;
225
226         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_READ_HEADER,1,&size,NULL);
227         if (!req)
228                 GOTO(out, rc = -ENOMEM);
229
230         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
231         body->lgd_logid = handle->lgh_id;
232         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
233         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
234
235         req->rq_replen = lustre_msg_size(1, &repsize);
236         rc = ptlrpc_queue_wait(req);
237         if (rc)
238                 GOTO(out, rc);
239
240         hdr = lustre_swab_repbuf(req, 0, sizeof(*hdr), lustre_swab_llog_hdr);
241         if (hdr == NULL) {
242                 CERROR ("Can't unpack llog_hdr\n");
243                 GOTO(out, rc =-EFAULT);
244         }
245
246         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
247         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
248
249         /* sanity checks */
250         llh_hdr = &handle->lgh_hdr->llh_hdr;
251         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
252                 CERROR("bad log header magic: %#x (expecting %#x)\n",
253                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
254                 rc = -EIO;
255         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
256                 CERROR("incorrectly sized log header: %#x "
257                        "(expecting %#x)\n",
258                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
259                 CERROR("you may need to re-run lconf --write_conf.\n");
260                 rc = -EIO;
261         }
262
263 out:
264         if (req)
265                 ptlrpc_req_finished(req);
266         RETURN(rc);
267 }
268
269 static int llog_client_close(struct llog_handle *handle)
270 {
271         int rc = 0;
272
273         RETURN(rc);
274 }
275
276
277 struct llog_operations llog_client_ops = {
278         lop_prev_block:  llog_client_prev_block,
279         lop_next_block:  llog_client_next_block,
280         lop_read_header: llog_client_read_header,
281         lop_open:        llog_client_open,
282         lop_close:       llog_client_close,
283 };