Whamcloud - gitweb
793c061200aca4fe5de9243f787b3714e1f4a8f2
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2012, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/ptlrpc/llog_client.c
39  *
40  * remote api for llog - client side
41  *
42  * Author: Andreas Dilger <adilger@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47 #ifndef EXPORT_SYMTAB
48 #define EXPORT_SYMTAB
49 #endif
50
51 #ifdef __KERNEL__
52 #include <libcfs/libcfs.h>
53 #else
54 #include <liblustre.h>
55 #endif
56
57 #include <obd_class.h>
58 #include <lustre_log.h>
59 #include <lustre_net.h>
60 #include <libcfs/list.h>
61
62 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                             \
63         cfs_mutex_down(&ctxt->loc_sem);                               \
64         if (ctxt->loc_imp) {                                          \
65                 imp = class_import_get(ctxt->loc_imp);                \
66         } else {                                                      \
67                 CERROR("ctxt->loc_imp == NULL for context idx %d."    \
68                        "Unable to complete MDS/OSS recovery,"         \
69                        "but I'll try again next time.  Not fatal.\n", \
70                        ctxt->loc_idx);                                \
71                 imp = NULL;                                           \
72                 cfs_mutex_up(&ctxt->loc_sem);                         \
73                 return (-EINVAL);                                     \
74         }                                                             \
75         cfs_mutex_up(&ctxt->loc_sem);                                 \
76 } while(0)
77
78 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
79         cfs_mutex_down(&ctxt->loc_sem);                               \
80         if (ctxt->loc_imp != imp)                                     \
81                 CWARN("loc_imp has changed from %p to %p\n",          \
82                        ctxt->loc_imp, imp);                           \
83         class_import_put(imp);                                        \
84         cfs_mutex_up(&ctxt->loc_sem);                                 \
85 } while(0)
86
87 /* This is a callback from the llog_* functions.
88  * Assumes caller has already pushed us into the kernel context. */
89 static int llog_client_create(struct llog_ctxt *ctxt, struct llog_handle **res,
90                               struct llog_logid *logid, char *name)
91 {
92         struct obd_import     *imp;
93         struct llogd_body     *body;
94         struct llog_handle    *handle;
95         struct ptlrpc_request *req = NULL;
96         int                    rc;
97         ENTRY;
98
99         LLOG_CLIENT_ENTRY(ctxt, imp);
100
101         handle = llog_alloc_handle();
102         if (handle == NULL)
103                 RETURN(-ENOMEM);
104         *res = handle;
105
106         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
107         if (req == NULL)
108                 GOTO(err_free, rc = -ENOMEM);
109
110         if (name)
111                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
112                                      strlen(name) + 1);
113
114         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
115                                  LLOG_ORIGIN_HANDLE_CREATE);
116         if (rc) {
117                 ptlrpc_request_free(req);
118                 req = NULL;
119                 GOTO(err_free, rc);
120         }
121         ptlrpc_request_set_replen(req);
122
123         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
124         if (logid)
125                 body->lgd_logid = *logid;
126         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
127
128         if (name) {
129                 char *tmp;
130                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
131                                                    strlen(name) + 1);
132                 LASSERT(tmp);
133                 strcpy(tmp, name);
134         }
135
136         rc = ptlrpc_queue_wait(req);
137         if (rc)
138                 GOTO(err_free, rc);
139
140         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
141         if (body == NULL)
142                 GOTO(err_free, rc =-EFAULT);
143
144         handle->lgh_id = body->lgd_logid;
145         handle->lgh_ctxt = ctxt;
146         EXIT;
147 out:
148         LLOG_CLIENT_EXIT(ctxt, imp);
149         ptlrpc_req_finished(req);
150         return rc;
151 err_free:
152         *res = NULL;
153         llog_free_handle(handle);
154         goto out;
155 }
156
157 static int llog_client_destroy(struct llog_handle *loghandle)
158 {
159         struct obd_import     *imp;
160         struct ptlrpc_request *req = NULL;
161         struct llogd_body     *body;
162         int                    rc;
163         ENTRY;
164
165         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
166         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
167                                         LUSTRE_LOG_VERSION,
168                                         LLOG_ORIGIN_HANDLE_DESTROY);
169         if (req == NULL)
170                 GOTO(err_exit, rc =-ENOMEM);
171
172         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
173         body->lgd_logid = loghandle->lgh_id;
174         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
175
176         ptlrpc_request_set_replen(req);
177         rc = ptlrpc_queue_wait(req);
178         
179         ptlrpc_req_finished(req);
180 err_exit:
181         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
182         RETURN(rc);
183 }
184
185
186 static int llog_client_next_block(struct llog_handle *loghandle,
187                                   int *cur_idx, int next_idx,
188                                   __u64 *cur_offset, void *buf, int len)
189 {
190         struct obd_import     *imp;
191         struct ptlrpc_request *req = NULL;
192         struct llogd_body     *body;
193         void                  *ptr;
194         int                    rc;
195         ENTRY;
196
197         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
198         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
199                                         LUSTRE_LOG_VERSION,
200                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
201         if (req == NULL)
202                 GOTO(err_exit, rc =-ENOMEM);
203                 
204         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
205         body->lgd_logid = loghandle->lgh_id;
206         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
207         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
208         body->lgd_index = next_idx;
209         body->lgd_saved_index = *cur_idx;
210         body->lgd_len = len;
211         body->lgd_cur_offset = *cur_offset;
212
213         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
214         ptlrpc_request_set_replen(req);
215         rc = ptlrpc_queue_wait(req);
216         if (rc)
217                 GOTO(out, rc);
218
219         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
220         if (body == NULL)
221                 GOTO(out, rc =-EFAULT);
222
223         /* The log records are swabbed as they are processed */
224         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
225         if (ptr == NULL)
226                 GOTO(out, rc =-EFAULT);
227
228         *cur_idx = body->lgd_saved_index;
229         *cur_offset = body->lgd_cur_offset;
230
231         memcpy(buf, ptr, len);
232         EXIT;
233 out:
234         ptlrpc_req_finished(req);
235 err_exit:
236         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
237         return rc;
238 }
239
240 static int llog_client_prev_block(struct llog_handle *loghandle,
241                                   int prev_idx, void *buf, int len)
242 {
243         struct obd_import     *imp;
244         struct ptlrpc_request *req = NULL;
245         struct llogd_body     *body;
246         void                  *ptr;
247         int                    rc;
248         ENTRY;
249
250         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
251         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
252                                         LUSTRE_LOG_VERSION,
253                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
254         if (req == NULL)
255                 GOTO(err_exit, rc = -ENOMEM);
256
257         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
258         body->lgd_logid = loghandle->lgh_id;
259         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
260         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
261         body->lgd_index = prev_idx;
262         body->lgd_len = len;
263
264         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
265         ptlrpc_request_set_replen(req);
266
267         rc = ptlrpc_queue_wait(req);
268         if (rc)
269                 GOTO(out, rc);
270
271         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
272         if (body == NULL)
273                 GOTO(out, rc =-EFAULT);
274
275         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
276         if (ptr == NULL)
277                 GOTO(out, rc =-EFAULT);
278
279         memcpy(buf, ptr, len);
280         EXIT;
281 out:
282         ptlrpc_req_finished(req);
283 err_exit:
284         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
285         return rc;
286 }
287
288 static int llog_client_read_header(struct llog_handle *handle)
289 {
290         struct obd_import     *imp;
291         struct ptlrpc_request *req = NULL;
292         struct llogd_body     *body;
293         struct llog_log_hdr   *hdr;
294         struct llog_rec_hdr   *llh_hdr;
295         int                    rc;
296         ENTRY;
297
298         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
299         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
300                                         LUSTRE_LOG_VERSION,
301                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
302         if (req == NULL)
303                 GOTO(err_exit, rc = -ENOMEM);
304
305         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
306         body->lgd_logid = handle->lgh_id;
307         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
308         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
309
310         ptlrpc_request_set_replen(req);
311         rc = ptlrpc_queue_wait(req);
312         if (rc)
313                 GOTO(out, rc);
314
315         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
316         if (hdr == NULL)
317                 GOTO(out, rc =-EFAULT);
318
319         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
320         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
321
322         /* sanity checks */
323         llh_hdr = &handle->lgh_hdr->llh_hdr;
324         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
325                 CERROR("bad log header magic: %#x (expecting %#x)\n",
326                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
327                 rc = -EIO;
328         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
329                 CERROR("incorrectly sized log header: %#x "
330                        "(expecting %#x)\n",
331                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
332                 CERROR("you may need to re-run lconf --write_conf.\n");
333                 rc = -EIO;
334         }
335         EXIT;
336 out:
337         ptlrpc_req_finished(req);
338 err_exit:
339         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
340         return rc;
341 }
342
343 static int llog_client_close(struct llog_handle *handle)
344 {
345         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
346            the servers all close the file at the end of every
347            other LLOG_ RPC. */
348         return(0);
349 }
350
351
352 struct llog_operations llog_client_ops = {
353         lop_next_block:  llog_client_next_block,
354         lop_prev_block:  llog_client_prev_block,
355         lop_read_header: llog_client_read_header,
356         lop_create:      llog_client_create,
357         lop_destroy:     llog_client_destroy,
358         lop_close:       llog_client_close,
359 };