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,
78 if (obd->obd_llog_ctxt[index] &&
79 (!llogs || (llogs && llogs->llog_ctxt[index]))) {
80 /* mds_lov_update_mds might call here multiple times. So if the
81 llog is already set up then don't to do it again. */
82 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
83 obd->obd_name, index);
84 ctxt = obd->obd_llog_ctxt[index];
85 LASSERT(ctxt->loc_obd == obd);
86 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
87 LASSERT(ctxt->loc_logops == op);
91 OBD_ALLOC(ctxt, sizeof(*ctxt));
93 GOTO(out, rc = -ENOMEM);
96 llogs->llog_ctxt[index] = ctxt;
98 if (!obd->obd_llog_ctxt[index])
99 obd->obd_llog_ctxt[index] = ctxt;
102 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
103 ctxt->loc_idx = index;
104 ctxt->loc_logops = op;
105 sema_init(&ctxt->loc_sem, 1);
108 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
111 obd->obd_llog_ctxt[index] = NULL;
112 class_export_put(ctxt->loc_exp);
113 OBD_FREE(ctxt, sizeof(*ctxt));
119 EXPORT_SYMBOL(llog_setup);
121 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
129 if (CTXTP(ctxt, sync))
130 rc = CTXTP(ctxt, sync)(ctxt, exp);
134 EXPORT_SYMBOL(llog_sync);
136 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
137 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
148 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
150 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
153 EXPORT_SYMBOL(llog_add);
155 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
156 int count, struct llog_cookie *cookies, int flags)
166 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
167 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
170 EXPORT_SYMBOL(llog_cancel);
172 /* callback func for llog_process in llog_obd_origin_setup */
173 static int cat_cancel_cb(struct llog_handle *cathandle,
174 struct llog_rec_hdr *rec, void *data)
176 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
177 struct llog_handle *loghandle;
178 struct llog_log_hdr *llh;
182 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
183 CERROR("invalid record in catalog\n");
186 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
187 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
188 rec->lrh_index, cathandle->lgh_id.lgl_oid);
190 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
192 CERROR("Cannot find handle for log "LPX64"\n",
193 lir->lid_id.lgl_oid);
197 llh = loghandle->lgh_hdr;
198 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
199 (llh->llh_count == 1)) {
200 rc = llog_destroy(loghandle);
202 CERROR("failure destroying log in postsetup: %d\n", rc);
204 index = loghandle->u.phd.phd_cookie.lgc_index;
205 llog_free_handle(loghandle);
208 llog_cat_set_first_idx(cathandle, index);
209 rc = llog_cancel_rec(cathandle, index);
211 CWARN("cancel log "LPX64":%x at index %u of catalog "
212 LPX64"\n", lir->lid_id.lgl_oid,
213 lir->lid_id.lgl_ogen, rec->lrh_index,
214 cathandle->lgh_id.lgl_oid);
220 /* lop_setup method for filter/osc */
221 // XXX how to set exports
222 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
223 int index, struct obd_device *disk_obd, int count,
224 struct llog_logid *logid)
226 struct llog_ctxt *ctxt;
227 struct llog_handle *handle;
228 struct lvfs_run_ctxt saved;
238 ctxt = llog_get_context(obd, index);
240 ctxt = llog_get_context_from_llogs(llogs, index);
246 rc = llog_create(ctxt, &handle, logid, NULL);
248 rc = llog_create(ctxt, &handle, NULL, NULL);
250 *logid = handle->lgh_id;
255 ctxt->loc_handle = handle;
256 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
257 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
258 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
262 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
264 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
268 EXPORT_SYMBOL(llog_obd_origin_setup);
270 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
272 struct llog_handle *cathandle, *n, *loghandle;
273 struct llog_log_hdr *llh;
280 cathandle = ctxt->loc_handle;
282 list_for_each_entry_safe(loghandle, n,
283 &cathandle->u.chd.chd_head,
285 llh = loghandle->lgh_hdr;
286 if ((llh->llh_flags &
287 LLOG_F_ZAP_WHEN_EMPTY) &&
288 (llh->llh_count == 1)) {
289 rc = llog_destroy(loghandle);
291 CERROR("failure destroying log during "
292 "cleanup: %d\n", rc);
294 index = loghandle->u.phd.phd_cookie.lgc_index;
295 llog_free_handle(loghandle);
298 llog_cat_set_first_idx(cathandle, index);
299 rc = llog_cancel_rec(cathandle, index);
301 CDEBUG(D_HA, "cancel plain log at index"
302 " %u of catalog "LPX64"\n",
303 index,cathandle->lgh_id.lgl_oid);
306 llog_cat_put(ctxt->loc_handle);
310 EXPORT_SYMBOL(llog_obd_origin_cleanup);
312 /* add for obdfilter/sz and mds/unlink */
313 int llog_obd_origin_add(struct llog_ctxt *ctxt,
314 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
315 struct llog_cookie *logcookies, int numcookies)
317 struct llog_handle *cathandle;
321 cathandle = ctxt->loc_handle;
322 LASSERT(cathandle != NULL);
323 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
325 CERROR("write one catalog record failed: %d\n", rc);
328 EXPORT_SYMBOL(llog_obd_origin_add);
330 int llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs,
331 int count, struct obd_uuid *uuid)
333 char name[32] = CATLIST;
334 struct llog_catid *idarray;
335 int size = sizeof(*idarray) * count;
339 OBD_ALLOC(idarray, size);
343 rc = llog_get_cat_list(obd, obd, name, count, idarray);
345 CERROR("rc: %d\n", rc);
349 rc = obd_llog_init(obd, llogs, obd, count, idarray, uuid);
351 CERROR("rc: %d\n", rc);
355 rc = llog_put_cat_list(obd, obd, name, count, idarray);
357 CERROR("rc: %d\n", rc);
362 OBD_FREE(idarray, size);
365 EXPORT_SYMBOL(llog_cat_initialize);
367 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
368 struct obd_device *disk_obd, int count,
369 struct llog_catid *logid, struct obd_uuid *uuid)
373 OBD_CHECK_DT_OP(obd, llog_init, 0);
374 OBD_COUNTER_INCREMENT(obd, llog_init);
376 rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid,
380 EXPORT_SYMBOL(obd_llog_init);
382 int obd_llog_finish(struct obd_device *obd, int count)
386 OBD_CHECK_DT_OP(obd, llog_finish, 0);
387 OBD_COUNTER_INCREMENT(obd, llog_finish);
389 rc = OBP(obd, llog_finish)(obd, count);
392 EXPORT_SYMBOL(obd_llog_finish);