Merge remote-tracking branch 'staging/staging-next'
[deliverable/linux.git] / drivers / staging / lustre / lustre / obdclass / llog_cat.c
CommitLineData
d7e09d03
PT
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
6a5b99a4 18 * http://www.gnu.org/licenses/gpl-2.0.html
d7e09d03 19 *
d7e09d03
PT
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 *
1dc563a6 26 * Copyright (c) 2012, 2015, Intel Corporation.
d7e09d03
PT
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
610f7377 47#include "../include/obd_class.h"
d7e09d03
PT
48
49#include "llog_internal.h"
50
d7e09d03
PT
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 */
17ad0ce6
SB
60static int llog_cat_id2handle(const struct lu_env *env,
61 struct llog_handle *cathandle,
62 struct llog_handle **res,
63 struct llog_logid *logid)
d7e09d03
PT
64{
65 struct llog_handle *loghandle;
66 int rc = 0;
67
cce3c2da 68 if (!cathandle)
0a3bdb00 69 return -EBADF;
d7e09d03
PT
70
71 down_write(&cathandle->lgh_lock);
72 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
926d6fb2 73 u.phd.phd_entry) {
d7e09d03
PT
74 struct llog_logid *cgl = &loghandle->lgh_id;
75
76 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
77 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
78 if (cgl->lgl_ogen != logid->lgl_ogen) {
79 CERROR("%s: log "DOSTID" generation %x != %x\n",
80 loghandle->lgh_ctxt->loc_obd->obd_name,
81 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
82 logid->lgl_ogen);
83 continue;
84 }
85 loghandle->u.phd.phd_cat_handle = cathandle;
86 up_write(&cathandle->lgh_lock);
d212afd9
JL
87 rc = 0;
88 goto out;
d7e09d03
PT
89 }
90 }
91 up_write(&cathandle->lgh_lock);
92
93 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
94 LLOG_OPEN_EXISTS);
95 if (rc < 0) {
96 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
97 cathandle->lgh_ctxt->loc_obd->obd_name,
98 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
0a3bdb00 99 return rc;
d7e09d03
PT
100 }
101
102 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
103 if (rc < 0) {
104 llog_close(env, loghandle);
105 loghandle = NULL;
0a3bdb00 106 return rc;
d7e09d03
PT
107 }
108
109 down_write(&cathandle->lgh_lock);
110 list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
111 up_write(&cathandle->lgh_lock);
112
113 loghandle->u.phd.phd_cat_handle = cathandle;
114 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
115 loghandle->u.phd.phd_cookie.lgc_index =
116 loghandle->lgh_hdr->llh_cat_idx;
d7e09d03
PT
117out:
118 llog_handle_get(loghandle);
119 *res = loghandle;
120 return 0;
121}
122
123int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
124{
125 struct llog_handle *loghandle, *n;
d7e09d03 126
d7e09d03 127 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
926d6fb2 128 u.phd.phd_entry) {
d7e09d03
PT
129 /* unlink open-not-created llogs */
130 list_del_init(&loghandle->u.phd.phd_entry);
d7e09d03
PT
131 llog_close(env, loghandle);
132 }
133 /* if handle was stored in ctxt, remove it too */
134 if (cathandle->lgh_ctxt->loc_handle == cathandle)
135 cathandle->lgh_ctxt->loc_handle = NULL;
ac4455e0 136 return llog_close(env, cathandle);
d7e09d03
PT
137}
138EXPORT_SYMBOL(llog_cat_close);
139
89026cbc
AA
140static int llog_cat_process_cb(const struct lu_env *env,
141 struct llog_handle *cat_llh,
142 struct llog_rec_hdr *rec, void *data)
d7e09d03
PT
143{
144 struct llog_process_data *d = data;
145 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
146 struct llog_handle *llh;
147 int rc;
148
d7e09d03
PT
149 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
150 CERROR("invalid record in catalog\n");
0a3bdb00 151 return -EINVAL;
d7e09d03
PT
152 }
153 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
154 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
155 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
156
157 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
158 if (rc) {
159 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
160 cat_llh->lgh_ctxt->loc_obd->obd_name,
161 POSTID(&lir->lid_id.lgl_oi), rc);
0a3bdb00 162 return rc;
d7e09d03
PT
163 }
164
165 if (rec->lrh_index < d->lpd_startcat)
166 /* Skip processing of the logs until startcat */
7345fb75
JH
167 rc = 0;
168 else if (d->lpd_startidx > 0) {
d7e09d03
PT
169 struct llog_process_cat_data cd;
170
171 cd.lpcd_first_idx = d->lpd_startidx;
172 cd.lpcd_last_idx = 0;
173 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
174 &cd, false);
175 /* Continue processing the next log from idx 0 */
176 d->lpd_startidx = 0;
177 } else {
178 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
179 NULL, false);
180 }
7345fb75 181
d7e09d03
PT
182 llog_handle_put(llh);
183
0a3bdb00 184 return rc;
d7e09d03
PT
185}
186
17ad0ce6
SB
187static int llog_cat_process_or_fork(const struct lu_env *env,
188 struct llog_handle *cat_llh,
189 llog_cb_t cb, void *data, int startcat,
190 int startidx, bool fork)
d7e09d03
PT
191{
192 struct llog_process_data d;
193 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
194 int rc;
d7e09d03
PT
195
196 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
197 d.lpd_data = data;
198 d.lpd_cb = cb;
199 d.lpd_startcat = startcat;
200 d.lpd_startidx = startidx;
201
202 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
203 struct llog_process_cat_data cd;
204
205 CWARN("catlog "DOSTID" crosses index zero\n",
206 POSTID(&cat_llh->lgh_id.lgl_oi));
207
208 cd.lpcd_first_idx = llh->llh_cat_idx;
209 cd.lpcd_last_idx = 0;
210 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
211 &d, &cd, fork);
212 if (rc != 0)
0a3bdb00 213 return rc;
d7e09d03
PT
214
215 cd.lpcd_first_idx = 0;
216 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
217 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
218 &d, &cd, fork);
219 } else {
220 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
221 &d, NULL, fork);
222 }
223
0a3bdb00 224 return rc;
d7e09d03 225}
d7e09d03
PT
226
227int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
228 llog_cb_t cb, void *data, int startcat, int startidx)
229{
230 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
231 startidx, false);
232}
233EXPORT_SYMBOL(llog_cat_process);
This page took 0.538807 seconds and 5 git commands to generate.