Whamcloud - gitweb
LU-13004 ptlrpc: Allow BULK_BUF_KIOV to accept a kvec
[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.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 <linux/list.h>
42 #include <libcfs/libcfs.h>
43
44 #include <obd_class.h>
45 #include <lustre_log.h>
46 #include <lustre_net.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 /*
74  * This is a callback from the llog_* functions.
75  * Assumes caller has already pushed us into the kernel context.
76  */
77 static int llog_client_open(const struct lu_env *env,
78                             struct llog_handle *lgh, struct llog_logid *logid,
79                             char *name, enum llog_open_param open_param)
80 {
81         struct obd_import *imp;
82         struct llogd_body *body;
83         struct llog_ctxt *ctxt = lgh->lgh_ctxt;
84         struct ptlrpc_request *req = NULL;
85         int rc;
86
87         ENTRY;
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)
97                 GOTO(out, rc = -ENOMEM);
98
99         if (name)
100                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
101                                      strlen(name) + 1);
102
103         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
104                                  LLOG_ORIGIN_HANDLE_CREATE);
105         if (rc) {
106                 ptlrpc_request_free(req);
107                 req = NULL;
108                 GOTO(out, rc);
109         }
110         ptlrpc_request_set_replen(req);
111
112         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
113         if (logid)
114                 body->lgd_logid = *logid;
115         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
116
117         if (name) {
118                 char *tmp;
119
120                 tmp = req_capsule_client_sized_get(&req->rq_pill, &RMF_NAME,
121                                                    strlen(name) + 1);
122                 LASSERT(tmp);
123                 strcpy(tmp, name);
124         }
125
126         rc = ptlrpc_queue_wait(req);
127         if (rc)
128                 GOTO(out, rc);
129
130         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
131         if (!body)
132                 GOTO(out, rc = -EFAULT);
133
134         lgh->lgh_id = body->lgd_logid;
135         lgh->lgh_ctxt = ctxt;
136         EXIT;
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         ENTRY;
155
156         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
157         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
158                                         LUSTRE_LOG_VERSION,
159                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
160         if (!req)
161                 GOTO(err_exit, rc = -ENOMEM);
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         /*
176          * -EIO has a special meaning here. If llog_osd_next_block()
177          * reaches the end of the log without finding the desired
178          * record then it updates *cur_offset and *cur_idx and returns
179          * -EIO. In llog_process_thread() we use this to detect
180          * EOF. But we must be careful to distinguish between -EIO
181          * coming from llog_osd_next_block() and -EIO coming from
182          * ptlrpc or below.
183          */
184         if (rc == -EIO) {
185                 if (!req->rq_repmsg ||
186                     lustre_msg_get_status(req->rq_repmsg) != -EIO)
187                         GOTO(out, rc);
188         } else if (rc < 0) {
189                 GOTO(out, rc);
190         }
191
192         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
193         if (!body)
194                 GOTO(out, rc = -EFAULT);
195
196         *cur_idx = body->lgd_saved_index;
197         *cur_offset = body->lgd_cur_offset;
198
199         if (rc < 0)
200                 GOTO(out, rc);
201
202         /* The log records are swabbed as they are processed */
203         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
204         if (!ptr)
205                 GOTO(out, rc = -EFAULT);
206
207         memcpy(buf, ptr, len);
208         EXIT;
209 out:
210         ptlrpc_req_finished(req);
211 err_exit:
212         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
213         return rc;
214 }
215
216 static int llog_client_prev_block(const struct lu_env *env,
217                                   struct llog_handle *loghandle,
218                                   int prev_idx, void *buf, int len)
219 {
220         struct obd_import *imp;
221         struct ptlrpc_request *req = NULL;
222         struct llogd_body *body;
223         void *ptr;
224         int rc;
225
226         ENTRY;
227
228         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
229         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_PREV_BLOCK,
230                                         LUSTRE_LOG_VERSION,
231                                         LLOG_ORIGIN_HANDLE_PREV_BLOCK);
232         if (!req)
233                 GOTO(err_exit, rc = -ENOMEM);
234
235         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
236         body->lgd_logid = loghandle->lgh_id;
237         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
238         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
239         body->lgd_index = prev_idx;
240         body->lgd_len = len;
241
242         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
243         ptlrpc_request_set_replen(req);
244
245         rc = ptlrpc_queue_wait(req);
246         if (rc)
247                 GOTO(out, rc);
248
249         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
250         if (!body)
251                 GOTO(out, rc = -EFAULT);
252
253         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
254         if (!ptr)
255                 GOTO(out, rc = -EFAULT);
256
257         memcpy(buf, ptr, len);
258         EXIT;
259 out:
260         ptlrpc_req_finished(req);
261 err_exit:
262         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
263         return rc;
264 }
265
266 static int llog_client_read_header(const struct lu_env *env,
267                                    struct llog_handle *handle)
268 {
269         struct obd_import *imp;
270         struct ptlrpc_request *req = NULL;
271         struct llogd_body *body;
272         struct llog_log_hdr *hdr;
273         struct llog_rec_hdr *llh_hdr;
274         int rc;
275
276         ENTRY;
277
278         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
279         req = ptlrpc_request_alloc_pack(imp,
280                                         &RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
281                                         LUSTRE_LOG_VERSION,
282                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
283         if (!req)
284                 GOTO(err_exit, rc = -ENOMEM);
285
286         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
287         body->lgd_logid = handle->lgh_id;
288         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
289         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
290
291         ptlrpc_request_set_replen(req);
292         rc = ptlrpc_queue_wait(req);
293         if (rc)
294                 GOTO(out, rc);
295
296         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
297         if (!hdr)
298                 GOTO(out, rc = -EFAULT);
299
300         if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
301                 GOTO(out, rc = -EFAULT);
302
303         memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
304         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
305
306         /* sanity checks */
307         llh_hdr = &handle->lgh_hdr->llh_hdr;
308         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
309                 CERROR("bad log header magic: %#x (expecting %#x)\n",
310                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
311                 rc = -EIO;
312         } else if (llh_hdr->lrh_len !=
313                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len ||
314                    (llh_hdr->lrh_len & (llh_hdr->lrh_len - 1)) != 0 ||
315                    llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
316                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
317                 CERROR("incorrectly sized log header: %#x, expecting %#x (power of two > 8192)\n",
318                        llh_hdr->lrh_len,
319                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
320                 CERROR("you may need to re-run lconf --write_conf.\n");
321                 rc = -EIO;
322         }
323         EXIT;
324 out:
325         ptlrpc_req_finished(req);
326 err_exit:
327         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
328         return rc;
329 }
330
331 static int llog_client_close(const struct lu_env *env,
332                              struct llog_handle *handle)
333 {
334         /*
335          * this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
336          * the servers all close the file at the end of every
337          * other LLOG_ RPC.
338          */
339         return 0;
340 }
341
342 struct llog_operations llog_client_ops = {
343         .lop_next_block         = llog_client_next_block,
344         .lop_prev_block         = llog_client_prev_block,
345         .lop_read_header        = llog_client_read_header,
346         .lop_open               = llog_client_open,
347         .lop_close              = llog_client_close,
348 };
349 EXPORT_SYMBOL(llog_client_ops);