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