GNU Linux-libre 4.9.332-gnu1
[releases.git] / drivers / staging / lustre / lustre / obdclass / llog_cat.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/obdclass/llog_cat.c
33  *
34  * OST<->MDS recovery logging infrastructure.
35  *
36  * Invariants in implementation:
37  * - we do not share logs among different OST<->MDS connections, so that
38  *   if an OST or MDS fails it need only look at log(s) relevant to itself
39  *
40  * Author: Andreas Dilger <adilger@clusterfs.com>
41  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42  * Author: Mikhail Pershin <mike.pershin@intel.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47 #include "../include/obd_class.h"
48
49 #include "llog_internal.h"
50
51 /* Open an existent log handle and add it to the open list.
52  * This log handle will be closed when all of the records in it are removed.
53  *
54  * Assumes caller has already pushed us into the kernel context and is locking.
55  * We return a lock on the handle to ensure nobody yanks it from us.
56  *
57  * This takes extra reference on llog_handle via llog_handle_get() and require
58  * this reference to be put by caller using llog_handle_put()
59  */
60 static int llog_cat_id2handle(const struct lu_env *env,
61                               struct llog_handle *cathandle,
62                               struct llog_handle **res,
63                               struct llog_logid *logid)
64 {
65         struct llog_handle      *loghandle;
66         enum llog_flag fmt;
67         int                      rc = 0;
68
69         if (!cathandle)
70                 return -EBADF;
71
72         fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
73         down_write(&cathandle->lgh_lock);
74         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
75                             u.phd.phd_entry) {
76                 struct llog_logid *cgl = &loghandle->lgh_id;
77
78                 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
79                     ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
80                         if (cgl->lgl_ogen != logid->lgl_ogen) {
81                                 CERROR("%s: log "DOSTID" generation %x != %x\n",
82                                        loghandle->lgh_ctxt->loc_obd->obd_name,
83                                        POSTID(&logid->lgl_oi), cgl->lgl_ogen,
84                                        logid->lgl_ogen);
85                                 continue;
86                         }
87                         loghandle->u.phd.phd_cat_handle = cathandle;
88                         up_write(&cathandle->lgh_lock);
89                         rc = 0;
90                         goto out;
91                 }
92         }
93         up_write(&cathandle->lgh_lock);
94
95         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
96                        LLOG_OPEN_EXISTS);
97         if (rc < 0) {
98                 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
99                        cathandle->lgh_ctxt->loc_obd->obd_name,
100                        POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
101                 return rc;
102         }
103
104         rc = llog_init_handle(env, loghandle, fmt | LLOG_F_IS_PLAIN, NULL);
105         if (rc < 0) {
106                 llog_close(env, loghandle);
107                 loghandle = NULL;
108                 return rc;
109         }
110
111         down_write(&cathandle->lgh_lock);
112         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
113         up_write(&cathandle->lgh_lock);
114
115         loghandle->u.phd.phd_cat_handle = cathandle;
116         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
117         loghandle->u.phd.phd_cookie.lgc_index =
118                                 loghandle->lgh_hdr->llh_cat_idx;
119 out:
120         llog_handle_get(loghandle);
121         *res = loghandle;
122         return 0;
123 }
124
125 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
126 {
127         struct llog_handle      *loghandle, *n;
128
129         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
130                                  u.phd.phd_entry) {
131                 /* unlink open-not-created llogs */
132                 list_del_init(&loghandle->u.phd.phd_entry);
133                 llog_close(env, loghandle);
134         }
135         /* if handle was stored in ctxt, remove it too */
136         if (cathandle->lgh_ctxt->loc_handle == cathandle)
137                 cathandle->lgh_ctxt->loc_handle = NULL;
138         return llog_close(env, cathandle);
139 }
140 EXPORT_SYMBOL(llog_cat_close);
141
142 static int llog_cat_process_cb(const struct lu_env *env,
143                                struct llog_handle *cat_llh,
144                                struct llog_rec_hdr *rec, void *data)
145 {
146         struct llog_process_data *d = data;
147         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
148         struct llog_handle *llh;
149         int rc;
150
151         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
152                 CERROR("invalid record in catalog\n");
153                 return -EINVAL;
154         }
155         CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
156                DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
157                rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
158
159         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
160         if (rc) {
161                 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
162                        cat_llh->lgh_ctxt->loc_obd->obd_name,
163                        POSTID(&lir->lid_id.lgl_oi), rc);
164                 return rc;
165         }
166
167         if (rec->lrh_index < d->lpd_startcat)
168                 /* Skip processing of the logs until startcat */
169                 rc = 0;
170         else if (d->lpd_startidx > 0) {
171                 struct llog_process_cat_data cd;
172
173                 cd.lpcd_first_idx = d->lpd_startidx;
174                 cd.lpcd_last_idx = 0;
175                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
176                                           &cd, false);
177                 /* Continue processing the next log from idx 0 */
178                 d->lpd_startidx = 0;
179         } else {
180                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
181                                           NULL, false);
182         }
183
184         llog_handle_put(llh);
185
186         return rc;
187 }
188
189 static int llog_cat_process_or_fork(const struct lu_env *env,
190                                     struct llog_handle *cat_llh,
191                                     llog_cb_t cb, void *data, int startcat,
192                                     int startidx, bool fork)
193 {
194         struct llog_process_data d;
195         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
196         int rc;
197
198         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
199         d.lpd_data = data;
200         d.lpd_cb = cb;
201         d.lpd_startcat = startcat;
202         d.lpd_startidx = startidx;
203
204         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
205                 struct llog_process_cat_data cd;
206
207                 CWARN("catlog "DOSTID" crosses index zero\n",
208                       POSTID(&cat_llh->lgh_id.lgl_oi));
209
210                 cd.lpcd_first_idx = llh->llh_cat_idx;
211                 cd.lpcd_last_idx = 0;
212                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
213                                           &d, &cd, fork);
214                 if (rc != 0)
215                         return rc;
216
217                 cd.lpcd_first_idx = 0;
218                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
219                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
220                                           &d, &cd, fork);
221         } else {
222                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
223                                           &d, NULL, fork);
224         }
225
226         return rc;
227 }
228
229 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
230                      llog_cb_t cb, void *data, int startcat, int startidx)
231 {
232         return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
233                                         startidx, false);
234 }
235 EXPORT_SYMBOL(llog_cat_process);