1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
37 #define DEBUG_SUBSYSTEM S_LOG
44 #include <liblustre.h>
47 #include <obd_class.h>
48 #include <lustre_log.h>
49 #include <libcfs/list.h>
50 #include "llog_internal.h"
52 /* helper functions for calling the llog obd methods */
53 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
55 struct llog_ctxt *ctxt;
62 atomic_set(&ctxt->loc_refcount, 1);
67 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
70 class_export_put(ctxt->loc_exp);
72 class_import_put(ctxt->loc_imp);
80 int __llog_ctxt_put(struct llog_ctxt *ctxt)
82 struct obd_llog_group *olg = ctxt->loc_olg;
83 struct obd_device *obd;
86 spin_lock(&olg->olg_lock);
87 if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
88 spin_unlock(&olg->olg_lock);
91 olg->olg_ctxts[ctxt->loc_idx] = NULL;
92 spin_unlock(&olg->olg_lock);
95 spin_lock(&obd->obd_dev_lock);
96 spin_unlock(&obd->obd_dev_lock); /* sync with llog ctxt user thread */
98 /* obd->obd_starting is needed for the case of cleanup
99 * in error case while obd is starting up. */
100 LASSERTF(obd->obd_starting == 1 ||
101 obd->obd_stopping == 1 || obd->obd_set_up == 0,
102 "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
103 !!obd->obd_stopping, !!obd->obd_set_up);
105 /* cleanup the llog ctxt here */
106 if (CTXTP(ctxt, cleanup))
107 rc = CTXTP(ctxt, cleanup)(ctxt);
109 llog_ctxt_destroy(ctxt);
110 wake_up(&olg->olg_waitq);
113 EXPORT_SYMBOL(__llog_ctxt_put);
115 int llog_cleanup(struct llog_ctxt *ctxt)
117 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
118 struct obd_llog_group *olg;
130 /* banlance the ctxt get when calling llog_cleanup */
131 LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
134 /* try to free the ctxt */
135 rc = __llog_ctxt_put(ctxt);
137 CERROR("Error %d while cleaning up ctxt %p\n",
140 l_wait_event(olg->olg_waitq,
141 llog_group_ctxt_null(olg, idx), &lwi);
145 EXPORT_SYMBOL(llog_cleanup);
147 int llog_setup(struct obd_device *obd, struct obd_llog_group *olg, int index,
148 struct obd_device *disk_obd, int count, struct llog_logid *logid,
149 struct llog_operations *op)
152 struct llog_ctxt *ctxt;
155 if (index < 0 || index >= LLOG_MAX_CTXTS)
158 LASSERT(olg != NULL);
160 ctxt = llog_new_ctxt(obd);
162 GOTO(out, rc = -ENOMEM);
165 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
167 ctxt->loc_idx = index;
168 ctxt->loc_logops = op;
169 sema_init(&ctxt->loc_sem, 1);
171 rc = llog_group_set_ctxt(olg, ctxt, index);
173 llog_ctxt_destroy(ctxt);
176 ctxt = llog_group_get_ctxt(olg, index);
178 /* mds_lov_update_mds might call here multiple times.
179 * So if the llog is already set up then don't to do
181 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
182 obd->obd_name, index);
183 LASSERT(ctxt->loc_olg == olg);
184 LASSERT(ctxt->loc_obd == obd);
185 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
186 LASSERT(ctxt->loc_logops == op);
194 rc = op->lop_setup(obd, olg, index, disk_obd, count, logid);
197 llog_ctxt_destroy(ctxt);
202 EXPORT_SYMBOL(llog_setup);
204 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
212 if (CTXTP(ctxt, sync))
213 rc = CTXTP(ctxt, sync)(ctxt, exp);
217 EXPORT_SYMBOL(llog_sync);
219 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
220 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
231 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
232 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
234 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
235 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
237 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
240 EXPORT_SYMBOL(llog_add);
242 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
243 int count, struct llog_cookie *cookies, int flags)
253 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
254 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
257 EXPORT_SYMBOL(llog_cancel);
259 /* callback func for llog_process in llog_obd_origin_setup */
260 static int cat_cancel_cb(struct llog_handle *cathandle,
261 struct llog_rec_hdr *rec, void *data)
263 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
264 struct llog_handle *loghandle;
265 struct llog_log_hdr *llh;
269 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
270 CERROR("invalid record in catalog\n");
273 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
274 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
275 rec->lrh_index, cathandle->lgh_id.lgl_oid);
277 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
279 CERROR("Cannot find handle for log "LPX64"\n",
280 lir->lid_id.lgl_oid);
284 llh = loghandle->lgh_hdr;
285 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
286 (llh->llh_count == 1)) {
287 rc = llog_destroy(loghandle);
289 CERROR("failure destroying log in postsetup: %d\n", rc);
291 index = loghandle->u.phd.phd_cookie.lgc_index;
292 llog_free_handle(loghandle);
295 llog_cat_set_first_idx(cathandle, index);
296 rc = llog_cancel_rec(cathandle, index);
298 CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
299 LPX64"\n", lir->lid_id.lgl_oid,
300 lir->lid_id.lgl_ogen, rec->lrh_index,
301 cathandle->lgh_id.lgl_oid);
307 /* lop_setup method for filter/osc */
308 // XXX how to set exports
309 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llog_group *olg,
310 int index, struct obd_device *disk_obd, int count,
311 struct llog_logid *logid)
313 struct llog_ctxt *ctxt;
314 struct llog_handle *handle;
315 struct lvfs_run_ctxt saved;
324 LASSERT(olg != NULL);
325 ctxt = llog_group_get_ctxt(olg, index);
331 rc = llog_create(ctxt, &handle, logid, NULL);
333 rc = llog_create(ctxt, &handle, NULL, NULL);
335 *logid = handle->lgh_id;
340 ctxt->loc_handle = handle;
341 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
342 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
343 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
347 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
349 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
354 EXPORT_SYMBOL(llog_obd_origin_setup);
356 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
358 struct llog_handle *cathandle, *n, *loghandle;
359 struct llog_log_hdr *llh;
366 cathandle = ctxt->loc_handle;
368 list_for_each_entry_safe(loghandle, n,
369 &cathandle->u.chd.chd_head,
371 llh = loghandle->lgh_hdr;
372 if ((llh->llh_flags &
373 LLOG_F_ZAP_WHEN_EMPTY) &&
374 (llh->llh_count == 1)) {
375 rc = llog_destroy(loghandle);
377 CERROR("failure destroying log during "
378 "cleanup: %d\n", rc);
380 index = loghandle->u.phd.phd_cookie.lgc_index;
381 llog_free_handle(loghandle);
384 llog_cat_set_first_idx(cathandle, index);
385 rc = llog_cancel_rec(cathandle, index);
387 CDEBUG(D_RPCTRACE, "cancel plain log at"
388 "index %u of catalog "LPX64"\n",
389 index,cathandle->lgh_id.lgl_oid);
392 llog_cat_put(ctxt->loc_handle);
396 EXPORT_SYMBOL(llog_obd_origin_cleanup);
398 /* add for obdfilter/sz and mds/unlink */
399 int llog_obd_origin_add(struct llog_ctxt *ctxt,
400 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
401 struct llog_cookie *logcookies, int numcookies)
403 struct llog_handle *cathandle;
407 cathandle = ctxt->loc_handle;
408 LASSERT(cathandle != NULL);
409 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
411 CERROR("write one catalog record failed: %d\n", rc);
414 EXPORT_SYMBOL(llog_obd_origin_add);
416 int llog_cat_initialize(struct obd_device *obd, struct obd_llog_group *olg,
417 int idx, struct obd_uuid *uuid)
419 char name[32] = CATLIST;
420 struct llog_catid idarray;
424 mutex_down(&olg->olg_cat_processing);
425 rc = llog_get_cat_list(obd, obd, name, idx, 1, &idarray);
427 CERROR("rc: %d\n", rc);
431 CDEBUG(D_INFO, "%s: Init llog for %s/%d - catid "LPX64"/"LPX64":%x\n",
432 obd->obd_name, uuid->uuid, idx, idarray.lci_logid.lgl_oid,
433 idarray.lci_logid.lgl_ogr, idarray.lci_logid.lgl_ogen);
435 rc = obd_llog_init(obd, olg, obd, 1, &idarray, uuid);
437 CERROR("rc: %d\n", rc);
441 rc = llog_put_cat_list(obd, obd, name, idx, 1, &idarray);
443 CERROR("rc: %d\n", rc);
448 mutex_up(&olg->olg_cat_processing);
452 EXPORT_SYMBOL(llog_cat_initialize);
454 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
455 struct obd_device *disk_obd, int count,
456 struct llog_catid *logid, struct obd_uuid *uuid)
460 OBD_CHECK_DT_OP(obd, llog_init, 0);
461 OBD_COUNTER_INCREMENT(obd, llog_init);
463 rc = OBP(obd, llog_init)(obd, olg, disk_obd, count, logid, uuid);
466 EXPORT_SYMBOL(obd_llog_init);
468 int obd_llog_finish(struct obd_device *obd, int count)
472 OBD_CHECK_DT_OP(obd, llog_finish, 0);
473 OBD_COUNTER_INCREMENT(obd, llog_finish);
475 rc = OBP(obd, llog_finish)(obd, count);
478 EXPORT_SYMBOL(obd_llog_finish);