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