Add the rt linux 4.1.3-rt3 as base
[kvmfornfv.git] / kernel / drivers / staging / lustre / 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, Intel Corporation.
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 #include "../../include/linux/libcfs/libcfs.h"
46
47 #include "../include/obd_class.h"
48 #include "../include/lustre_log.h"
49 #include "../include/lustre_net.h"
50 #include <linux/list.h>
51
52 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                            \
53         mutex_lock(&ctxt->loc_mutex);                        \
54         if (ctxt->loc_imp) {                                      \
55                 imp = class_import_get(ctxt->loc_imp);          \
56         } else {                                                      \
57                 CERROR("ctxt->loc_imp == NULL for context idx %d."    \
58                        "Unable to complete MDS/OSS recovery,"    \
59                        "but I'll try again next time.  Not fatal.\n", \
60                        ctxt->loc_idx);                          \
61                 imp = NULL;                                        \
62                 mutex_unlock(&ctxt->loc_mutex);            \
63                 return (-EINVAL);                                    \
64         }                                                            \
65         mutex_unlock(&ctxt->loc_mutex);                    \
66 } while (0)
67
68 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                              \
69         mutex_lock(&ctxt->loc_mutex);                        \
70         if (ctxt->loc_imp != imp)                                    \
71                 CWARN("loc_imp has changed from %p to %p\n",      \
72                        ctxt->loc_imp, imp);                        \
73         class_import_put(imp);                                  \
74         mutex_unlock(&ctxt->loc_mutex);                    \
75 } while (0)
76
77 /* This is a callback from the llog_* functions.
78  * Assumes caller has already pushed us into the kernel context. */
79 static int llog_client_open(const struct lu_env *env,
80                             struct llog_handle *lgh, struct llog_logid *logid,
81                             char *name, enum llog_open_param open_param)
82 {
83         struct obd_import     *imp;
84         struct llogd_body     *body;
85         struct llog_ctxt      *ctxt = lgh->lgh_ctxt;
86         struct ptlrpc_request *req = NULL;
87         int                 rc;
88
89         LLOG_CLIENT_ENTRY(ctxt, imp);
90
91         /* client cannot create llog */
92         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
93         LASSERT(lgh);
94
95         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
96         if (req == NULL) {
97                 rc = -ENOMEM;
98                 goto out;
99         }
100
101         if (name)
102                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
103                                      strlen(name) + 1);
104
105         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
106                                  LLOG_ORIGIN_HANDLE_CREATE);
107         if (rc) {
108                 ptlrpc_request_free(req);
109                 req = NULL;
110                 goto out;
111         }
112         ptlrpc_request_set_replen(req);
113
114         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
115         if (logid)
116                 body->lgd_logid = *logid;
117         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
118
119         if (name) {
120                 char *tmp;
121                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
122                                                    strlen(name) + 1);
123                 LASSERT(tmp);
124                 strcpy(tmp, name);
125         }
126
127         rc = ptlrpc_queue_wait(req);
128         if (rc)
129                 goto out;
130
131         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
132         if (body == NULL) {
133                 rc = -EFAULT;
134                 goto out;
135         }
136
137         lgh->lgh_id = body->lgd_logid;
138         lgh->lgh_ctxt = ctxt;
139 out:
140         LLOG_CLIENT_EXIT(ctxt, imp);
141         ptlrpc_req_finished(req);
142         return rc;
143 }
144
145 static int llog_client_destroy(const struct lu_env *env,
146                                struct llog_handle *loghandle)
147 {
148         struct obd_import     *imp;
149         struct ptlrpc_request *req = NULL;
150         struct llogd_body     *body;
151         int                 rc;
152
153         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
154         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_DESTROY,
155                                         LUSTRE_LOG_VERSION,
156                                         LLOG_ORIGIN_HANDLE_DESTROY);
157         if (req == NULL) {
158                 rc = -ENOMEM;
159                 goto err_exit;
160         }
161
162         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
163         body->lgd_logid = loghandle->lgh_id;
164         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
165
166         if (!(body->lgd_llh_flags & LLOG_F_IS_PLAIN))
167                 CERROR("%s: wrong llog flags %x\n", imp->imp_obd->obd_name,
168                        body->lgd_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(const struct lu_env *env,
181                                   struct llog_handle *loghandle,
182                                   int *cur_idx, int next_idx,
183                                   __u64 *cur_offset, void *buf, int len)
184 {
185         struct obd_import     *imp;
186         struct ptlrpc_request *req = NULL;
187         struct llogd_body     *body;
188         void              *ptr;
189         int                 rc;
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                 rc = -ENOMEM;
197                 goto err_exit;
198         }
199
200         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
201         body->lgd_logid = loghandle->lgh_id;
202         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
203         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
204         body->lgd_index = next_idx;
205         body->lgd_saved_index = *cur_idx;
206         body->lgd_len = len;
207         body->lgd_cur_offset = *cur_offset;
208
209         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
210         ptlrpc_request_set_replen(req);
211         rc = ptlrpc_queue_wait(req);
212         if (rc)
213                 goto out;
214
215         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
216         if (body == NULL) {
217                 rc = -EFAULT;
218                 goto out;
219         }
220
221         /* The log records are swabbed as they are processed */
222         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
223         if (ptr == NULL) {
224                 rc = -EFAULT;
225                 goto out;
226         }
227
228         *cur_idx = body->lgd_saved_index;
229         *cur_offset = body->lgd_cur_offset;
230
231         memcpy(buf, ptr, len);
232 out:
233         ptlrpc_req_finished(req);
234 err_exit:
235         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
236         return rc;
237 }
238
239 static int llog_client_prev_block(const struct lu_env *env,
240                                   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
249         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
250         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
251                                         LUSTRE_LOG_VERSION,
252                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
253         if (req == NULL) {
254                 rc = -ENOMEM;
255                 goto err_exit;
256         }
257
258         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
259         body->lgd_logid = loghandle->lgh_id;
260         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
261         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
262         body->lgd_index = prev_idx;
263         body->lgd_len = len;
264
265         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
266         ptlrpc_request_set_replen(req);
267
268         rc = ptlrpc_queue_wait(req);
269         if (rc)
270                 goto out;
271
272         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
273         if (body == NULL) {
274                 rc = -EFAULT;
275                 goto out;
276         }
277
278         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
279         if (ptr == NULL) {
280                 rc = -EFAULT;
281                 goto out;
282         }
283
284         memcpy(buf, ptr, len);
285 out:
286         ptlrpc_req_finished(req);
287 err_exit:
288         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
289         return rc;
290 }
291
292 static int llog_client_read_header(const struct lu_env *env,
293                                    struct llog_handle *handle)
294 {
295         struct obd_import     *imp;
296         struct ptlrpc_request *req = NULL;
297         struct llogd_body     *body;
298         struct llog_log_hdr   *hdr;
299         struct llog_rec_hdr   *llh_hdr;
300         int                 rc;
301
302         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
303         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
304                                         LUSTRE_LOG_VERSION,
305                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
306         if (req == NULL) {
307                 rc = -ENOMEM;
308                 goto err_exit;
309         }
310
311         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
312         body->lgd_logid = handle->lgh_id;
313         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
314         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
315
316         ptlrpc_request_set_replen(req);
317         rc = ptlrpc_queue_wait(req);
318         if (rc)
319                 goto out;
320
321         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
322         if (hdr == NULL) {
323                 rc = -EFAULT;
324                 goto out;
325         }
326
327         memcpy(handle->lgh_hdr, hdr, sizeof(*hdr));
328         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
329
330         /* sanity checks */
331         llh_hdr = &handle->lgh_hdr->llh_hdr;
332         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
333                 CERROR("bad log header magic: %#x (expecting %#x)\n",
334                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
335                 rc = -EIO;
336         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
337                 CERROR("incorrectly sized log header: %#x (expecting %#x)\n",
338                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
339                 CERROR("you may need to re-run lconf --write_conf.\n");
340                 rc = -EIO;
341         }
342 out:
343         ptlrpc_req_finished(req);
344 err_exit:
345         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
346         return rc;
347 }
348
349 static int llog_client_close(const struct lu_env *env,
350                              struct llog_handle *handle)
351 {
352         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
353            the servers all close the file at the end of every
354            other LLOG_ RPC. */
355         return 0;
356 }
357
358 struct llog_operations llog_client_ops = {
359         .lop_next_block         = llog_client_next_block,
360         .lop_prev_block         = llog_client_prev_block,
361         .lop_read_header        = llog_client_read_header,
362         .lop_open               = llog_client_open,
363         .lop_destroy            = llog_client_destroy,
364         .lop_close              = llog_client_close,
365 };
366 EXPORT_SYMBOL(llog_client_ops);