Merge tag 'gpio-v4.8-1' of git://git.kernel.org/pub/scm/linux/kernel/git/linusw/linux...
[cascardo/linux.git] / 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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2015, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/ptlrpc/llog_client.c
33  *
34  * remote api for llog - client side
35  *
36  * Author: Andreas Dilger <adilger@clusterfs.com>
37  */
38
39 #define DEBUG_SUBSYSTEM S_LOG
40
41 #include "../../include/linux/libcfs/libcfs.h"
42
43 #include "../include/obd_class.h"
44 #include "../include/lustre_log.h"
45 #include "../include/lustre_net.h"
46 #include <linux/list.h>
47
48 #define LLOG_CLIENT_ENTRY(ctxt, imp) do {                               \
49         mutex_lock(&ctxt->loc_mutex);                                   \
50         if (ctxt->loc_imp) {                                            \
51                 imp = class_import_get(ctxt->loc_imp);                  \
52         } else {                                                        \
53                 CERROR("ctxt->loc_imp == NULL for context idx %d."      \
54                        "Unable to complete MDS/OSS recovery,"           \
55                        "but I'll try again next time.  Not fatal.\n",   \
56                        ctxt->loc_idx);                                  \
57                 imp = NULL;                                             \
58                 mutex_unlock(&ctxt->loc_mutex);                         \
59                 return (-EINVAL);                                       \
60         }                                                               \
61         mutex_unlock(&ctxt->loc_mutex);                                 \
62 } while (0)
63
64 #define LLOG_CLIENT_EXIT(ctxt, imp) do {                                \
65         mutex_lock(&ctxt->loc_mutex);                                   \
66         if (ctxt->loc_imp != imp)                                       \
67                 CWARN("loc_imp has changed from %p to %p\n",            \
68                        ctxt->loc_imp, imp);                             \
69         class_import_put(imp);                                          \
70         mutex_unlock(&ctxt->loc_mutex);                                 \
71 } while (0)
72
73 /* This is a callback from the llog_* functions.
74  * Assumes caller has already pushed us into the kernel context.
75  */
76 static int llog_client_open(const struct lu_env *env,
77                             struct llog_handle *lgh, struct llog_logid *logid,
78                             char *name, enum llog_open_param open_param)
79 {
80         struct obd_import *imp;
81         struct llogd_body *body;
82         struct llog_ctxt *ctxt = lgh->lgh_ctxt;
83         struct ptlrpc_request *req = NULL;
84         int rc;
85
86         LLOG_CLIENT_ENTRY(ctxt, imp);
87
88         /* client cannot create llog */
89         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
90         LASSERT(lgh);
91
92         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
93         if (!req) {
94                 rc = -ENOMEM;
95                 goto out;
96         }
97
98         if (name)
99                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
100                                      strlen(name) + 1);
101
102         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
103                                  LLOG_ORIGIN_HANDLE_CREATE);
104         if (rc) {
105                 ptlrpc_request_free(req);
106                 req = NULL;
107                 goto out;
108         }
109         ptlrpc_request_set_replen(req);
110
111         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
112         if (logid)
113                 body->lgd_logid = *logid;
114         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
115
116         if (name) {
117                 char *tmp;
118
119                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
120                                                    strlen(name) + 1);
121                 LASSERT(tmp);
122                 strcpy(tmp, name);
123         }
124
125         rc = ptlrpc_queue_wait(req);
126         if (rc)
127                 goto out;
128
129         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
130         if (!body) {
131                 rc = -EFAULT;
132                 goto out;
133         }
134
135         lgh->lgh_id = body->lgd_logid;
136         lgh->lgh_ctxt = ctxt;
137 out:
138         LLOG_CLIENT_EXIT(ctxt, imp);
139         ptlrpc_req_finished(req);
140         return rc;
141 }
142
143 static int llog_client_next_block(const struct lu_env *env,
144                                   struct llog_handle *loghandle,
145                                   int *cur_idx, int next_idx,
146                                   __u64 *cur_offset, void *buf, int len)
147 {
148         struct obd_import *imp;
149         struct ptlrpc_request *req = NULL;
150         struct llogd_body *body;
151         void *ptr;
152         int rc;
153
154         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
155         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
156                                         LUSTRE_LOG_VERSION,
157                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
158         if (!req) {
159                 rc = -ENOMEM;
160                 goto err_exit;
161         }
162
163         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
164         body->lgd_logid = loghandle->lgh_id;
165         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
166         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
167         body->lgd_index = next_idx;
168         body->lgd_saved_index = *cur_idx;
169         body->lgd_len = len;
170         body->lgd_cur_offset = *cur_offset;
171
172         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
173         ptlrpc_request_set_replen(req);
174         rc = ptlrpc_queue_wait(req);
175         if (rc)
176                 goto out;
177
178         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
179         if (!body) {
180                 rc = -EFAULT;
181                 goto out;
182         }
183
184         /* The log records are swabbed as they are processed */
185         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
186         if (!ptr) {
187                 rc = -EFAULT;
188                 goto out;
189         }
190
191         *cur_idx = body->lgd_saved_index;
192         *cur_offset = body->lgd_cur_offset;
193
194         memcpy(buf, ptr, len);
195 out:
196         ptlrpc_req_finished(req);
197 err_exit:
198         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
199         return rc;
200 }
201
202 static int llog_client_prev_block(const struct lu_env *env,
203                                   struct llog_handle *loghandle,
204                                   int prev_idx, void *buf, int len)
205 {
206         struct obd_import *imp;
207         struct ptlrpc_request *req = NULL;
208         struct llogd_body *body;
209         void *ptr;
210         int rc;
211
212         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
213         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
214                                         LUSTRE_LOG_VERSION,
215                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
216         if (!req) {
217                 rc = -ENOMEM;
218                 goto err_exit;
219         }
220
221         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
222         body->lgd_logid = loghandle->lgh_id;
223         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
224         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
225         body->lgd_index = prev_idx;
226         body->lgd_len = len;
227
228         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
229         ptlrpc_request_set_replen(req);
230
231         rc = ptlrpc_queue_wait(req);
232         if (rc)
233                 goto out;
234
235         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
236         if (!body) {
237                 rc = -EFAULT;
238                 goto out;
239         }
240
241         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
242         if (!ptr) {
243                 rc = -EFAULT;
244                 goto out;
245         }
246
247         memcpy(buf, ptr, len);
248 out:
249         ptlrpc_req_finished(req);
250 err_exit:
251         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
252         return rc;
253 }
254
255 static int llog_client_read_header(const struct lu_env *env,
256                                    struct llog_handle *handle)
257 {
258         struct obd_import *imp;
259         struct ptlrpc_request *req = NULL;
260         struct llogd_body *body;
261         struct llog_log_hdr *hdr;
262         struct llog_rec_hdr *llh_hdr;
263         int rc;
264
265         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
266         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
267                                         LUSTRE_LOG_VERSION,
268                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
269         if (!req) {
270                 rc = -ENOMEM;
271                 goto err_exit;
272         }
273
274         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
275         body->lgd_logid = handle->lgh_id;
276         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
277         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
278
279         ptlrpc_request_set_replen(req);
280         rc = ptlrpc_queue_wait(req);
281         if (rc)
282                 goto out;
283
284         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
285         if (!hdr) {
286                 rc = -EFAULT;
287                 goto out;
288         }
289
290         memcpy(handle->lgh_hdr, hdr, sizeof(*hdr));
291         handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
292
293         /* sanity checks */
294         llh_hdr = &handle->lgh_hdr->llh_hdr;
295         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
296                 CERROR("bad log header magic: %#x (expecting %#x)\n",
297                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
298                 rc = -EIO;
299         } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
300                 CERROR("incorrectly sized log header: %#x (expecting %#x)\n",
301                        llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
302                 CERROR("you may need to re-run lconf --write_conf.\n");
303                 rc = -EIO;
304         }
305 out:
306         ptlrpc_req_finished(req);
307 err_exit:
308         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
309         return rc;
310 }
311
312 static int llog_client_close(const struct lu_env *env,
313                              struct llog_handle *handle)
314 {
315         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
316          *  the servers all close the file at the end of every
317          * other LLOG_ RPC.
318          */
319         return 0;
320 }
321
322 struct llog_operations llog_client_ops = {
323         .lop_next_block         = llog_client_next_block,
324         .lop_prev_block         = llog_client_prev_block,
325         .lop_read_header        = llog_client_read_header,
326         .lop_open               = llog_client_open,
327         .lop_close              = llog_client_close,
328 };
329 EXPORT_SYMBOL(llog_client_ops);