Whamcloud - gitweb
Land b_orphan on HEAD (20040130_1601)
[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-2003 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_create(struct llog_ctxt *ctxt, struct llog_handle **res,
46                             struct llog_logid *logid, char *name)
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;
56         int repsize[] = {sizeof (req_body)};
57         int rc;
58         ENTRY;
59
60         LASSERT(ctxt->loc_imp);
61         imp = ctxt->loc_imp;
62
63         handle = llog_alloc_handle();
64         if (handle == NULL)
65                 RETURN(-ENOMEM);
66         *res = handle;
67
68         memset(&req_body, 0, sizeof(req_body));
69         if (logid)
70                 req_body.lgd_logid = *logid;
71         req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
72
73         if (name) {
74                 size[bufcount] = strlen(name) + 1;
75                 tmp[bufcount] = name;
76                 bufcount++;
77         }
78
79         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_CREATE,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
109 static int llog_client_next_block(struct llog_handle *loghandle,
110                                   int *cur_idx, int next_idx,
111                                   __u64 *cur_offset, void *buf, int len)
112 {
113         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
114         struct ptlrpc_request *req = NULL;
115         struct llogd_body *body;
116         void * ptr;
117         int size = sizeof(*body);
118         int repsize[2] = {sizeof (*body)};
119         int rc;
120         ENTRY;
121
122         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 1,&size,NULL);
123         if (!req)
124                 GOTO(out, rc = -ENOMEM);
125
126         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
127         body->lgd_logid = loghandle->lgh_id;
128         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
129         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
130         body->lgd_index = next_idx;
131         body->lgd_saved_index = *cur_idx;
132         body->lgd_len = len;
133         body->lgd_cur_offset = *cur_offset;
134         repsize[1] = len;
135
136         req->rq_replen = lustre_msg_size(2, repsize);
137         rc = ptlrpc_queue_wait(req);
138         if (rc)
139                 GOTO(out, rc);
140
141         body = lustre_swab_repbuf(req, 0, sizeof(*body),
142                                  lustre_swab_llogd_body);
143         if (body == NULL) {
144                 CERROR ("Can't unpack llogd_body\n");
145                 GOTO(out, rc =-EFAULT);
146         }
147
148         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
149         if (ptr == NULL) {
150                 CERROR ("Can't unpack bitmap\n");
151                 GOTO(out, rc =-EFAULT);
152         }
153
154         *cur_idx = body->lgd_saved_index;
155         *cur_offset = body->lgd_cur_offset;
156
157         memcpy(buf, ptr, len);
158
159 out:
160         if (req)
161                 ptlrpc_req_finished(req);
162         RETURN(rc);
163 }
164
165
166 static int llog_client_read_header(struct llog_handle *handle)
167 {
168         struct obd_import *imp = handle->lgh_ctxt->loc_imp;
169         struct ptlrpc_request *req = NULL;
170         struct llogd_body *body;
171         struct llog_log_hdr *hdr;
172         int size = sizeof(*body);
173         int repsize = sizeof (*hdr);
174         int rc;
175         ENTRY;
176
177         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_READ_HEADER,1,&size,NULL);
178         if (!req)
179                 GOTO(out, rc = -ENOMEM);
180
181         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
182         body->lgd_logid = handle->lgh_id;
183         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
184         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
185
186         req->rq_replen = lustre_msg_size(1, &repsize);
187         rc = ptlrpc_queue_wait(req);
188         if (rc)
189                 GOTO(out, rc);
190
191         hdr = lustre_swab_repbuf(req, 0, sizeof(*hdr), lustre_swab_llog_hdr);
192         if (hdr == NULL) {
193                 CERROR ("Can't unpack llog_hdr\n");
194                 GOTO(out, rc =-EFAULT);
195         }
196         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
197         handle->lgh_last_idx = le32_to_cpu(handle->lgh_hdr->llh_tail.lrt_index);
198
199 out:
200         if (req)
201                 ptlrpc_req_finished(req);
202         RETURN(rc);
203 }
204
205 static int llog_client_close(struct llog_handle *handle)
206 {
207         int rc = 0;
208
209         RETURN(rc);
210 }
211
212
213 struct llog_operations llog_client_ops = {
214         lop_next_block:  llog_client_next_block,
215         lop_read_header: llog_client_read_header,
216         lop_create:      llog_client_create,
217         lop_close:       llog_client_close,
218 };