Whamcloud - gitweb
LU-1302 llog: pass lu_env as parametr in llog functions
[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(const struct lu_env *env, struct llog_ctxt *ctxt,
84                               struct llog_handle **res,
85                               struct llog_logid *logid, char *name)
86 {
87         struct obd_import     *imp;
88         struct llogd_body     *body;
89         struct llog_handle    *handle;
90         struct ptlrpc_request *req = NULL;
91         int                    rc;
92         ENTRY;
93
94         LLOG_CLIENT_ENTRY(ctxt, imp);
95
96         handle = llog_alloc_handle();
97         if (handle == NULL)
98                 RETURN(-ENOMEM);
99         *res = handle;
100
101         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
102         if (req == NULL)
103                 GOTO(err_free, rc = -ENOMEM);
104
105         if (name)
106                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
107                                      strlen(name) + 1);
108
109         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
110                                  LLOG_ORIGIN_HANDLE_CREATE);
111         if (rc) {
112                 ptlrpc_request_free(req);
113                 req = NULL;
114                 GOTO(err_free, rc);
115         }
116         ptlrpc_request_set_replen(req);
117
118         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
119         if (logid)
120                 body->lgd_logid = *logid;
121         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
122
123         if (name) {
124                 char *tmp;
125                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
126                                                    strlen(name) + 1);
127                 LASSERT(tmp);
128                 strcpy(tmp, name);
129         }
130
131         rc = ptlrpc_queue_wait(req);
132         if (rc)
133                 GOTO(err_free, rc);
134
135         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
136         if (body == NULL)
137                 GOTO(err_free, rc =-EFAULT);
138
139         handle->lgh_id = body->lgd_logid;
140         handle->lgh_ctxt = ctxt;
141         EXIT;
142 out:
143         LLOG_CLIENT_EXIT(ctxt, imp);
144         ptlrpc_req_finished(req);
145         return rc;
146 err_free:
147         *res = NULL;
148         llog_free_handle(handle);
149         goto out;
150 }
151
152 static int llog_client_destroy(const struct lu_env *env,
153                                struct llog_handle *loghandle)
154 {
155         struct obd_import     *imp;
156         struct ptlrpc_request *req = NULL;
157         struct llogd_body     *body;
158         int                    rc;
159         ENTRY;
160
161         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
162         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
163                                         LUSTRE_LOG_VERSION,
164                                         LLOG_ORIGIN_HANDLE_DESTROY);
165         if (req == NULL)
166                 GOTO(err_exit, rc =-ENOMEM);
167
168         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
169         body->lgd_logid = loghandle->lgh_id;
170         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
171
172         ptlrpc_request_set_replen(req);
173         rc = ptlrpc_queue_wait(req);
174
175         ptlrpc_req_finished(req);
176 err_exit:
177         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
178         RETURN(rc);
179 }
180
181
182 static int llog_client_next_block(const struct lu_env *env,
183                                   struct llog_handle *loghandle,
184                                   int *cur_idx, int next_idx,
185                                   __u64 *cur_offset, void *buf, int len)
186 {
187         struct obd_import     *imp;
188         struct ptlrpc_request *req = NULL;
189         struct llogd_body     *body;
190         void                  *ptr;
191         int                    rc;
192         ENTRY;
193
194         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
195         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
196                                         LUSTRE_LOG_VERSION,
197                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
198         if (req == NULL)
199                 GOTO(err_exit, rc =-ENOMEM);
200
201         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
202         body->lgd_logid = loghandle->lgh_id;
203         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
204         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
205         body->lgd_index = next_idx;
206         body->lgd_saved_index = *cur_idx;
207         body->lgd_len = len;
208         body->lgd_cur_offset = *cur_offset;
209
210         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
211         ptlrpc_request_set_replen(req);
212         rc = ptlrpc_queue_wait(req);
213         if (rc)
214                 GOTO(out, rc);
215
216         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
217         if (body == NULL)
218                 GOTO(out, rc =-EFAULT);
219
220         /* The log records are swabbed as they are processed */
221         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
222         if (ptr == NULL)
223                 GOTO(out, rc =-EFAULT);
224
225         *cur_idx = body->lgd_saved_index;
226         *cur_offset = body->lgd_cur_offset;
227
228         memcpy(buf, ptr, len);
229         EXIT;
230 out:
231         ptlrpc_req_finished(req);
232 err_exit:
233         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
234         return rc;
235 }
236
237 static int llog_client_prev_block(const struct lu_env *env,
238                                   struct llog_handle *loghandle,
239                                   int prev_idx, void *buf, int len)
240 {
241         struct obd_import     *imp;
242         struct ptlrpc_request *req = NULL;
243         struct llogd_body     *body;
244         void                  *ptr;
245         int                    rc;
246         ENTRY;
247
248         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
249         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
250                                         LUSTRE_LOG_VERSION,
251                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
252         if (req == NULL)
253                 GOTO(err_exit, rc = -ENOMEM);
254
255         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
256         body->lgd_logid = loghandle->lgh_id;
257         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
258         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
259         body->lgd_index = prev_idx;
260         body->lgd_len = len;
261
262         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
263         ptlrpc_request_set_replen(req);
264
265         rc = ptlrpc_queue_wait(req);
266         if (rc)
267                 GOTO(out, rc);
268
269         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
270         if (body == NULL)
271                 GOTO(out, rc =-EFAULT);
272
273         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
274         if (ptr == NULL)
275                 GOTO(out, rc =-EFAULT);
276
277         memcpy(buf, ptr, len);
278         EXIT;
279 out:
280         ptlrpc_req_finished(req);
281 err_exit:
282         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
283         return rc;
284 }
285
286 static int llog_client_read_header(const struct lu_env *env,
287                                    struct llog_handle *handle)
288 {
289         struct obd_import     *imp;
290         struct ptlrpc_request *req = NULL;
291         struct llogd_body     *body;
292         struct llog_log_hdr   *hdr;
293         struct llog_rec_hdr   *llh_hdr;
294         int                    rc;
295         ENTRY;
296
297         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
298         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
299                                         LUSTRE_LOG_VERSION,
300                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
301         if (req == NULL)
302                 GOTO(err_exit, rc = -ENOMEM);
303
304         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
305         body->lgd_logid = handle->lgh_id;
306         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
307         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
308
309         ptlrpc_request_set_replen(req);
310         rc = ptlrpc_queue_wait(req);
311         if (rc)
312                 GOTO(out, rc);
313
314         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
315         if (hdr == NULL)
316                 GOTO(out, rc =-EFAULT);
317
318         memcpy(handle->lgh_hdr, hdr, sizeof (*hdr));
319         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
320
321         /* sanity checks */
322         llh_hdr = &handle->lgh_hdr->llh_hdr;
323         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
324                 CERROR("bad log header magic: %#x (expecting %#x)\n",
325                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
326                 rc = -EIO;
327         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
328                 CERROR("incorrectly sized log header: %#x "
329                        "(expecting %#x)\n",
330                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
331                 CERROR("you may need to re-run lconf --write_conf.\n");
332                 rc = -EIO;
333         }
334         EXIT;
335 out:
336         ptlrpc_req_finished(req);
337 err_exit:
338         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
339         return rc;
340 }
341
342 static int llog_client_close(const struct lu_env *env,
343                              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 };
360 EXPORT_SYMBOL(llog_client_ops);