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, int index, struct obd_device *disk_obd,
30 int count, struct llog_logid *logid, struct llog_operations *op)
33 struct llog_ctxt *ctxt;
36 if (index < 0 || index >= LLOG_MAX_CTXTS)
39 OBD_ALLOC(ctxt, sizeof(*ctxt));
43 obd->obd_llog_ctxt[index] = ctxt;
45 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
46 ctxt->loc_idx = index;
47 ctxt->loc_logops = op;
48 sema_init(&ctxt->loc_sem, 1);
51 rc = op->lop_setup(obd, index, disk_obd, count, logid);
53 OBD_FREE(ctxt, sizeof(*ctxt));
57 EXPORT_SYMBOL(llog_setup);
59 int llog_cleanup(struct llog_ctxt *ctxt)
67 if (CTXTP(ctxt, cleanup))
68 rc = CTXTP(ctxt, cleanup)(ctxt);
70 ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
71 class_export_put(ctxt->loc_exp);
74 OBD_FREE(ctxt, sizeof(*ctxt));
78 EXPORT_SYMBOL(llog_cleanup);
80 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
88 if (ctxt->loc_llcd && CTXTP(ctxt, sync))
89 rc = CTXTP(ctxt, sync)(ctxt, exp);
95 EXPORT_SYMBOL(llog_sync);
97 int llog_add(struct llog_ctxt *ctxt,
98 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
99 struct llog_cookie *logcookies, int numcookies)
105 down(&ctxt->loc_sem);
106 CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
108 rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
112 EXPORT_SYMBOL(llog_add);
114 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
115 int count, struct llog_cookie *cookies, int flags)
121 CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
122 rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
125 EXPORT_SYMBOL(llog_cancel);
127 /* callback func for llog_process in llog_obd_origin_setup */
128 static int cat_cancel_cb(struct llog_handle *cathandle,
129 struct llog_rec_hdr *rec, void *data)
131 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
132 struct llog_handle *loghandle;
133 struct llog_log_hdr *llh;
137 if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
138 CERROR("invalid record in catalog\n");
141 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
142 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
143 le32_to_cpu(rec->lrh_index), cathandle->lgh_id.lgl_oid);
145 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
147 CERROR("Cannot find handle for log "LPX64"\n", lir->lid_id.lgl_oid);
151 llh = loghandle->lgh_hdr;
152 if ((le32_to_cpu(llh->llh_flags) & LLOG_F_ZAP_WHEN_EMPTY) &&
153 (le32_to_cpu(llh->llh_count) == 1)) {
154 rc = llog_destroy(loghandle);
156 CERROR("failure destroying log during postsetup: %d\n", rc);
159 index = loghandle->u.phd.phd_cookie.lgc_index;
160 if (cathandle->u.chd.chd_current_log == loghandle)
161 cathandle->u.chd.chd_current_log = NULL;
162 llog_free_handle(loghandle);
165 rc = llog_cancel_rec(cathandle, index);
167 CWARN("cancel log "LPX64":%x at index %u of catalog "LPX64"\n",
168 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
169 le32_to_cpu(rec->lrh_index), cathandle->lgh_id.lgl_oid);
175 /* lop_setup method for filter/osc */
176 // XXX how to set exports
177 int llog_obd_origin_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
178 int count, 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(obd, index);
195 down(&ctxt->loc_sem);
197 rc = llog_create(ctxt, &handle, logid, NULL);
199 rc = llog_create(ctxt, &handle, NULL, NULL);
201 *logid = handle->lgh_id;
206 ctxt->loc_handle = handle;
207 push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
208 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
209 pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
213 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL);
215 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
219 obd->obd_llog_ctxt[index] = NULL;
220 OBD_FREE(ctxt, sizeof(*ctxt));
224 EXPORT_SYMBOL(llog_obd_origin_setup);
226 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
228 struct llog_handle *cathandle, *n, *loghandle;
229 struct llog_log_hdr *llh;
236 cathandle = ctxt->loc_handle;
238 list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
240 llh = loghandle->lgh_hdr;
241 if ((le32_to_cpu(llh->llh_flags) & LLOG_F_ZAP_WHEN_EMPTY) &&
242 (le32_to_cpu(llh->llh_count) == 1)) {
243 rc = llog_destroy(loghandle);
245 CERROR("failure destroying log during cleanup: %d\n",
249 index = loghandle->u.phd.phd_cookie.lgc_index;
250 if (cathandle->u.chd.chd_current_log == loghandle)
251 cathandle->u.chd.chd_current_log = NULL;
252 llog_free_handle(loghandle);
255 rc = llog_cancel_rec(cathandle, index);
257 CWARN("cancel plain log at index %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);
268 /* add for obdfilter/sz and mds/unlink */
269 int llog_obd_origin_add(struct llog_ctxt *ctxt,
270 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
271 struct llog_cookie *logcookies, int numcookies)
273 struct llog_handle *cathandle;
277 cathandle = ctxt->loc_handle;
278 LASSERT(cathandle != NULL);
279 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
281 CERROR("write one catalog record failed: %d\n", rc);
284 EXPORT_SYMBOL(llog_obd_origin_add);
286 int llog_cat_initialize(struct obd_device *obd, int count)
288 struct llog_logid *idarray;
289 int size = sizeof(*idarray) * count;
290 char name[32] = "CATLIST";
294 OBD_ALLOC(idarray, size);
298 memset(idarray, 0, size);
300 rc = llog_get_cat_list(obd, obd, name, count, idarray);
302 CERROR("rc: %d\n", rc);
306 rc = obd_llog_init(obd, obd, count, idarray);
308 CERROR("rc: %d\n", rc);
312 rc = llog_put_cat_list(obd, obd, name, count, idarray);
314 CERROR("rc: %d\n", rc);
319 OBD_FREE(idarray, size);
322 EXPORT_SYMBOL(llog_cat_initialize);
324 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
325 int count, struct llog_logid *logid)
329 OBD_CHECK_OP(obd, llog_init, 0);
330 OBD_COUNTER_INCREMENT(obd, llog_init);
332 rc = OBP(obd, llog_init)(obd, disk_obd, count, logid);
335 EXPORT_SYMBOL(obd_llog_init);
337 int obd_llog_finish(struct obd_device *obd, int count)
341 OBD_CHECK_OP(obd, llog_finish, 0);
342 OBD_COUNTER_INCREMENT(obd, llog_finish);
344 rc = OBP(obd, llog_finish)(obd, count);
347 EXPORT_SYMBOL(obd_llog_finish);