2 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3 * vim:expandtab:shiftwidth=8:tabstop=8:
5 * You should have received a copy of the GNU General Public License
6 * along with Lustre; if not, write to the Free Software
7 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
10 #define DEBUG_SUBSYSTEM S_LOG
19 #include <liblustre.h>
22 #include <linux/obd_class.h>
23 #include <linux/lustre_log.h>
24 #include <portals/list.h>
25 #include "llog_internal.h"
27 /* helper functions for calling the llog obd methods */
29 int llog_setup(struct obd_device *obd, struct obd_llogs *llogs, int index,
30 struct obd_device *disk_obd, int count, struct llog_logid *logid,
31 struct llog_operations *op)
34 struct llog_ctxt *ctxt;
39 if (index < 0 || index >= LLOG_MAX_CTXTS)
42 OBD_ALLOC(ctxt, sizeof(*ctxt));
46 llogs->llog_ctxt[index] = ctxt;
48 ctxt->loc_llogs = llogs;
49 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
50 ctxt->loc_idx = index;
51 ctxt->loc_logops = op;
52 sema_init(&ctxt->loc_sem, 1);
55 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
57 OBD_FREE(ctxt, sizeof(*ctxt));
61 EXPORT_SYMBOL(llog_setup);
63 int llog_cleanup(struct llog_ctxt *ctxt)
70 if (CTXTP(ctxt, cleanup))
71 rc = CTXTP(ctxt, cleanup)(ctxt);
73 ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
74 class_export_put(ctxt->loc_exp);
76 OBD_FREE(ctxt, sizeof(*ctxt));
80 EXPORT_SYMBOL(llog_cleanup);
82 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
90 if (CTXTP(ctxt, sync))
91 rc = CTXTP(ctxt, sync)(ctxt, exp);
95 EXPORT_SYMBOL(llog_sync);
97 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
98 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
105 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
107 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
110 EXPORT_SYMBOL(llog_add);
112 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
113 int count, struct llog_cookie *cookies, int flags)
119 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
120 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
123 EXPORT_SYMBOL(llog_cancel);
125 /* callback func for llog_process in llog_obd_origin_setup */
126 static int cat_cancel_cb(struct llog_handle *cathandle,
127 struct llog_rec_hdr *rec, void *data)
129 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
130 struct llog_handle *loghandle;
131 struct llog_log_hdr *llh;
135 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
136 CERROR("invalid record in catalog\n");
139 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
140 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
141 rec->lrh_index, cathandle->lgh_id.lgl_oid);
143 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
145 CERROR("Cannot find handle for log "LPX64"\n",
146 lir->lid_id.lgl_oid);
150 llh = loghandle->lgh_hdr;
151 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
152 (llh->llh_count == 1)) {
153 rc = llog_destroy(loghandle);
155 CERROR("failure destroying log in postsetup: %d\n", rc);
158 index = loghandle->u.phd.phd_cookie.lgc_index;
159 llog_free_handle(loghandle);
162 llog_cat_set_first_idx(cathandle, index);
163 rc = llog_cancel_rec(cathandle, index);
165 CWARN("cancel log "LPX64":%x at index %u of catalog "
166 LPX64"\n", lir->lid_id.lgl_oid,
167 lir->lid_id.lgl_ogen, rec->lrh_index,
168 cathandle->lgh_id.lgl_oid);
174 /* lop_setup method for filter/osc */
175 // XXX how to set exports
176 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
177 int index, struct obd_device *disk_obd, int count,
178 struct llog_logid *logid)
180 struct llog_ctxt *ctxt;
181 struct llog_handle *handle;
182 struct obd_run_ctxt saved;
191 ctxt = llog_get_context(llogs, index);
196 rc = llog_create(ctxt, &handle, logid, NULL);
198 rc = llog_create(ctxt, &handle, NULL, NULL);
200 *logid = handle->lgh_id;
205 ctxt->loc_handle = handle;
206 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
207 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
208 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
212 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
214 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
217 llogs->llog_ctxt[index] = NULL;
218 OBD_FREE(ctxt, sizeof(*ctxt));
222 EXPORT_SYMBOL(llog_obd_origin_setup);
224 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
226 struct llog_handle *cathandle, *n, *loghandle;
227 struct llog_log_hdr *llh;
234 cathandle = ctxt->loc_handle;
236 list_for_each_entry_safe(loghandle, n,
237 &cathandle->u.chd.chd_head,
239 llh = loghandle->lgh_hdr;
240 if ((llh->llh_flags &
241 LLOG_F_ZAP_WHEN_EMPTY) &&
242 (llh->llh_count == 1)) {
243 rc = llog_destroy(loghandle);
245 CERROR("failure destroying log during "
246 "cleanup: %d\n", rc);
249 index = loghandle->u.phd.phd_cookie.lgc_index;
250 llog_free_handle(loghandle);
253 llog_cat_set_first_idx(cathandle, index);
254 rc = llog_cancel_rec(cathandle, index);
256 CDEBUG(D_HA, "cancel plain log at index"
257 " %u of catalog "LPX64"\n",
258 index,cathandle->lgh_id.lgl_oid);
261 llog_cat_put(ctxt->loc_handle);
265 EXPORT_SYMBOL(llog_obd_origin_cleanup);
267 /* add for obdfilter/sz and mds/unlink */
268 int llog_obd_origin_add(struct llog_ctxt *ctxt,
269 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
270 struct llog_cookie *logcookies, int numcookies)
272 struct llog_handle *cathandle;
276 cathandle = ctxt->loc_handle;
277 LASSERT(cathandle != NULL);
278 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
280 CERROR("write one catalog record failed: %d\n", rc);
283 EXPORT_SYMBOL(llog_obd_origin_add);
285 int llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs, int count)
287 struct llog_catid *idarray;
288 int size = sizeof(*idarray) * count;
289 char name[32] = CATLIST;
293 OBD_ALLOC(idarray, size);
297 rc = llog_get_cat_list(obd, obd, name, count, idarray);
299 CERROR("rc: %d\n", rc);
303 rc = obd_llog_init(obd, llogs, obd, count, idarray);
305 CERROR("rc: %d\n", rc);
309 rc = llog_put_cat_list(obd, obd, name, count, idarray);
311 CERROR("rc: %d\n", rc);
316 OBD_FREE(idarray, size);
319 EXPORT_SYMBOL(llog_cat_initialize);
321 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
322 struct obd_device *disk_obd, int count,
323 struct llog_catid *logid)
327 OBD_CHECK_OP(obd, llog_init, 0);
328 OBD_COUNTER_INCREMENT(obd, llog_init);
330 rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
333 EXPORT_SYMBOL(obd_llog_init);
335 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
339 OBD_CHECK_OP(obd, llog_finish, 0);
340 OBD_COUNTER_INCREMENT(obd, llog_finish);
342 rc = OBP(obd, llog_finish)(obd, llogs, count);
345 EXPORT_SYMBOL(obd_llog_finish);