4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_cat.c
38 * OST<->MDS recovery logging infrastructure.
40 * Invariants in implementation:
41 * - we do not share logs among different OST<->MDS connections, so that
42 * if an OST or MDS fails it need only look at log(s) relevant to itself
44 * Author: Andreas Dilger <adilger@clusterfs.com>
45 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46 * Author: Mikhail Pershin <mike.pershin@intel.com>
49 #define DEBUG_SUBSYSTEM S_LOG
51 #include "../include/obd_class.h"
53 #include "llog_internal.h"
55 /* Open an existent log handle and add it to the open list.
56 * This log handle will be closed when all of the records in it are removed.
58 * Assumes caller has already pushed us into the kernel context and is locking.
59 * We return a lock on the handle to ensure nobody yanks it from us.
61 * This takes extra reference on llog_handle via llog_handle_get() and require
62 * this reference to be put by caller using llog_handle_put()
64 static int llog_cat_id2handle(const struct lu_env *env,
65 struct llog_handle *cathandle,
66 struct llog_handle **res,
67 struct llog_logid *logid)
69 struct llog_handle *loghandle;
72 if (cathandle == NULL)
75 down_write(&cathandle->lgh_lock);
76 list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
78 struct llog_logid *cgl = &loghandle->lgh_id;
80 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
81 ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
82 if (cgl->lgl_ogen != logid->lgl_ogen) {
83 CERROR("%s: log "DOSTID" generation %x != %x\n",
84 loghandle->lgh_ctxt->loc_obd->obd_name,
85 POSTID(&logid->lgl_oi), cgl->lgl_ogen,
89 loghandle->u.phd.phd_cat_handle = cathandle;
90 up_write(&cathandle->lgh_lock);
95 up_write(&cathandle->lgh_lock);
97 rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
100 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
101 cathandle->lgh_ctxt->loc_obd->obd_name,
102 POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
106 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
108 llog_close(env, loghandle);
113 down_write(&cathandle->lgh_lock);
114 list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
115 up_write(&cathandle->lgh_lock);
117 loghandle->u.phd.phd_cat_handle = cathandle;
118 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
119 loghandle->u.phd.phd_cookie.lgc_index =
120 loghandle->lgh_hdr->llh_cat_idx;
122 llog_handle_get(loghandle);
127 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
129 struct llog_handle *loghandle, *n;
132 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
134 /* unlink open-not-created llogs */
135 list_del_init(&loghandle->u.phd.phd_entry);
136 llog_close(env, loghandle);
138 /* if handle was stored in ctxt, remove it too */
139 if (cathandle->lgh_ctxt->loc_handle == cathandle)
140 cathandle->lgh_ctxt->loc_handle = NULL;
141 rc = llog_close(env, cathandle);
144 EXPORT_SYMBOL(llog_cat_close);
146 static int llog_cat_process_cb(const struct lu_env *env,
147 struct llog_handle *cat_llh,
148 struct llog_rec_hdr *rec, void *data)
150 struct llog_process_data *d = data;
151 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
152 struct llog_handle *llh;
155 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
156 CERROR("invalid record in catalog\n");
159 CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
160 DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
161 rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
163 rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
165 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
166 cat_llh->lgh_ctxt->loc_obd->obd_name,
167 POSTID(&lir->lid_id.lgl_oi), rc);
171 if (rec->lrh_index < d->lpd_startcat)
172 /* Skip processing of the logs until startcat */
174 else if (d->lpd_startidx > 0) {
175 struct llog_process_cat_data cd;
177 cd.lpcd_first_idx = d->lpd_startidx;
178 cd.lpcd_last_idx = 0;
179 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
181 /* Continue processing the next log from idx 0 */
184 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
188 llog_handle_put(llh);
193 static int llog_cat_process_or_fork(const struct lu_env *env,
194 struct llog_handle *cat_llh,
195 llog_cb_t cb, void *data, int startcat,
196 int startidx, bool fork)
198 struct llog_process_data d;
199 struct llog_log_hdr *llh = cat_llh->lgh_hdr;
202 LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
205 d.lpd_startcat = startcat;
206 d.lpd_startidx = startidx;
208 if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
209 struct llog_process_cat_data cd;
211 CWARN("catlog "DOSTID" crosses index zero\n",
212 POSTID(&cat_llh->lgh_id.lgl_oi));
214 cd.lpcd_first_idx = llh->llh_cat_idx;
215 cd.lpcd_last_idx = 0;
216 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
221 cd.lpcd_first_idx = 0;
222 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
223 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
226 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
233 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
234 llog_cb_t cb, void *data, int startcat, int startidx)
236 return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
239 EXPORT_SYMBOL(llog_cat_process);