Whamcloud - gitweb
742b54588de25c1f7f88a2b6ec29099f7bd31a95
[fs/lustre-release.git] / lustre / ptlrpc / llog_server.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 - server side
23  *
24  */
25
26 #define DEBUG_SUBSYSTEM S_LOG
27
28 #ifndef EXPORT_SYMTAB
29 #define EXPORT_SYMTAB
30 #endif
31
32 #include <linux/fs.h>
33 #include <linux/obd_class.h>
34 #include <linux/lustre_log.h>
35 #include <linux/lustre_net.h>
36 #include <portals/list.h>
37
38 int llog_origin_handle_create(struct ptlrpc_request *req)
39 {
40         struct obd_export *exp = req->rq_export;
41         struct obd_device *obd = exp->exp_obd;
42         struct obd_device *disk_obd;
43         struct llog_handle  *loghandle;
44         struct llogd_body *body;
45         struct obd_run_ctxt saved;
46         struct llog_logid *logid = NULL;
47         struct llog_ctxt *ctxt;
48         char * name = NULL;
49         int size = sizeof (*body);
50         int rc, rc2;
51         ENTRY;
52
53         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
54                                  lustre_swab_llogd_body);
55         if (body == NULL) {
56                 CERROR ("Can't unpack llogd_body\n");
57                 GOTO(out, rc =-EFAULT);
58         }
59
60         if (body->lgd_logid.lgl_oid > 0)
61                 logid = &body->lgd_logid;
62
63         if (req->rq_reqmsg->bufcount > 1) {
64                 name = lustre_msg_string(req->rq_reqmsg, 1, 0);
65                 if (name == NULL) {
66                         CERROR("Can't unpack name\n");
67                         GOTO(out, rc = -EFAULT);
68                 }
69         }
70
71         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
72         LASSERT(ctxt != NULL);
73         disk_obd = ctxt->loc_exp->exp_obd;
74         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
75
76         rc = llog_create(ctxt, &loghandle, logid, name);
77         if (rc)
78                 GOTO(out_pop, rc);
79
80         rc = lustre_pack_reply(req, 1, &size, NULL);
81         if (rc)
82                 GOTO(out_close, rc = -ENOMEM);
83
84         body = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*body));
85         body->lgd_logid = loghandle->lgh_id;
86
87 out_close:
88         rc2 = llog_close(loghandle);
89         if (!rc)
90                 rc = rc2;
91 out_pop:
92         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
93 out:
94         RETURN(rc);
95 }
96
97 int llog_origin_handle_next_block(struct ptlrpc_request *req)
98 {
99         struct obd_export *exp = req->rq_export;
100         struct obd_device *obd = exp->exp_obd;
101         struct obd_device *disk_obd;
102         struct llog_handle  *loghandle;
103         struct llogd_body *body;
104         struct obd_run_ctxt saved;
105         struct llog_ctxt *ctxt;
106         __u32 flags;
107         __u8 *buf;
108         void * ptr;
109         int size[] = {sizeof (*body),
110                       LLOG_CHUNK_SIZE};
111         int rc, rc2;
112         ENTRY;
113
114         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
115                                   lustre_swab_llogd_body);
116         if (body == NULL) {
117                 CERROR ("Can't unpack llogd_body\n");
118                 GOTO(out, rc =-EFAULT);
119         }
120
121         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
122         if (!buf)
123                 GOTO(out, rc = -ENOMEM);
124
125         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
126         LASSERT(ctxt != NULL);
127         disk_obd = ctxt->loc_exp->exp_obd;
128         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
129
130         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
131         if (rc)
132                 GOTO(out_pop, rc);
133
134         flags = body->lgd_llh_flags;
135         rc = llog_init_handle(loghandle, flags, NULL);
136         if (rc)
137                 GOTO(out_close, rc);
138
139         memset(buf, 0, LLOG_CHUNK_SIZE);
140         rc = llog_next_block(loghandle, &body->lgd_saved_index,
141                              body->lgd_index,
142                              &body->lgd_cur_offset, buf, LLOG_CHUNK_SIZE);
143         if (rc)
144                 GOTO(out_close, rc);
145
146
147         rc = lustre_pack_reply(req, 2, size, NULL);
148         if (rc)
149                 GOTO(out_close, rc = -ENOMEM);
150
151         ptr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (body));
152         memcpy(ptr, body, sizeof(*body));
153
154         ptr = lustre_msg_buf(req->rq_repmsg, 1, LLOG_CHUNK_SIZE);
155         memcpy(ptr, buf, LLOG_CHUNK_SIZE);
156
157 out_close:
158         rc2 = llog_close(loghandle);
159         if (!rc)
160                 rc = rc2;
161
162 out_pop:
163         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
164         OBD_FREE(buf, LLOG_CHUNK_SIZE);
165 out:
166         RETURN(rc);
167 }
168
169 int llog_origin_handle_read_header(struct ptlrpc_request *req)
170 {
171         struct obd_export *exp = req->rq_export;
172         struct obd_device *obd = exp->exp_obd;
173         struct obd_device *disk_obd;
174         struct llog_handle  *loghandle;
175         struct llogd_body *body;
176         struct llog_log_hdr *hdr;
177         struct obd_run_ctxt saved;
178         struct llog_ctxt *ctxt;
179         __u32 flags;
180         __u8 *buf;
181         int size[] = {sizeof (*hdr)};
182         int rc, rc2;
183         ENTRY;
184
185         body = lustre_swab_reqbuf(req, 0, sizeof(*body),
186                                   lustre_swab_llogd_body);
187         if (body == NULL) {
188                 CERROR ("Can't unpack llogd_body\n");
189                 GOTO(out, rc =-EFAULT);
190         }
191
192         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
193         if (!buf)
194                 GOTO(out, rc = -ENOMEM);
195
196         ctxt = llog_get_context(obd, body->lgd_ctxt_idx);
197         LASSERT(ctxt != NULL);
198         disk_obd = ctxt->loc_exp->exp_obd;
199         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
200
201         rc = llog_create(ctxt, &loghandle, &body->lgd_logid, NULL);
202         if (rc)
203                 GOTO(out_pop, rc);
204
205         /* init_handle reads the header */
206         flags = body->lgd_llh_flags;
207         rc = llog_init_handle(loghandle, flags, NULL);
208         if (rc)
209                 GOTO(out_close, rc);
210
211
212         rc = lustre_pack_reply(req, 1, size, NULL);
213         if (rc)
214                 GOTO(out_close, rc = -ENOMEM);
215
216         hdr = lustre_msg_buf(req->rq_repmsg, 0, sizeof (*hdr));
217         memcpy(hdr, loghandle->lgh_hdr, sizeof(*hdr));
218
219 out_close:
220         rc2 = llog_close(loghandle);
221         if (!rc)
222                 rc = rc2;
223
224 out_pop:
225         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
226         OBD_FREE(buf, LLOG_CHUNK_SIZE);
227
228 out:
229         RETURN(rc);
230 }
231
232 int llog_origin_handle_close(struct ptlrpc_request *req)
233 {
234         int rc;
235
236         rc = 0;
237
238         RETURN(rc);
239 }
240
241 #ifdef ENABLE_ORPHANS
242 int llog_origin_handle_cancel(struct ptlrpc_request *req)
243 {
244         struct obd_device *obd = req->rq_export->exp_obd;
245         struct obd_device *disk_obd;
246         struct llog_cookie *logcookies;
247         struct llog_ctxt *ctxt;
248         int num_cookies, rc = 0;
249         struct obd_run_ctxt saved;
250         struct llog_handle *cathandle;
251         ENTRY;
252
253         logcookies = lustre_msg_buf(req->rq_reqmsg, 0, sizeof(*logcookies));
254         num_cookies = req->rq_reqmsg->buflens[0]/sizeof(*logcookies);
255         if (logcookies == NULL || num_cookies == 0) {
256                 DEBUG_REQ(D_HA, req, "no cookies sent");
257                 RETURN(-EFAULT);
258         }
259
260         ctxt = llog_get_context(obd, logcookies->lgc_subsys);
261         if (ctxt == NULL) {
262                 CWARN("llog subsys not setup or already cleanup\n");
263                 RETURN(-ENOENT);
264         }
265         down(&ctxt->loc_sem);
266         disk_obd = ctxt->loc_exp->exp_obd;
267         cathandle = ctxt->loc_handle;
268         LASSERT(cathandle);
269
270         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
271         rc = llog_cat_cancel_records(cathandle, num_cookies, logcookies);
272         if (rc)
273                 CERROR("cancel %d llog-records failed: %d\n", num_cookies, rc);
274         else
275                 CWARN("cancel %d llog-records\n", num_cookies);
276
277         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
278         up(&ctxt->loc_sem);
279
280         RETURN(rc);
281 }
282 EXPORT_SYMBOL(llog_origin_handle_cancel);
283 #endif