Whamcloud - gitweb
land b_groups 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, LUSTRE_LOG_VERSION, LLOG_ORIGIN_HANDLE_OPEN,
79                               bufcount, size,tmp);
80         if (!req)
81                 GOTO(err_free, rc = -ENOMEM);
82
83         req->rq_replen = lustre_msg_size(1, repsize);
84         rc = ptlrpc_queue_wait(req);
85         if (rc)
86                 GOTO(err_free, rc);
87
88         body = lustre_swab_repbuf(req, 0, sizeof(*body),
89                                  lustre_swab_llogd_body);
90         if (body == NULL) {
91                 CERROR ("Can't unpack llogd_body\n");
92                 GOTO(err_free, rc =-EFAULT);
93         }
94
95         handle->lgh_id = body->lgd_logid;
96         handle->lgh_ctxt = ctxt;
97
98 out:
99         if (req)
100                 ptlrpc_req_finished(req);
101         RETURN(rc);
102
103 err_free:
104         llog_free_handle(handle);
105         goto out;
106 }
107
108 static int llog_client_prev_block(struct llog_handle *loghandle,
109                                   int prev_idx, void *buf, int len)
110 {
111         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
112         struct ptlrpc_request *req = NULL;
113         struct llogd_body *body;
114         void * ptr;
115         int size = sizeof(*body);
116         int repsize[2] = {sizeof (*body)};
117         int rc;
118         ENTRY;
119
120         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
121                               LLOG_ORIGIN_HANDLE_PREV_BLOCK, 1, &size, NULL);
122         if (!req)
123                 GOTO(out, rc = -ENOMEM);
124
125         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
126         body->lgd_logid = loghandle->lgh_id;
127         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
128         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
129         body->lgd_index = prev_idx;
130         body->lgd_len = len;
131         repsize[1] = len;
132
133         req->rq_replen = lustre_msg_size(2, repsize);
134         rc = ptlrpc_queue_wait(req);
135         if (rc)
136                 GOTO(out, rc);
137
138         body = lustre_swab_repbuf(req, 0, sizeof(*body),
139                                  lustre_swab_llogd_body);
140         if (body == NULL) {
141                 CERROR ("Can't unpack llogd_body\n");
142                 GOTO(out, rc =-EFAULT);
143         }
144
145         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
146         if (ptr == NULL) {
147                 CERROR ("Can't unpack bitmap\n");
148                 GOTO(out, rc =-EFAULT);
149         }
150
151         memcpy(buf, ptr, len);
152 out:
153         if (req)
154                 ptlrpc_req_finished(req);
155         RETURN(rc);
156 }
157
158 static int llog_client_next_block(struct llog_handle *loghandle,
159                                   int *cur_idx, int next_idx,
160                                   __u64 *cur_offset, void *buf, int len)
161 {
162         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
163         struct ptlrpc_request *req = NULL;
164         struct llogd_body *body;
165         void * ptr;
166         int size = sizeof(*body);
167         int repsize[2] = {sizeof (*body)};
168         int rc;
169         ENTRY;
170
171         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
172                               LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 1, &size, NULL);
173         if (!req)
174                 GOTO(out, rc = -ENOMEM);
175
176         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
177         body->lgd_logid = loghandle->lgh_id;
178         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
179         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
180         body->lgd_index = next_idx;
181         body->lgd_saved_index = *cur_idx;
182         body->lgd_len = len;
183         body->lgd_cur_offset = *cur_offset;
184         repsize[1] = len;
185
186         req->rq_replen = lustre_msg_size(2, repsize);
187         rc = ptlrpc_queue_wait(req);
188         if (rc)
189                 GOTO(out, rc);
190
191         body = lustre_swab_repbuf(req, 0, sizeof(*body),
192                                  lustre_swab_llogd_body);
193         if (body == NULL) {
194                 CERROR ("Can't unpack llogd_body\n");
195                 GOTO(out, rc =-EFAULT);
196         }
197
198         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
199         if (ptr == NULL) {
200                 CERROR ("Can't unpack bitmap\n");
201                 GOTO(out, rc =-EFAULT);
202         }
203
204         *cur_idx = body->lgd_saved_index;
205         *cur_offset = body->lgd_cur_offset;
206
207         memcpy(buf, ptr, len);
208
209 out:
210         if (req)
211                 ptlrpc_req_finished(req);
212         RETURN(rc);
213 }
214
215
216 static int llog_client_read_header(struct llog_handle *handle)
217 {
218         struct obd_import *imp = handle->lgh_ctxt->loc_imp;
219         struct ptlrpc_request *req = NULL;
220         struct llogd_body *body;
221         struct llog_log_hdr *hdr;
222         struct llog_rec_hdr *llh_hdr;
223         int size = sizeof(*body);
224         int repsize = sizeof (*hdr);
225         int rc;
226         ENTRY;
227
228         req = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
229                               LLOG_ORIGIN_HANDLE_READ_HEADER, 1, &size, NULL);
230         if (!req)
231                 GOTO(out, rc = -ENOMEM);
232
233         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
234         body->lgd_logid = handle->lgh_id;
235         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
236         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
237
238         req->rq_replen = lustre_msg_size(1, &repsize);
239         rc = ptlrpc_queue_wait(req);
240         if (rc)
241                 GOTO(out, rc);
242
243         hdr = lustre_swab_repbuf(req, 0, sizeof(*hdr), lustre_swab_llog_hdr);
244         if (hdr == NULL) {
245                 CERROR ("Can't unpack llog_hdr\n");
246                 GOTO(out, rc =-EFAULT);
247         }
248
249         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
250         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
251
252         /* sanity checks */
253         llh_hdr = &handle->lgh_hdr->llh_hdr;
254         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
255                 CERROR("bad log header magic: %#x (expecting %#x)\n",
256                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
257                 rc = -EIO;
258         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
259                 CERROR("incorrectly sized log header: %#x "
260                        "(expecting %#x)\n",
261                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
262                 CERROR("you may need to re-run lconf --write_conf.\n");
263                 rc = -EIO;
264         }
265
266 out:
267         if (req)
268                 ptlrpc_req_finished(req);
269         RETURN(rc);
270 }
271
272 static int llog_client_close(struct llog_handle *handle)
273 {
274         int rc = 0;
275
276         RETURN(rc);
277 }
278
279
280 struct llog_operations llog_client_ops = {
281         lop_prev_block:  llog_client_prev_block,
282         lop_next_block:  llog_client_next_block,
283         lop_read_header: llog_client_read_header,
284         lop_open:        llog_client_open,
285         lop_close:       llog_client_close,
286 };