Whamcloud - gitweb
00eec031ae0329c9b237d0edfe025a8dd34aae72
[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 <linux/fs.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <linux/obd_class.h>
42 #include <linux/lustre_log.h>
43 #include <linux/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* fsname ,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(req_body)};
57         char *tmp[3] = {(char*) &req_body};
58         int bufcount = 1;
59         int repsize[] = {sizeof (req_body)};
60         int rc;
61         ENTRY;
62
63         if (ctxt->loc_imp == NULL) {
64                 /* This used to be an assert; bug 6200 */
65                 CERROR("ctxt->loc_imp == NULL for context idx %d.  Unable to "
66                        "complete MDS/OSS recovery, but I'll try again next "
67                        "time.  Not fatal.\n", ctxt->loc_idx);
68                 RETURN(-EINVAL);
69         }
70         imp = ctxt->loc_imp;
71
72         handle = llog_alloc_handle();
73         if (handle == NULL)
74                 RETURN(-ENOMEM);
75         *res = handle;
76
77         memset(&req_body, 0, sizeof(req_body));
78         if (logid)
79                 req_body.lgd_logid = *logid;
80         req_body.lgd_ctxt_idx = ctxt->loc_idx - 1;
81
82         if (fsname) {
83                 size[bufcount] = strlen(fsname) + 1;
84                 tmp[bufcount] = fsname;
85                 bufcount++;
86         }
87
88         if (name) {
89                 size[bufcount] = strlen(name) + 1;
90                 tmp[bufcount] = name;
91                 bufcount++;
92         }
93
94         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_CREATE, 
95                               bufcount, size, tmp);
96         if (!req)
97                 GOTO(err_free, rc = -ENOMEM);
98
99         req->rq_replen = lustre_msg_size(1, repsize);
100         rc = ptlrpc_queue_wait(req);
101         if (rc)
102                 GOTO(err_free, rc);
103
104         body = lustre_swab_repbuf(req, 0, sizeof(*body),
105                                  lustre_swab_llogd_body);
106         if (body == NULL) {
107                 CERROR ("Can't unpack llogd_body\n");
108                 GOTO(err_free, rc =-EFAULT);
109         }
110
111         handle->lgh_id = body->lgd_logid;
112         handle->lgh_ctxt = ctxt;
113
114 out:
115         if (req)
116                 ptlrpc_req_finished(req);
117         RETURN(rc);
118
119 err_free:
120         llog_free_handle(handle);
121         goto out;
122 }
123
124
125 static int llog_client_next_block(struct llog_handle *loghandle,
126                                   int *cur_idx, int next_idx,
127                                   __u64 *cur_offset, void *buf, int len)
128 {
129         struct obd_import *imp = loghandle->lgh_ctxt->loc_imp;
130         struct ptlrpc_request *req = NULL;
131         struct llogd_body *body;
132         void * ptr;
133         int size = sizeof(*body);
134         int repsize[2] = {sizeof (*body)};
135         int rc;
136         ENTRY;
137
138         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_NEXT_BLOCK, 1,&size,NULL);
139         if (!req)
140                 GOTO(out, rc = -ENOMEM);
141
142         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
143         body->lgd_logid = loghandle->lgh_id;
144         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
145         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
146         body->lgd_index = next_idx;
147         body->lgd_saved_index = *cur_idx;
148         body->lgd_len = len;
149         body->lgd_cur_offset = *cur_offset;
150         repsize[1] = len;
151
152         req->rq_replen = lustre_msg_size(2, repsize);
153         rc = ptlrpc_queue_wait(req);
154         if (rc)
155                 GOTO(out, rc);
156
157         body = lustre_swab_repbuf(req, 0, sizeof(*body),
158                                  lustre_swab_llogd_body);
159         if (body == NULL) {
160                 CERROR ("Can't unpack llogd_body\n");
161                 GOTO(out, rc =-EFAULT);
162         }
163
164         /* The log records are swabbed as they are processed */
165         ptr = lustre_msg_buf(req->rq_repmsg, 1, len);
166         if (ptr == NULL) {
167                 CERROR ("Can't unpack bitmap\n");
168                 GOTO(out, rc =-EFAULT);
169         }
170
171         *cur_idx = body->lgd_saved_index;
172         *cur_offset = body->lgd_cur_offset;
173
174         memcpy(buf, ptr, len);
175
176 out:
177         if (req)
178                 ptlrpc_req_finished(req);
179         RETURN(rc);
180 }
181
182
183 static int llog_client_read_header(struct llog_handle *handle)
184 {
185         struct obd_import *imp = handle->lgh_ctxt->loc_imp;
186         struct ptlrpc_request *req = NULL;
187         struct llogd_body *body;
188         struct llog_log_hdr *hdr;
189         struct llog_rec_hdr *llh_hdr;
190         int size = sizeof(*body);
191         int repsize = sizeof (*hdr);
192         int rc;
193         ENTRY;
194
195         req = ptlrpc_prep_req(imp, LLOG_ORIGIN_HANDLE_READ_HEADER,1,&size,NULL);
196         if (!req)
197                 GOTO(out, rc = -ENOMEM);
198
199         body = lustre_msg_buf(req->rq_reqmsg, 0, sizeof (*body));
200         body->lgd_logid = handle->lgh_id;
201         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
202         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
203
204         req->rq_replen = lustre_msg_size(1, &repsize);
205         rc = ptlrpc_queue_wait(req);
206         if (rc)
207                 GOTO(out, rc);
208
209         hdr = lustre_swab_repbuf(req, 0, sizeof(*hdr), lustre_swab_llog_hdr);
210         if (hdr == NULL) {
211                 CERROR ("Can't unpack llog_hdr\n");
212                 GOTO(out, rc =-EFAULT);
213         }
214
215         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
216         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
217
218         /* sanity checks */
219         llh_hdr = &handle->lgh_hdr->llh_hdr;
220         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
221                 CERROR("bad log header magic: %#x (expecting %#x)\n",
222                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
223                 rc = -EIO;
224         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
225                 CERROR("incorrectly sized log header: %#x "
226                        "(expecting %#x)\n",
227                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
228                 CERROR("you may need to re-run lconf --write_conf.\n");
229                 rc = -EIO;
230         }
231
232 out:
233         if (req)
234                 ptlrpc_req_finished(req);
235         RETURN(rc);
236 }
237
238 static int llog_client_close(struct llog_handle *handle)
239 {
240         int rc = 0;
241
242         RETURN(rc);
243 }
244
245
246 struct llog_operations llog_client_ops = {
247         lop_next_block:  llog_client_next_block,
248         lop_read_header: llog_client_read_header,
249         lop_create:      llog_client_create,
250         lop_close:       llog_client_close,
251 };