• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 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/obdclass/llog_cat.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  *
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
43  *
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46  * Author: Mikhail Pershin <mike.pershin@intel.com>
47  */
48 
49 #define DEBUG_SUBSYSTEM S_LOG
50 
51 #include "../include/obd_class.h"
52 
53 #include "llog_internal.h"
54 
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.
57  *
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.
60  *
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()
63  */
llog_cat_id2handle(const struct lu_env * env,struct llog_handle * cathandle,struct llog_handle ** res,struct llog_logid * logid)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)
68 {
69 	struct llog_handle	*loghandle;
70 	int			 rc = 0;
71 
72 	if (cathandle == NULL)
73 		return -EBADF;
74 
75 	down_write(&cathandle->lgh_lock);
76 	list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
77 				u.phd.phd_entry) {
78 		struct llog_logid *cgl = &loghandle->lgh_id;
79 
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,
86 				       logid->lgl_ogen);
87 				continue;
88 			}
89 			loghandle->u.phd.phd_cat_handle = cathandle;
90 			up_write(&cathandle->lgh_lock);
91 			rc = 0;
92 			goto out;
93 		}
94 	}
95 	up_write(&cathandle->lgh_lock);
96 
97 	rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
98 		       LLOG_OPEN_EXISTS);
99 	if (rc < 0) {
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);
103 		return rc;
104 	}
105 
106 	rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
107 	if (rc < 0) {
108 		llog_close(env, loghandle);
109 		loghandle = NULL;
110 		return rc;
111 	}
112 
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);
116 
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;
121 out:
122 	llog_handle_get(loghandle);
123 	*res = loghandle;
124 	return 0;
125 }
126 
llog_cat_close(const struct lu_env * env,struct llog_handle * cathandle)127 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
128 {
129 	struct llog_handle	*loghandle, *n;
130 	int			 rc;
131 
132 	list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
133 				     u.phd.phd_entry) {
134 		/* unlink open-not-created llogs */
135 		list_del_init(&loghandle->u.phd.phd_entry);
136 		llog_close(env, loghandle);
137 	}
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);
142 	return rc;
143 }
144 EXPORT_SYMBOL(llog_cat_close);
145 
llog_cat_process_cb(const struct lu_env * env,struct llog_handle * cat_llh,struct llog_rec_hdr * rec,void * data)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)
149 {
150 	struct llog_process_data *d = data;
151 	struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
152 	struct llog_handle *llh;
153 	int rc;
154 
155 	if (rec->lrh_type != LLOG_LOGID_MAGIC) {
156 		CERROR("invalid record in catalog\n");
157 		return -EINVAL;
158 	}
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));
162 
163 	rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
164 	if (rc) {
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);
168 		return rc;
169 	}
170 
171 	if (rec->lrh_index < d->lpd_startcat)
172 		/* Skip processing of the logs until startcat */
173 		rc = 0;
174 	else if (d->lpd_startidx > 0) {
175 		struct llog_process_cat_data cd;
176 
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,
180 					  &cd, false);
181 		/* Continue processing the next log from idx 0 */
182 		d->lpd_startidx = 0;
183 	} else {
184 		rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
185 					  NULL, false);
186 	}
187 
188 	llog_handle_put(llh);
189 
190 	return rc;
191 }
192 
llog_cat_process_or_fork(const struct lu_env * env,struct llog_handle * cat_llh,llog_cb_t cb,void * data,int startcat,int startidx,bool fork)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)
197 {
198 	struct llog_process_data d;
199 	struct llog_log_hdr *llh = cat_llh->lgh_hdr;
200 	int rc;
201 
202 	LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
203 	d.lpd_data = data;
204 	d.lpd_cb = cb;
205 	d.lpd_startcat = startcat;
206 	d.lpd_startidx = startidx;
207 
208 	if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
209 		struct llog_process_cat_data cd;
210 
211 		CWARN("catlog "DOSTID" crosses index zero\n",
212 		      POSTID(&cat_llh->lgh_id.lgl_oi));
213 
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,
217 					  &d, &cd, fork);
218 		if (rc != 0)
219 			return rc;
220 
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,
224 					  &d, &cd, fork);
225 	} else {
226 		rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
227 					  &d, NULL, fork);
228 	}
229 
230 	return rc;
231 }
232 
llog_cat_process(const struct lu_env * env,struct llog_handle * cat_llh,llog_cb_t cb,void * data,int startcat,int startidx)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)
235 {
236 	return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
237 					startidx, false);
238 }
239 EXPORT_SYMBOL(llog_cat_process);
240