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 [sun.com URL with a
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 */
97 LASSERT(obd->obd_stopping == 1 || obd->obd_set_up == 0);
98 /* cleanup the llog ctxt here */
99 if (CTXTP(ctxt, cleanup))
100 rc = CTXTP(ctxt, cleanup)(ctxt);
102 llog_ctxt_destroy(ctxt);
103 wake_up(&olg->olg_waitq);
106 EXPORT_SYMBOL(__llog_ctxt_put);
108 int llog_cleanup(struct llog_ctxt *ctxt)
110 struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
111 struct obd_llog_group *olg;
123 /* banlance the ctxt get when calling llog_cleanup */
124 LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
127 /* try to free the ctxt */
128 rc = __llog_ctxt_put(ctxt);
130 l_wait_event(olg->olg_waitq,
131 llog_group_ctxt_null(olg, idx), &lwi);
135 EXPORT_SYMBOL(llog_cleanup);
137 int llog_setup(struct obd_device *obd, struct obd_llog_group *olg, int index,
138 struct obd_device *disk_obd, int count, struct llog_logid *logid,
139 struct llog_operations *op)
142 struct llog_ctxt *ctxt;
145 if (index < 0 || index >= LLOG_MAX_CTXTS)
148 LASSERT(olg != NULL);
149 ctxt = llog_group_get_ctxt(olg, index);
151 /* in some recovery cases, obd_llog_ctxt might already be set,
152 * but llogs might still be zero, for example in obd_filter recovery */
154 /* mds_lov_update_mds might call here multiple times. So if the
155 llog is already set up then don't to do it again. */
156 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
157 obd->obd_name, index);
158 LASSERT(ctxt->loc_olg == olg);
159 LASSERT(ctxt->loc_obd == obd);
160 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
161 LASSERT(ctxt->loc_logops == op);
165 ctxt = llog_new_ctxt(obd);
167 GOTO(out, rc = -ENOMEM);
169 rc = llog_group_set_ctxt(olg, ctxt, index);
171 llog_ctxt_destroy(ctxt);
177 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
179 ctxt->loc_idx = index;
180 ctxt->loc_logops = op;
181 sema_init(&ctxt->loc_sem, 1);
184 rc = op->lop_setup(obd, olg, index, disk_obd, count, logid);
187 llog_ctxt_destroy(ctxt);
192 EXPORT_SYMBOL(llog_setup);
194 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
202 if (CTXTP(ctxt, sync))
203 rc = CTXTP(ctxt, sync)(ctxt, exp);
207 EXPORT_SYMBOL(llog_sync);
209 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
210 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
222 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
223 cap = current->cap_effective;
224 cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
225 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
226 current->cap_effective = cap;
229 EXPORT_SYMBOL(llog_add);
231 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
232 int count, struct llog_cookie *cookies, int flags)
242 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
243 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
246 EXPORT_SYMBOL(llog_cancel);
248 /* callback func for llog_process in llog_obd_origin_setup */
249 static int cat_cancel_cb(struct llog_handle *cathandle,
250 struct llog_rec_hdr *rec, void *data)
252 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
253 struct llog_handle *loghandle;
254 struct llog_log_hdr *llh;
258 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
259 CERROR("invalid record in catalog\n");
262 CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
263 LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
264 rec->lrh_index, cathandle->lgh_id.lgl_oid);
266 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
268 CERROR("Cannot find handle for log "LPX64"\n",
269 lir->lid_id.lgl_oid);
273 llh = loghandle->lgh_hdr;
274 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
275 (llh->llh_count == 1)) {
276 rc = llog_destroy(loghandle);
278 CERROR("failure destroying log in postsetup: %d\n", rc);
280 index = loghandle->u.phd.phd_cookie.lgc_index;
281 llog_free_handle(loghandle);
284 llog_cat_set_first_idx(cathandle, index);
285 rc = llog_cancel_rec(cathandle, index);
287 CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
288 LPX64"\n", lir->lid_id.lgl_oid,
289 lir->lid_id.lgl_ogen, rec->lrh_index,
290 cathandle->lgh_id.lgl_oid);
296 /* lop_setup method for filter/osc */
297 // XXX how to set exports
298 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llog_group *olg,
299 int index, struct obd_device *disk_obd, int count,
300 struct llog_logid *logid)
302 struct llog_ctxt *ctxt;
303 struct llog_handle *handle;
304 struct lvfs_run_ctxt saved;
313 LASSERT(olg != NULL);
314 ctxt = llog_group_get_ctxt(olg, index);
320 rc = llog_create(ctxt, &handle, logid, NULL);
322 rc = llog_create(ctxt, &handle, NULL, NULL);
324 *logid = handle->lgh_id;
329 ctxt->loc_handle = handle;
330 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
331 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
332 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
336 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
338 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
343 EXPORT_SYMBOL(llog_obd_origin_setup);
345 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
347 struct llog_handle *cathandle, *n, *loghandle;
348 struct llog_log_hdr *llh;
355 cathandle = ctxt->loc_handle;
357 list_for_each_entry_safe(loghandle, n,
358 &cathandle->u.chd.chd_head,
360 llh = loghandle->lgh_hdr;
361 if ((llh->llh_flags &
362 LLOG_F_ZAP_WHEN_EMPTY) &&
363 (llh->llh_count == 1)) {
364 rc = llog_destroy(loghandle);
366 CERROR("failure destroying log during "
367 "cleanup: %d\n", rc);
369 index = loghandle->u.phd.phd_cookie.lgc_index;
370 llog_free_handle(loghandle);
373 llog_cat_set_first_idx(cathandle, index);
374 rc = llog_cancel_rec(cathandle, index);
376 CDEBUG(D_RPCTRACE, "cancel plain log at"
377 "index %u of catalog "LPX64"\n",
378 index,cathandle->lgh_id.lgl_oid);
381 llog_cat_put(ctxt->loc_handle);
385 EXPORT_SYMBOL(llog_obd_origin_cleanup);
387 /* add for obdfilter/sz and mds/unlink */
388 int llog_obd_origin_add(struct llog_ctxt *ctxt,
389 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
390 struct llog_cookie *logcookies, int numcookies)
392 struct llog_handle *cathandle;
396 cathandle = ctxt->loc_handle;
397 LASSERT(cathandle != NULL);
398 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
400 CERROR("write one catalog record failed: %d\n", rc);
403 EXPORT_SYMBOL(llog_obd_origin_add);
405 int llog_cat_initialize(struct obd_device *obd, struct obd_llog_group *olg,
406 int count, struct obd_uuid *uuid)
408 char name[32] = CATLIST;
409 struct llog_catid *idarray = NULL;
410 int size = sizeof(*idarray) * count;
415 OBD_VMALLOC(idarray, size);
420 rc = llog_get_cat_list(obd, obd, name, count, idarray);
422 CERROR("rc: %d\n", rc);
426 rc = obd_llog_init(obd, olg, obd, count, idarray, uuid);
428 CERROR("rc: %d\n", rc);
432 rc = llog_put_cat_list(obd, obd, name, count, idarray);
434 CERROR("rc: %d\n", rc);
440 OBD_VFREE(idarray, size);
443 EXPORT_SYMBOL(llog_cat_initialize);
445 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
446 struct obd_device *disk_obd, int count,
447 struct llog_catid *logid, struct obd_uuid *uuid)
451 OBD_CHECK_DT_OP(obd, llog_init, 0);
452 OBD_COUNTER_INCREMENT(obd, llog_init);
454 rc = OBP(obd, llog_init)(obd, olg, disk_obd, count, logid, uuid);
457 EXPORT_SYMBOL(obd_llog_init);
459 int obd_llog_finish(struct obd_device *obd, int count)
463 OBD_CHECK_DT_OP(obd, llog_finish, 0);
464 OBD_COUNTER_INCREMENT(obd, llog_finish);
466 rc = OBP(obd, llog_finish)(obd, count);
469 EXPORT_SYMBOL(obd_llog_finish);