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 <linux/lustre_mds.h>
25 #include <portals/list.h>
27 /* helper functions for calling the llog obd methods */
29 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *llogs,
30 int index, struct obd_device *disk_obd, int count,
31 struct llog_logid *logid, 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;
47 obd->obd_llog_ctxt[index] = ctxt;
48 ctxt->loc_logops = op;
50 ctxt->loc_llogs = llogs;
51 ctxt->loc_idx = index;
53 ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
54 sema_init(&ctxt->loc_sem, 1);
56 if (op->lop_close == llog_lvfs_ops.lop_close) {
57 ctxt->loc_fsops = disk_obd->obd_fsops;
58 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
59 if (!strcmp(disk_obd->obd_type->typ_name, LUSTRE_MDS_NAME)) {
60 struct mds_obd *mds = &disk_obd->u.mds;
61 ctxt->loc_objects_dir = mds->mds_objects_dir;
62 ctxt->loc_logs_dir = mds->mds_logs_dir;
67 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
69 obd->obd_llog_ctxt[index] = NULL;
70 OBD_FREE(ctxt, sizeof(*ctxt));
75 EXPORT_SYMBOL(obd_llog_setup);
77 int obd_llog_cleanup(struct llog_ctxt *ctxt)
85 if (CTXTP(ctxt, cleanup))
86 rc = CTXTP(ctxt, cleanup)(ctxt);
88 ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
89 ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
90 class_export_put(ctxt->loc_exp);
92 OBD_FREE(ctxt, sizeof(*ctxt));
95 EXPORT_SYMBOL(obd_llog_cleanup);
97 /* callback func for llog_process in llog_obd_origin_setup */
98 static int cat_cancel_cb(struct llog_handle *cathandle,
99 struct llog_rec_hdr *rec, void *data)
101 struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
102 struct llog_handle *loghandle;
103 struct llog_log_hdr *llh;
107 if (rec->lrh_type != LLOG_LOGID_MAGIC) {
108 CERROR("invalid record in catalog\n");
111 CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
112 lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
113 rec->lrh_index, cathandle->lgh_id.lgl_oid);
115 rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
117 CERROR("Cannot find handle for log "LPX64"\n",
118 lir->lid_id.lgl_oid);
122 llh = loghandle->lgh_hdr;
123 if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
124 (llh->llh_count == 1)) {
125 rc = llog_destroy(loghandle);
127 CERROR("failure destroying log in postsetup: %d\n", rc);
130 index = loghandle->u.phd.phd_cookie.lgc_index;
131 llog_free_handle(loghandle);
134 llog_cat_set_first_idx(cathandle, index);
135 rc = llog_cancel_rec(cathandle, index);
137 CWARN("cancel log "LPX64":%x at index %u of catalog "
138 LPX64"\n", lir->lid_id.lgl_oid,
139 lir->lid_id.lgl_ogen, rec->lrh_index,
140 cathandle->lgh_id.lgl_oid);
146 /* lop_setup method for filter/osc */
147 // XXX how to set exports
148 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
149 int index, struct obd_device *disk_obd, int count,
150 struct llog_logid *logid)
152 struct llog_ctxt *ctxt;
153 struct llog_handle *handle;
154 struct lvfs_run_ctxt saved;
163 ctxt = llog_get_context(llogs, index);
167 if (logid->lgl_oid) {
168 rc = llog_open(ctxt, &handle, logid, NULL, 0);
170 rc = llog_open(ctxt, &handle, NULL, NULL, 0);
172 *logid = handle->lgh_id;
177 ctxt->loc_handle = handle;
178 push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
179 rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
180 pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
184 rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
186 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
189 llogs->llog_ctxt[index] = NULL;
190 OBD_FREE(ctxt, sizeof(*ctxt));
194 EXPORT_SYMBOL(llog_obd_origin_setup);
196 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
198 struct llog_handle *cathandle, *n, *loghandle;
199 struct llog_log_hdr *llh;
206 cathandle = ctxt->loc_handle;
208 list_for_each_entry_safe(loghandle, n,
209 &cathandle->u.chd.chd_head,
211 llh = loghandle->lgh_hdr;
212 if ((llh->llh_flags &
213 LLOG_F_ZAP_WHEN_EMPTY) &&
214 (llh->llh_count == 1)) {
215 rc = llog_destroy(loghandle);
217 CERROR("failure destroying log during "
218 "cleanup: %d\n", rc);
221 index = loghandle->u.phd.phd_cookie.lgc_index;
222 llog_free_handle(loghandle);
225 llog_cat_set_first_idx(cathandle, index);
226 rc = llog_cancel_rec(cathandle, index);
228 CDEBUG(D_HA, "cancel plain log at index"
229 " %u of catalog "LPX64"\n",
230 index,cathandle->lgh_id.lgl_oid);
233 llog_cat_put(ctxt->loc_handle);
237 EXPORT_SYMBOL(llog_obd_origin_cleanup);
239 /* add for obdfilter/sz and mds/unlink */
240 int llog_obd_origin_add(struct llog_ctxt *ctxt,
241 struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
242 struct llog_cookie *logcookies, int numcookies)
244 struct llog_handle *cathandle;
248 cathandle = ctxt->loc_handle;
249 LASSERT(cathandle != NULL);
250 rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
252 CERROR("write one catalog record failed: %d\n", rc);
255 EXPORT_SYMBOL(llog_obd_origin_add);
257 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs,
258 int count, char *name)
260 struct llog_catid *idarray;
261 int size = sizeof(*idarray) * count;
265 OBD_ALLOC(idarray, size);
269 rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
270 name, count, idarray);
272 CERROR("rc: %d\n", rc);
276 rc = obd_llog_init(obd, llogs, obd, count, idarray);
278 CERROR("rc: %d\n", rc);
282 rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
283 name, count, idarray);
285 CERROR("rc: %d\n", rc);
290 OBD_FREE(idarray, size);
293 EXPORT_SYMBOL(obd_llog_cat_initialize);
295 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
296 struct obd_device *disk_obd, int count,
297 struct llog_catid *logid)
301 OBD_CHECK_OP(obd, llog_init, 0);
302 OBD_COUNTER_INCREMENT(obd, llog_init);
304 rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
307 EXPORT_SYMBOL(obd_llog_init);
309 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
313 OBD_CHECK_OP(obd, llog_finish, 0);
314 OBD_COUNTER_INCREMENT(obd, llog_finish);
316 rc = OBP(obd, llog_finish)(obd, llogs, count);
319 EXPORT_SYMBOL(obd_llog_finish);