2 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * You should have received a copy of the GNU General Public License
6 * along with Lustre; if not, write to the Free Software
7 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10 #define DEBUG_SUBSYSTEM S_LOG
19 #include <liblustre.h>
22 #include <linux/obd_class.h>
23 #include <linux/lustre_log.h>
24 #include <portals/list.h>
25 #include "llog_internal.h"
27 /* helper functions for calling the llog obd methods */
29 int llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
30 int count, struct llog_logid *logid, struct llog_operations *op)
33 struct llog_ctxt *ctxt;
36 if (index < 0 || index >= LLOG_MAX_CTXTS)
39 OBD_ALLOC(ctxt, sizeof(*ctxt));
43 obd->obd_llog_ctxt[index] = ctxt;
45 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
46 ctxt->loc_idx = index;
47 ctxt->loc_logops = op;
48 sema_init(&ctxt->loc_sem, 1);
51 rc = op->lop_setup(obd, index, disk_obd, count, logid);
53 OBD_FREE(ctxt, sizeof(*ctxt));
57 EXPORT_SYMBOL(llog_setup);
59 int llog_cleanup(struct llog_ctxt *ctxt)
66 if (CTXTP(ctxt, cleanup))
67 rc = CTXTP(ctxt, cleanup)(ctxt);
69 ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
70 class_export_put(ctxt->loc_exp);
72 OBD_FREE(ctxt, sizeof(*ctxt));
76 EXPORT_SYMBOL(llog_cleanup);
78 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
86 if (CTXTP(ctxt, sync))
87 rc = CTXTP(ctxt, sync)(ctxt, exp);
91 EXPORT_SYMBOL(llog_sync);
93 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
94 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
101 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
103 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
106 EXPORT_SYMBOL(llog_add);
108 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
109 int count, struct llog_cookie *cookies, int flags)
115 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
116 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
119 EXPORT_SYMBOL(llog_cancel);
121 /* callback func for llog_process in llog_obd_origin_setup */
122 static int cat_cancel_cb(struct llog_handle *cathandle,
123 struct llog_rec_hdr *rec, void *data)
125 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
126 struct llog_handle *loghandle;
127 struct llog_log_hdr *llh;
131 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
132 CERROR("invalid record in catalog\n");
135 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
136 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
137 rec->lrh_index, cathandle->lgh_id.lgl_oid);
139 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
141 CERROR("Cannot find handle for log "LPX64"\n",
142 lir->lid_id.lgl_oid);
146 llh = loghandle->lgh_hdr;
147 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
148 (llh->llh_count == 1)) {
149 rc = llog_destroy(loghandle);
151 CERROR("failure destroying log in postsetup: %d\n", rc);
154 index = loghandle->u.phd.phd_cookie.lgc_index;
155 llog_free_handle(loghandle);
158 llog_cat_set_first_idx(cathandle, index);
159 rc = llog_cancel_rec(cathandle, index);
161 CWARN("cancel log "LPX64":%x at index %u of catalog "
162 LPX64"\n", lir->lid_id.lgl_oid,
163 lir->lid_id.lgl_ogen, rec->lrh_index,
164 cathandle->lgh_id.lgl_oid);
170 /* lop_setup method for filter/osc */
171 // XXX how to set exports
172 int llog_obd_origin_setup(struct obd_device *obd, int index,
173 struct obd_device *disk_obd, int count,
174 struct llog_logid *logid)
176 struct llog_ctxt *ctxt;
177 struct llog_handle *handle;
178 struct obd_run_ctxt saved;
187 ctxt = llog_get_context(obd, index);
192 rc = llog_create(ctxt, &handle, logid, NULL);
194 rc = llog_create(ctxt, &handle, NULL, NULL);
196 *logid = handle->lgh_id;
201 ctxt->loc_handle = handle;
202 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
203 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
204 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
208 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
210 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
213 obd->obd_llog_ctxt[index] = NULL;
214 OBD_FREE(ctxt, sizeof(*ctxt));
218 EXPORT_SYMBOL(llog_obd_origin_setup);
220 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
222 struct llog_handle *cathandle, *n, *loghandle;
223 struct llog_log_hdr *llh;
230 cathandle = ctxt->loc_handle;
232 list_for_each_entry_safe(loghandle, n,
233 &cathandle->u.chd.chd_head,
235 llh = loghandle->lgh_hdr;
236 if ((llh->llh_flags &
237 LLOG_F_ZAP_WHEN_EMPTY) &&
238 (llh->llh_count == 1)) {
239 rc = llog_destroy(loghandle);
241 CERROR("failure destroying log during "
242 "cleanup: %d\n", rc);
245 index = loghandle->u.phd.phd_cookie.lgc_index;
246 llog_free_handle(loghandle);
249 llog_cat_set_first_idx(cathandle, index);
250 rc = llog_cancel_rec(cathandle, index);
252 CDEBUG(D_HA, "cancel plain log at index"
253 " %u of catalog "LPX64"\n",
254 index,cathandle->lgh_id.lgl_oid);
257 llog_cat_put(ctxt->loc_handle);
261 EXPORT_SYMBOL(llog_obd_origin_cleanup);
263 /* add for obdfilter/sz and mds/unlink */
264 int llog_obd_origin_add(struct llog_ctxt *ctxt,
265 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
266 struct llog_cookie *logcookies, int numcookies)
268 struct llog_handle *cathandle;
272 cathandle = ctxt->loc_handle;
273 LASSERT(cathandle != NULL);
274 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
276 CERROR("write one catalog record failed: %d\n", rc);
279 EXPORT_SYMBOL(llog_obd_origin_add);
281 int llog_cat_initialize(struct obd_device *obd, int count)
283 struct llog_catid *idarray;
284 int size = sizeof(*idarray) * count;
285 char name[32] = CATLIST;
289 OBD_ALLOC(idarray, size);
293 rc = llog_get_cat_list(obd, obd, name, count, idarray);
295 CERROR("rc: %d\n", rc);
299 rc = obd_llog_init(obd, obd, count, idarray);
301 CERROR("rc: %d\n", rc);
305 rc = llog_put_cat_list(obd, obd, name, count, idarray);
307 CERROR("rc: %d\n", rc);
312 OBD_FREE(idarray, size);
315 EXPORT_SYMBOL(llog_cat_initialize);
317 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
318 int count, struct llog_catid *logid)
322 OBD_CHECK_OP(obd, llog_init, 0);
323 OBD_COUNTER_INCREMENT(obd, llog_init);
325 rc = OBP(obd, llog_init)(obd, disk_obd, count, logid);
328 EXPORT_SYMBOL(obd_llog_init);
330 int obd_llog_finish(struct obd_device *obd, int count)
334 OBD_CHECK_OP(obd, llog_finish, 0);
335 OBD_COUNTER_INCREMENT(obd, llog_finish);
337 rc = OBP(obd, llog_finish)(obd, count);
340 EXPORT_SYMBOL(obd_llog_finish);