Whamcloud - gitweb
land b1_5 onto HEAD
[fs/lustre-release.git] / lustre / obdclass / llog_obd.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2005 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LOG
26
27 #ifndef EXPORT_SYMTAB
28 #define EXPORT_SYMTAB
29 #endif
30
31 #ifndef __KERNEL__
32 #include <liblustre.h>
33 #endif
34
35 #include <obd_class.h>
36 #include <lustre_log.h>
37 #include <libcfs/list.h>
38 #include "llog_internal.h"
39
40 /* helper functions for calling the llog obd methods */
41
42 int llog_cleanup(struct llog_ctxt *ctxt)
43 {
44         int rc = 0;
45         ENTRY;
46
47         if (!ctxt) {
48                 CERROR("No ctxt\n");
49                 RETURN(-ENODEV);
50         }
51         
52         if (CTXTP(ctxt, cleanup))
53                 rc = CTXTP(ctxt, cleanup)(ctxt);
54
55         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
56         if (ctxt->loc_exp)
57                 class_export_put(ctxt->loc_exp);
58         OBD_FREE(ctxt, sizeof(*ctxt));
59
60         RETURN(rc);
61 }
62 EXPORT_SYMBOL(llog_cleanup);
63
64 int llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
65                int count, struct llog_logid *logid, struct llog_operations *op)
66 {
67         int rc = 0;
68         struct llog_ctxt *ctxt;
69         ENTRY;
70
71         if (index < 0 || index >= LLOG_MAX_CTXTS)
72                 RETURN(-EFAULT);
73
74         if (obd->obd_llog_ctxt[index]) {
75                 /* mds_lov_update_mds might call here multiple times. So if the
76                    llog is already set up then don't to do it again. */
77                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n", 
78                        obd->obd_name, index);
79                 ctxt = obd->obd_llog_ctxt[index];
80                 LASSERT(ctxt->loc_obd == obd);
81                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
82                 LASSERT(ctxt->loc_logops == op);
83                 GOTO(out, rc = 0);
84         }
85         
86         OBD_ALLOC(ctxt, sizeof(*ctxt));
87         if (!ctxt)
88                 GOTO(out, rc = -ENOMEM);
89
90         obd->obd_llog_ctxt[index] = ctxt;
91         ctxt->loc_obd = obd;
92         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
93         ctxt->loc_idx = index;
94         ctxt->loc_logops = op;
95         sema_init(&ctxt->loc_sem, 1);
96
97         if (op->lop_setup)
98                 rc = op->lop_setup(obd, index, disk_obd, count, logid);
99         
100         if (rc) {
101                 obd->obd_llog_ctxt[index] = NULL;
102                 class_export_put(ctxt->loc_exp);
103                 OBD_FREE(ctxt, sizeof(*ctxt));
104         }
105         
106 out:
107         RETURN(rc);
108 }
109 EXPORT_SYMBOL(llog_setup);
110
111 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
112 {
113         int rc = 0;
114         ENTRY;
115
116         if (!ctxt)
117                 RETURN(0);
118
119         if (CTXTP(ctxt, sync))
120                 rc = CTXTP(ctxt, sync)(ctxt, exp);
121
122         RETURN(rc);
123 }
124 EXPORT_SYMBOL(llog_sync);
125
126 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
127                 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
128                 int numcookies)
129 {
130         int rc;
131         ENTRY;
132
133         if (!ctxt) {
134                 CERROR("No ctxt\n");
135                 RETURN(-ENODEV);
136         }
137         
138         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
139         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
140         RETURN(rc);
141 }
142 EXPORT_SYMBOL(llog_add);
143
144 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
145                 int count, struct llog_cookie *cookies, int flags)
146 {
147         int rc;
148         ENTRY;
149
150         if (!ctxt) {
151                 CERROR("No ctxt\n");
152                 RETURN(-ENODEV);
153         }
154         
155         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
156         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
157         RETURN(rc);
158 }
159 EXPORT_SYMBOL(llog_cancel);
160
161 /* callback func for llog_process in llog_obd_origin_setup */
162 static int cat_cancel_cb(struct llog_handle *cathandle,
163                           struct llog_rec_hdr *rec, void *data)
164 {
165         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
166         struct llog_handle *loghandle;
167         struct llog_log_hdr *llh;
168         int rc, index;
169         ENTRY;
170
171         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
172                 CERROR("invalid record in catalog\n");
173                 RETURN(-EINVAL);
174         }
175         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
176                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
177                rec->lrh_index, cathandle->lgh_id.lgl_oid);
178
179         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
180         if (rc) {
181                 CERROR("Cannot find handle for log "LPX64"\n",
182                        lir->lid_id.lgl_oid);
183                 RETURN(rc);
184         }
185
186         llh = loghandle->lgh_hdr;
187         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
188             (llh->llh_count == 1)) {
189                 rc = llog_destroy(loghandle);
190                 if (rc)
191                         CERROR("failure destroying log in postsetup: %d\n", rc);
192
193                 index = loghandle->u.phd.phd_cookie.lgc_index;
194                 llog_free_handle(loghandle);
195
196                 LASSERT(index);
197                 llog_cat_set_first_idx(cathandle, index);
198                 rc = llog_cancel_rec(cathandle, index);
199                 if (rc == 0)
200                         CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of "
201                                "catalog "LPX64"\n", lir->lid_id.lgl_oid,
202                                lir->lid_id.lgl_ogen, rec->lrh_index,
203                                cathandle->lgh_id.lgl_oid);
204         }
205
206         RETURN(rc);
207 }
208
209 /* lop_setup method for filter/osc */
210 // XXX how to set exports
211 int llog_obd_origin_setup(struct obd_device *obd, int index,
212                           struct obd_device *disk_obd, int count,
213                           struct llog_logid *logid)
214 {
215         struct llog_ctxt *ctxt;
216         struct llog_handle *handle;
217         struct lvfs_run_ctxt saved;
218         int rc;
219         ENTRY;
220
221         if (count == 0)
222                 RETURN(0);
223
224         LASSERT(count == 1);
225
226         ctxt = llog_get_context(obd, index);
227         LASSERT(ctxt);
228         llog_gen_init(ctxt);
229
230         if (logid->lgl_oid)
231                 rc = llog_create(ctxt, &handle, logid, NULL);
232         else {
233                 rc = llog_create(ctxt, &handle, NULL, NULL);
234                 if (!rc)
235                         *logid = handle->lgh_id;
236         }
237         if (rc)
238                 GOTO(out, rc);
239
240         ctxt->loc_handle = handle;
241         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
242         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
243         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
244         if (rc)
245                 GOTO(out, rc);
246
247         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
248         if (rc)
249                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
250  out:
251         RETURN(rc);
252 }
253 EXPORT_SYMBOL(llog_obd_origin_setup);
254
255 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
256 {
257         struct llog_handle *cathandle, *n, *loghandle;
258         struct llog_log_hdr *llh;
259         int rc, index;
260         ENTRY;
261
262         if (!ctxt)
263                 RETURN(0);
264
265         cathandle = ctxt->loc_handle;
266         if (cathandle) {
267                 list_for_each_entry_safe(loghandle, n,
268                                          &cathandle->u.chd.chd_head,
269                                          u.phd.phd_entry) {
270                         llh = loghandle->lgh_hdr;
271                         if ((llh->llh_flags &
272                                 LLOG_F_ZAP_WHEN_EMPTY) &&
273                             (llh->llh_count == 1)) {
274                                 rc = llog_destroy(loghandle);
275                                 if (rc)
276                                         CERROR("failure destroying log during "
277                                                "cleanup: %d\n", rc);
278
279                                 index = loghandle->u.phd.phd_cookie.lgc_index;
280                                 llog_free_handle(loghandle);
281
282                                 LASSERT(index);
283                                 llog_cat_set_first_idx(cathandle, index);
284                                 rc = llog_cancel_rec(cathandle, index);
285                                 if (rc == 0)
286                                         CDEBUG(D_HA, "cancel plain log at index"
287                                                " %u of catalog "LPX64"\n",
288                                                index,cathandle->lgh_id.lgl_oid);
289                         }
290                 }
291                 llog_cat_put(ctxt->loc_handle);
292         }
293         RETURN(0);
294 }
295 EXPORT_SYMBOL(llog_obd_origin_cleanup);
296
297 /* add for obdfilter/sz and mds/unlink */
298 int llog_obd_origin_add(struct llog_ctxt *ctxt,
299                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
300                         struct llog_cookie *logcookies, int numcookies)
301 {
302         struct llog_handle *cathandle;
303         int rc;
304         ENTRY;
305
306         cathandle = ctxt->loc_handle;
307         LASSERT(cathandle != NULL);
308         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
309         if (rc != 1)
310                 CERROR("write one catalog record failed: %d\n", rc);
311         RETURN(rc);
312 }
313 EXPORT_SYMBOL(llog_obd_origin_add);
314
315 int llog_cat_initialize(struct obd_device *obd, int count,
316                         struct obd_uuid *uuid)
317 {
318         char name[32] = CATLIST;
319         struct llog_catid *idarray;
320         int size = sizeof(*idarray) * count;
321         int rc;
322         ENTRY;
323
324         OBD_ALLOC(idarray, size);
325         if (!idarray) 
326                 RETURN(-ENOMEM);
327
328         rc = llog_get_cat_list(obd, obd, name, count, idarray);
329         if (rc) {
330                 CERROR("rc: %d\n", rc);
331                 GOTO(out, rc);
332         }
333
334         rc = obd_llog_init(obd, obd, count, idarray, uuid);
335         if (rc) {
336                 CERROR("rc: %d\n", rc);
337                 GOTO(out, rc);
338         }
339
340         rc = llog_put_cat_list(obd, obd, name, count, idarray);
341         if (rc) {
342                 CERROR("rc: %d\n", rc);
343                 GOTO(out, rc);
344         }
345
346  out:
347         OBD_FREE(idarray, size);
348         RETURN(rc);
349 }
350 EXPORT_SYMBOL(llog_cat_initialize);
351
352 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
353                   int count, struct llog_catid *logid, struct obd_uuid *uuid)
354 {
355         int rc;
356         ENTRY;
357         OBD_CHECK_OP(obd, llog_init, 0);
358         OBD_COUNTER_INCREMENT(obd, llog_init);
359
360         rc = OBP(obd, llog_init)(obd, disk_obd, count, logid, uuid);
361         RETURN(rc);
362 }
363 EXPORT_SYMBOL(obd_llog_init);
364
365 int obd_llog_finish(struct obd_device *obd, int count)
366 {
367         int rc;
368         ENTRY;
369         OBD_CHECK_OP(obd, llog_finish, 0);
370         OBD_COUNTER_INCREMENT(obd, llog_finish);
371
372         rc = OBP(obd, llog_finish)(obd, count);
373         RETURN(rc);
374 }
375 EXPORT_SYMBOL(obd_llog_finish);