4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Whamcloud, Inc.
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
40 #include <liblustre.h>
43 #include <obd_class.h>
44 #include <lustre_log.h>
45 #include <libcfs/list.h>
46 #include "llog_internal.h"
48 /* helper functions for calling the llog obd methods */
49 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
51 struct llog_ctxt *ctxt;
58 cfs_atomic_set(&ctxt->loc_refcount, 1);
63 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
66 class_export_put(ctxt->loc_exp);
70 class_import_put(ctxt->loc_imp);
73 LASSERT(ctxt->loc_llcd == NULL);
77 int __llog_ctxt_put(struct llog_ctxt *ctxt)
79 struct obd_llog_group *olg = ctxt->loc_olg;
80 struct obd_device *obd;
83 cfs_spin_lock(&olg->olg_lock);
84 if (!cfs_atomic_dec_and_test(&ctxt->loc_refcount)) {
85 cfs_spin_unlock(&olg->olg_lock);
88 olg->olg_ctxts[ctxt->loc_idx] = NULL;
89 cfs_spin_unlock(&olg->olg_lock);
92 lcm_put(ctxt->loc_lcm);
95 cfs_spin_lock(&obd->obd_dev_lock);
96 /* sync with llog ctxt user thread */
97 cfs_spin_unlock(&obd->obd_dev_lock);
99 /* obd->obd_starting is needed for the case of cleanup
100 * in error case while obd is starting up. */
101 LASSERTF(obd->obd_starting == 1 ||
102 obd->obd_stopping == 1 || obd->obd_set_up == 0,
103 "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
104 !!obd->obd_stopping, !!obd->obd_set_up);
106 /* cleanup the llog ctxt here */
107 if (CTXTP(ctxt, cleanup))
108 rc = CTXTP(ctxt, cleanup)(ctxt);
110 llog_ctxt_destroy(ctxt);
111 cfs_waitq_signal(&olg->olg_waitq);
114 EXPORT_SYMBOL(__llog_ctxt_put);
116 int llog_cleanup(struct llog_ctxt *ctxt)
118 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
119 struct obd_llog_group *olg;
123 LASSERT(ctxt != NULL);
124 LASSERT(ctxt != LP_POISON);
127 LASSERT(olg != NULL);
128 LASSERT(olg != LP_POISON);
133 * Banlance the ctxt get when calling llog_cleanup()
135 LASSERT(cfs_atomic_read(&ctxt->loc_refcount) < LI_POISON);
136 LASSERT(cfs_atomic_read(&ctxt->loc_refcount) > 1);
140 * Try to free the ctxt.
142 rc = __llog_ctxt_put(ctxt);
144 CERROR("Error %d while cleaning up ctxt %p\n",
147 l_wait_event(olg->olg_waitq,
148 llog_group_ctxt_null(olg, idx), &lwi);
152 EXPORT_SYMBOL(llog_cleanup);
154 int llog_setup_named(struct obd_device *obd, struct obd_llog_group *olg,
155 int index, struct obd_device *disk_obd, int count,
156 struct llog_logid *logid, const char *logname,
157 struct llog_operations *op)
159 struct llog_ctxt *ctxt;
163 if (index < 0 || index >= LLOG_MAX_CTXTS)
166 LASSERT(olg != NULL);
168 ctxt = llog_new_ctxt(obd);
174 ctxt->loc_idx = index;
175 ctxt->loc_logops = op;
176 cfs_mutex_init(&ctxt->loc_mutex);
177 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
178 ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
180 rc = llog_group_set_ctxt(olg, ctxt, index);
182 llog_ctxt_destroy(ctxt);
184 ctxt = llog_group_get_ctxt(olg, index);
187 * mds_lov_update_desc() might call here multiple
188 * times. So if the llog is already set up then
189 * don't to do it again.
191 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
192 obd->obd_name, index);
193 LASSERT(ctxt->loc_olg == olg);
194 LASSERT(ctxt->loc_obd == obd);
195 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
196 LASSERT(ctxt->loc_logops == op);
204 if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP)) {
208 rc = op->lop_setup(obd, olg, index, disk_obd, count,
213 CERROR("obd %s ctxt %d lop_setup=%p failed %d\n",
214 obd->obd_name, index, op->lop_setup, rc);
217 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
218 obd->obd_name, index);
219 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
224 EXPORT_SYMBOL(llog_setup_named);
226 int llog_setup(struct obd_device *obd, struct obd_llog_group *olg,
227 int index, struct obd_device *disk_obd, int count,
228 struct llog_logid *logid, struct llog_operations *op)
230 return llog_setup_named(obd,olg,index,disk_obd,count,logid,NULL,op);
232 EXPORT_SYMBOL(llog_setup);
234 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp, int flags)
242 if (CTXTP(ctxt, sync))
243 rc = CTXTP(ctxt, sync)(ctxt, exp, flags);
247 EXPORT_SYMBOL(llog_sync);
249 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
250 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
261 if (ctxt->loc_flags & LLOG_CTXT_FLAG_UNINITIALIZED)
265 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
266 raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
268 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
269 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
271 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
274 EXPORT_SYMBOL(llog_add);
276 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
277 int count, struct llog_cookie *cookies, int flags)
287 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
288 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
291 EXPORT_SYMBOL(llog_cancel);
293 /* callback func for llog_process in llog_obd_origin_setup */
294 static int cat_cancel_cb(struct llog_handle *cathandle,
295 struct llog_rec_hdr *rec, void *data)
297 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
298 struct llog_handle *loghandle;
299 struct llog_log_hdr *llh;
303 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
304 CERROR("invalid record in catalog\n");
307 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
308 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
309 rec->lrh_index, cathandle->lgh_id.lgl_oid);
311 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
313 CERROR("Cannot find handle for log "LPX64"\n",
314 lir->lid_id.lgl_oid);
316 index = rec->lrh_index;
322 llh = loghandle->lgh_hdr;
323 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
324 (llh->llh_count == 1)) {
325 rc = llog_destroy(loghandle);
327 CERROR("failure destroying log in postsetup: %d\n", rc);
329 index = loghandle->u.phd.phd_cookie.lgc_index;
330 llog_free_handle(loghandle);
334 llog_cat_set_first_idx(cathandle, index);
335 rc = llog_cancel_rec(cathandle, index);
337 CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
338 LPX64"\n", lir->lid_id.lgl_oid,
339 lir->lid_id.lgl_ogen, rec->lrh_index,
340 cathandle->lgh_id.lgl_oid);
346 /* lop_setup method for filter/osc */
347 // XXX how to set exports
348 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llog_group *olg,
349 int index, struct obd_device *disk_obd, int count,
350 struct llog_logid *logid, const char *name)
352 struct llog_ctxt *ctxt;
353 struct llog_handle *handle;
354 struct lvfs_run_ctxt saved;
363 LASSERT(olg != NULL);
364 ctxt = llog_group_get_ctxt(olg, index);
368 if (logid && logid->lgl_oid) {
369 rc = llog_create(ctxt, &handle, logid, NULL);
371 rc = llog_create(ctxt, &handle, NULL, (char *)name);
373 *logid = handle->lgh_id;
378 ctxt->loc_handle = handle;
379 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
380 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
381 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
385 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
387 CERROR("llog_process() with cat_cancel_cb failed: %d\n", rc);
393 EXPORT_SYMBOL(llog_obd_origin_setup);
395 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
397 struct llog_handle *cathandle, *n, *loghandle;
398 struct llog_log_hdr *llh;
405 cathandle = ctxt->loc_handle;
407 cfs_list_for_each_entry_safe(loghandle, n,
408 &cathandle->u.chd.chd_head,
410 llh = loghandle->lgh_hdr;
411 if ((llh->llh_flags &
412 LLOG_F_ZAP_WHEN_EMPTY) &&
413 (llh->llh_count == 1)) {
414 rc = llog_destroy(loghandle);
416 CERROR("failure destroying log during "
417 "cleanup: %d\n", rc);
419 index = loghandle->u.phd.phd_cookie.lgc_index;
420 llog_free_handle(loghandle);
423 llog_cat_set_first_idx(cathandle, index);
424 rc = llog_cancel_rec(cathandle, index);
426 CDEBUG(D_RPCTRACE, "cancel plain log at"
427 "index %u of catalog "LPX64"\n",
428 index,cathandle->lgh_id.lgl_oid);
431 llog_cat_put(ctxt->loc_handle);
435 EXPORT_SYMBOL(llog_obd_origin_cleanup);
437 /* add for obdfilter/sz and mds/unlink */
438 int llog_obd_origin_add(struct llog_ctxt *ctxt,
439 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
440 struct llog_cookie *logcookies, int numcookies)
442 struct llog_handle *cathandle;
446 cathandle = ctxt->loc_handle;
447 LASSERT(cathandle != NULL);
448 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
449 if (rc != 0 && rc != 1)
450 CERROR("write one catalog record failed: %d\n", rc);
453 EXPORT_SYMBOL(llog_obd_origin_add);
455 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
456 struct obd_device *disk_obd, int *index)
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, index);
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);