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