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>
26 /* helper functions for calling the llog obd methods */
28 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *llogs,
29 int index, struct obd_device *disk_obd, int count,
30 struct llog_logid *logid, struct llog_operations *op)
33 struct llog_ctxt *ctxt;
38 if (index < 0 || index >= LLOG_MAX_CTXTS)
41 OBD_ALLOC(ctxt, sizeof(*ctxt));
45 llogs->llog_ctxt[index] = ctxt;
46 obd->obd_llog_ctxt[index] = ctxt;
47 ctxt->loc_logops = op;
49 ctxt->loc_llogs = llogs;
50 ctxt->loc_idx = index;
52 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
53 sema_init(&ctxt->loc_sem, 1);
55 if (op->lop_close == llog_lvfs_ops.lop_close) {
56 ctxt->loc_fsops = disk_obd->obd_fsops;
57 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
58 if (!strcmp(disk_obd->obd_type->typ_name, "mds")) {
59 struct mds_obd *mds = &disk_obd->u.mds;
60 ctxt->loc_objects_dir = mds->mds_objects_dir;
61 ctxt->loc_logs_dir = mds->mds_logs_dir;
66 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
68 obd->obd_llog_ctxt[index] = NULL;
69 OBD_FREE(ctxt, sizeof(*ctxt));
74 EXPORT_SYMBOL(obd_llog_setup);
76 int obd_llog_cleanup(struct llog_ctxt *ctxt)
83 if (CTXTP(ctxt, cleanup))
84 rc = CTXTP(ctxt, cleanup)(ctxt);
86 ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
87 ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
88 class_export_put(ctxt->loc_exp);
90 OBD_FREE(ctxt, sizeof(*ctxt));
93 EXPORT_SYMBOL(obd_llog_cleanup);
95 /* callback func for llog_process in llog_obd_origin_setup */
96 static int cat_cancel_cb(struct llog_handle *cathandle,
97 struct llog_rec_hdr *rec, void *data)
99 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
100 struct llog_handle *loghandle;
101 struct llog_log_hdr *llh;
105 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
106 CERROR("invalid record in catalog\n");
109 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
110 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
111 rec->lrh_index, cathandle->lgh_id.lgl_oid);
113 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
115 CERROR("Cannot find handle for log "LPX64"\n",
116 lir->lid_id.lgl_oid);
120 llh = loghandle->lgh_hdr;
121 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
122 (llh->llh_count == 1)) {
123 rc = llog_destroy(loghandle);
125 CERROR("failure destroying log in postsetup: %d\n", rc);
128 index = loghandle->u.phd.phd_cookie.lgc_index;
129 llog_free_handle(loghandle);
132 llog_cat_set_first_idx(cathandle, index);
133 rc = llog_cancel_rec(cathandle, index);
135 CWARN("cancel log "LPX64":%x at index %u of catalog "
136 LPX64"\n", lir->lid_id.lgl_oid,
137 lir->lid_id.lgl_ogen, rec->lrh_index,
138 cathandle->lgh_id.lgl_oid);
144 /* lop_setup method for filter/osc */
145 // XXX how to set exports
146 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
147 int index, struct obd_device *disk_obd, int count,
148 struct llog_logid *logid)
150 struct llog_ctxt *ctxt;
151 struct llog_handle *handle;
152 struct lvfs_run_ctxt saved;
161 ctxt = llog_get_context(llogs, index);
166 rc = llog_create(ctxt, &handle, logid, NULL);
168 rc = llog_create(ctxt, &handle, NULL, NULL);
170 *logid = handle->lgh_id;
175 ctxt->loc_handle = handle;
176 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
177 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
178 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
182 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
184 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
187 llogs->llog_ctxt[index] = NULL;
188 OBD_FREE(ctxt, sizeof(*ctxt));
192 EXPORT_SYMBOL(llog_obd_origin_setup);
194 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
196 struct llog_handle *cathandle, *n, *loghandle;
197 struct llog_log_hdr *llh;
204 cathandle = ctxt->loc_handle;
206 list_for_each_entry_safe(loghandle, n,
207 &cathandle->u.chd.chd_head,
209 llh = loghandle->lgh_hdr;
210 if ((llh->llh_flags &
211 LLOG_F_ZAP_WHEN_EMPTY) &&
212 (llh->llh_count == 1)) {
213 rc = llog_destroy(loghandle);
215 CERROR("failure destroying log during "
216 "cleanup: %d\n", rc);
219 index = loghandle->u.phd.phd_cookie.lgc_index;
220 llog_free_handle(loghandle);
223 llog_cat_set_first_idx(cathandle, index);
224 rc = llog_cancel_rec(cathandle, index);
226 CDEBUG(D_HA, "cancel plain log at index"
227 " %u of catalog "LPX64"\n",
228 index,cathandle->lgh_id.lgl_oid);
231 llog_cat_put(ctxt->loc_handle);
235 EXPORT_SYMBOL(llog_obd_origin_cleanup);
237 /* add for obdfilter/sz and mds/unlink */
238 int llog_obd_origin_add(struct llog_ctxt *ctxt,
239 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
240 struct llog_cookie *logcookies, int numcookies)
242 struct llog_handle *cathandle;
246 cathandle = ctxt->loc_handle;
247 LASSERT(cathandle != NULL);
248 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
250 CERROR("write one catalog record failed: %d\n", rc);
253 EXPORT_SYMBOL(llog_obd_origin_add);
255 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs,
256 int count, char *name)
258 struct llog_catid *idarray;
259 int size = sizeof(*idarray) * count;
263 OBD_ALLOC(idarray, size);
267 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
268 name, count, idarray);
270 CERROR("rc: %d\n", rc);
274 rc = obd_llog_init(obd, llogs, obd, count, idarray);
276 CERROR("rc: %d\n", rc);
280 rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
281 name, count, idarray);
283 CERROR("rc: %d\n", rc);
288 OBD_FREE(idarray, size);
291 EXPORT_SYMBOL(obd_llog_cat_initialize);
293 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
294 struct obd_device *disk_obd, int count,
295 struct llog_catid *logid)
299 OBD_CHECK_OP(obd, llog_init, 0);
300 OBD_COUNTER_INCREMENT(obd, llog_init);
302 rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
305 EXPORT_SYMBOL(obd_llog_init);
307 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
311 OBD_CHECK_OP(obd, llog_finish, 0);
312 OBD_COUNTER_INCREMENT(obd, llog_finish);
314 rc = OBP(obd, llog_finish)(obd, llogs, count);
317 EXPORT_SYMBOL(obd_llog_finish);