Whamcloud - gitweb
LU-5092 nodemap: save nodemaps to targets for caching
[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, 2015, 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 <libcfs/libcfs.h>
46
47 #include <obd_class.h>
48 #include <lustre_log.h>
49 #include <lustre_net.h>
50 #include <libcfs/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         ENTRY;
89
90         LLOG_CLIENT_ENTRY(ctxt, imp);
91
92         /* client cannot create llog */
93         LASSERTF(open_param != LLOG_OPEN_NEW, "%#x\n", open_param);
94         LASSERT(lgh);
95
96         req = ptlrpc_request_alloc(imp, &RQF_LLOG_ORIGIN_HANDLE_CREATE);
97         if (req == NULL)
98                 GOTO(out, rc = -ENOMEM);
99
100         if (name)
101                 req_capsule_set_size(&req->rq_pill, &RMF_NAME, RCL_CLIENT,
102                                      strlen(name) + 1);
103
104         rc = ptlrpc_request_pack(req, LUSTRE_LOG_VERSION,
105                                  LLOG_ORIGIN_HANDLE_CREATE);
106         if (rc) {
107                 ptlrpc_request_free(req);
108                 req = NULL;
109                 GOTO(out, rc);
110         }
111         ptlrpc_request_set_replen(req);
112
113         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
114         if (logid)
115                 body->lgd_logid = *logid;
116         body->lgd_ctxt_idx = ctxt->loc_idx - 1;
117
118         if (name) {
119                 char *tmp;
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 == NULL)
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_destroy(const struct lu_env *env,
144                                struct llog_handle *loghandle,
145                                struct thandle *th)
146 {
147         struct obd_import     *imp;
148         struct ptlrpc_request *req = NULL;
149         struct llogd_body     *body;
150         int                    rc;
151         ENTRY;
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                 GOTO(err_exit, rc =-ENOMEM);
159
160         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
161         body->lgd_logid = loghandle->lgh_id;
162         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
163
164         if (!(body->lgd_llh_flags & LLOG_F_IS_PLAIN))
165                 CERROR("%s: wrong llog flags %x\n", imp->imp_obd->obd_name,
166                        body->lgd_llh_flags);
167
168         ptlrpc_request_set_replen(req);
169         rc = ptlrpc_queue_wait(req);
170
171         ptlrpc_req_finished(req);
172 err_exit:
173         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
174         RETURN(rc);
175 }
176
177
178 static int llog_client_next_block(const struct lu_env *env,
179                                   struct llog_handle *loghandle,
180                                   int *cur_idx, int next_idx,
181                                   __u64 *cur_offset, void *buf, int len)
182 {
183         struct obd_import     *imp;
184         struct ptlrpc_request *req = NULL;
185         struct llogd_body     *body;
186         void                  *ptr;
187         int                    rc;
188         ENTRY;
189
190         LLOG_CLIENT_ENTRY(loghandle->lgh_ctxt, imp);
191         req = ptlrpc_request_alloc_pack(imp, &RQF_LLOG_ORIGIN_HANDLE_NEXT_BLOCK,
192                                         LUSTRE_LOG_VERSION,
193                                         LLOG_ORIGIN_HANDLE_NEXT_BLOCK);
194         if (req == NULL)
195                 GOTO(err_exit, rc =-ENOMEM);
196
197         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
198         body->lgd_logid = loghandle->lgh_id;
199         body->lgd_ctxt_idx = loghandle->lgh_ctxt->loc_idx - 1;
200         body->lgd_llh_flags = loghandle->lgh_hdr->llh_flags;
201         body->lgd_index = next_idx;
202         body->lgd_saved_index = *cur_idx;
203         body->lgd_len = len;
204         body->lgd_cur_offset = *cur_offset;
205
206         req_capsule_set_size(&req->rq_pill, &RMF_EADATA, RCL_SERVER, len);
207         ptlrpc_request_set_replen(req);
208         rc = ptlrpc_queue_wait(req);
209         if (rc)
210                 GOTO(out, rc);
211
212         body = req_capsule_server_get(&req->rq_pill, &RMF_LLOGD_BODY);
213         if (body == NULL)
214                 GOTO(out, rc =-EFAULT);
215
216         /* The log records are swabbed as they are processed */
217         ptr = req_capsule_server_get(&req->rq_pill, &RMF_EADATA);
218         if (ptr == NULL)
219                 GOTO(out, rc =-EFAULT);
220
221         *cur_idx = body->lgd_saved_index;
222         *cur_offset = body->lgd_cur_offset;
223
224         memcpy(buf, ptr, len);
225         EXIT;
226 out:
227         ptlrpc_req_finished(req);
228 err_exit:
229         LLOG_CLIENT_EXIT(loghandle->lgh_ctxt, imp);
230         return rc;
231 }
232
233 static int llog_client_prev_block(const struct lu_env *env,
234                                   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(const struct lu_env *env,
283                                    struct llog_handle *handle)
284 {
285         struct obd_import     *imp;
286         struct ptlrpc_request *req = NULL;
287         struct llogd_body     *body;
288         struct llog_log_hdr   *hdr;
289         struct llog_rec_hdr   *llh_hdr;
290         int                    rc;
291         ENTRY;
292
293         LLOG_CLIENT_ENTRY(handle->lgh_ctxt, imp);
294         req = ptlrpc_request_alloc_pack(imp,&RQF_LLOG_ORIGIN_HANDLE_READ_HEADER,
295                                         LUSTRE_LOG_VERSION,
296                                         LLOG_ORIGIN_HANDLE_READ_HEADER);
297         if (req == NULL)
298                 GOTO(err_exit, rc = -ENOMEM);
299
300         body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_BODY);
301         body->lgd_logid = handle->lgh_id;
302         body->lgd_ctxt_idx = handle->lgh_ctxt->loc_idx - 1;
303         body->lgd_llh_flags = handle->lgh_hdr->llh_flags;
304
305         ptlrpc_request_set_replen(req);
306         rc = ptlrpc_queue_wait(req);
307         if (rc)
308                 GOTO(out, rc);
309
310         hdr = req_capsule_server_get(&req->rq_pill, &RMF_LLOG_LOG_HDR);
311         if (hdr == NULL)
312                 GOTO(out, rc =-EFAULT);
313
314         if (handle->lgh_hdr_size < hdr->llh_hdr.lrh_len)
315                 GOTO(out, rc = -EFAULT);
316
317         memcpy(handle->lgh_hdr, hdr, hdr->llh_hdr.lrh_len);
318         handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
319
320         /* sanity checks */
321         llh_hdr = &handle->lgh_hdr->llh_hdr;
322         if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
323                 CERROR("bad log header magic: %#x (expecting %#x)\n",
324                        llh_hdr->lrh_type, LLOG_HDR_MAGIC);
325                 rc = -EIO;
326         } else if (llh_hdr->lrh_len !=
327                    LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len ||
328                    (llh_hdr->lrh_len & (llh_hdr->lrh_len - 1)) != 0 ||
329                    llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
330                    llh_hdr->lrh_len > handle->lgh_hdr_size) {
331                 CERROR("incorrectly sized log header: %#x, "
332                        "expecting %#x (power of two > 8192)\n",
333                        llh_hdr->lrh_len,
334                        LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len);
335                 CERROR("you may need to re-run lconf --write_conf.\n");
336                 rc = -EIO;
337         }
338         EXIT;
339 out:
340         ptlrpc_req_finished(req);
341 err_exit:
342         LLOG_CLIENT_EXIT(handle->lgh_ctxt, imp);
343         return rc;
344 }
345
346 static int llog_client_close(const struct lu_env *env,
347                              struct llog_handle *handle)
348 {
349         /* this doesn't call LLOG_ORIGIN_HANDLE_CLOSE because
350            the servers all close the file at the end of every
351            other LLOG_ RPC. */
352         return(0);
353 }
354
355 struct llog_operations llog_client_ops = {
356         .lop_next_block         = llog_client_next_block,
357         .lop_prev_block         = llog_client_prev_block,
358         .lop_read_header        = llog_client_read_header,
359         .lop_open               = llog_client_open,
360         .lop_destroy            = llog_client_destroy,
361         .lop_close              = llog_client_close,
362 };
363 EXPORT_SYMBOL(llog_client_ops);