1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45#define DEBUG_SUBSYSTEM S_LOG
46
47#include "../include/obd_class.h"
48
49#include "llog_internal.h"
50
51
52
53
54
55
56
57
58
59
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)
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;
119out:
120 llog_handle_get(loghandle);
121 *res = loghandle;
122 return 0;
123}
124
125int 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
132 list_del_init(&loghandle->u.phd.phd_entry);
133 llog_close(env, loghandle);
134 }
135
136 if (cathandle->lgh_ctxt->loc_handle == cathandle)
137 cathandle->lgh_ctxt->loc_handle = NULL;
138 return llog_close(env, cathandle);
139}
140EXPORT_SYMBOL(llog_cat_close);
141
142static 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
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
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
189static 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
229int 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}
235EXPORT_SYMBOL(llog_cat_process);
236