1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2005 Cluster File Systems, Inc.
6 * This file is part of the Lustre file system, http://www.lustre.org
7 * Lustre is a trademark of Cluster File Systems, Inc.
9 * You may have signed or agreed to another license before downloading
10 * this software. If so, you are bound by the terms and conditions
11 * of that agreement, and the following does not apply to you. See the
12 * LICENSE file included with this distribution for more information.
14 * If you did not agree to a different license, then this copy of Lustre
15 * is open source software; you can redistribute it and/or modify it
16 * under the terms of version 2 of the GNU General Public License as
17 * published by the Free Software Foundation.
19 * In either case, Lustre is distributed in the hope that it will be
20 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * license text for more details.
25 #define DEBUG_SUBSYSTEM S_LOG
32 #include <liblustre.h>
35 #include <obd_class.h>
36 #include <lustre_log.h>
37 #include <libcfs/list.h>
38 #include "llog_internal.h"
40 /* helper functions for calling the llog obd methods */
42 int llog_cleanup(struct llog_ctxt *ctxt)
52 if (CTXTP(ctxt, cleanup))
53 rc = CTXTP(ctxt, cleanup)(ctxt);
54 ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
57 class_export_put(ctxt->loc_exp);
58 OBD_FREE(ctxt, sizeof(*ctxt));
62 EXPORT_SYMBOL(llog_cleanup);
64 int llog_setup(struct obd_device *obd, struct obd_llogs *llogs, int index,
65 struct obd_device *disk_obd, int count, struct llog_logid *logid,
66 struct llog_operations *op)
69 struct llog_ctxt *ctxt;
72 if (index < 0 || index >= LLOG_MAX_CTXTS)
75 /* in some recovery cases, obd_llog_ctxt might already be set,
76 * but llogs might still be zero, for example in obd_filter recovery */
77 if (obd->obd_llog_ctxt[index] &&
78 (!llogs || (llogs && llogs->llog_ctxt[index]))) {
79 /* mds_lov_update_mds might call here multiple times. So if the
80 llog is already set up then don't to do it again. */
81 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
82 obd->obd_name, index);
83 ctxt = obd->obd_llog_ctxt[index];
84 LASSERT(ctxt->loc_obd == obd);
85 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
86 LASSERT(ctxt->loc_logops == op);
90 OBD_ALLOC(ctxt, sizeof(*ctxt));
92 GOTO(out, rc = -ENOMEM);
95 llogs->llog_ctxt[index] = ctxt;
97 if (!obd->obd_llog_ctxt[index])
98 obd->obd_llog_ctxt[index] = ctxt;
101 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
102 ctxt->loc_idx = index;
103 ctxt->loc_logops = op;
104 sema_init(&ctxt->loc_sem, 1);
107 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
110 obd->obd_llog_ctxt[index] = NULL;
111 class_export_put(ctxt->loc_exp);
112 OBD_FREE(ctxt, sizeof(*ctxt));
118 EXPORT_SYMBOL(llog_setup);
120 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
128 if (CTXTP(ctxt, sync))
129 rc = CTXTP(ctxt, sync)(ctxt, exp);
133 EXPORT_SYMBOL(llog_sync);
135 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
136 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
147 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
149 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
152 EXPORT_SYMBOL(llog_add);
154 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
155 int count, struct llog_cookie *cookies, int flags)
165 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
166 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
169 EXPORT_SYMBOL(llog_cancel);
171 /* callback func for llog_process in llog_obd_origin_setup */
172 static int cat_cancel_cb(struct llog_handle *cathandle,
173 struct llog_rec_hdr *rec, void *data)
175 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
176 struct llog_handle *loghandle;
177 struct llog_log_hdr *llh;
181 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
182 CERROR("invalid record in catalog\n");
185 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
186 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
187 rec->lrh_index, cathandle->lgh_id.lgl_oid);
189 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
191 CERROR("Cannot find handle for log "LPX64"\n",
192 lir->lid_id.lgl_oid);
196 llh = loghandle->lgh_hdr;
197 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
198 (llh->llh_count == 1)) {
199 rc = llog_destroy(loghandle);
201 CERROR("failure destroying log in postsetup: %d\n", rc);
203 index = loghandle->u.phd.phd_cookie.lgc_index;
204 llog_free_handle(loghandle);
207 llog_cat_set_first_idx(cathandle, index);
208 rc = llog_cancel_rec(cathandle, index);
210 CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
211 LPX64"\n", lir->lid_id.lgl_oid,
212 lir->lid_id.lgl_ogen, rec->lrh_index,
213 cathandle->lgh_id.lgl_oid);
219 /* lop_setup method for filter/osc */
220 // XXX how to set exports
221 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
222 int index, struct obd_device *disk_obd, int count,
223 struct llog_logid *logid)
225 struct llog_ctxt *ctxt;
226 struct llog_handle *handle;
227 struct lvfs_run_ctxt saved;
237 ctxt = llog_get_context(obd, index);
239 ctxt = llog_get_context_from_llogs(llogs, index);
245 rc = llog_create(ctxt, &handle, logid, NULL);
247 rc = llog_create(ctxt, &handle, NULL, NULL);
249 *logid = handle->lgh_id;
254 ctxt->loc_handle = handle;
255 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
256 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
257 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
261 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
263 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
267 EXPORT_SYMBOL(llog_obd_origin_setup);
269 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
271 struct llog_handle *cathandle, *n, *loghandle;
272 struct llog_log_hdr *llh;
279 cathandle = ctxt->loc_handle;
281 list_for_each_entry_safe(loghandle, n,
282 &cathandle->u.chd.chd_head,
284 llh = loghandle->lgh_hdr;
285 if ((llh->llh_flags &
286 LLOG_F_ZAP_WHEN_EMPTY) &&
287 (llh->llh_count == 1)) {
288 rc = llog_destroy(loghandle);
290 CERROR("failure destroying log during "
291 "cleanup: %d\n", rc);
293 index = loghandle->u.phd.phd_cookie.lgc_index;
294 llog_free_handle(loghandle);
297 llog_cat_set_first_idx(cathandle, index);
298 rc = llog_cancel_rec(cathandle, index);
300 CDEBUG(D_HA, "cancel plain log at index"
301 " %u of catalog "LPX64"\n",
302 index,cathandle->lgh_id.lgl_oid);
305 llog_cat_put(ctxt->loc_handle);
309 EXPORT_SYMBOL(llog_obd_origin_cleanup);
311 /* add for obdfilter/sz and mds/unlink */
312 int llog_obd_origin_add(struct llog_ctxt *ctxt,
313 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
314 struct llog_cookie *logcookies, int numcookies)
316 struct llog_handle *cathandle;
320 cathandle = ctxt->loc_handle;
321 LASSERT(cathandle != NULL);
322 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
324 CERROR("write one catalog record failed: %d\n", rc);
327 EXPORT_SYMBOL(llog_obd_origin_add);
329 int llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs,
330 int count, struct obd_uuid *uuid)
332 char name[32] = CATLIST;
333 struct llog_catid *idarray = NULL;
334 int size = sizeof(*idarray) * count;
339 OBD_VMALLOC(idarray, size);
344 rc = llog_get_cat_list(obd, obd, name, count, idarray);
346 CERROR("rc: %d\n", rc);
350 rc = obd_llog_init(obd, llogs, obd, count, idarray, uuid);
352 CERROR("rc: %d\n", rc);
356 rc = llog_put_cat_list(obd, obd, name, count, idarray);
358 CERROR("rc: %d\n", rc);
364 OBD_VFREE(idarray, size);
367 EXPORT_SYMBOL(llog_cat_initialize);
369 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
370 struct obd_device *disk_obd, int count,
371 struct llog_catid *logid, struct obd_uuid *uuid)
375 OBD_CHECK_DT_OP(obd, llog_init, 0);
376 OBD_COUNTER_INCREMENT(obd, llog_init);
378 rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid,
382 EXPORT_SYMBOL(obd_llog_init);
384 int obd_llog_finish(struct obd_device *obd, int count)
388 OBD_CHECK_DT_OP(obd, llog_finish, 0);
389 OBD_COUNTER_INCREMENT(obd, llog_finish);
391 rc = OBP(obd, llog_finish)(obd, count);
394 EXPORT_SYMBOL(obd_llog_finish);