Whamcloud - gitweb
Introduction of lu_env.
[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         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
55
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,  struct obd_llogs *llogs, int index, 
65                struct obd_device *disk_obd, int count, struct llog_logid *logid,
66                struct llog_operations *op)
67 {
68         int rc = 0;
69         struct llog_ctxt *ctxt;
70         ENTRY;
71
72         if (index < 0 || index >= LLOG_MAX_CTXTS)
73                 RETURN(-EFAULT);
74
75         /* in some recovery cases, obd_llog_ctxt might already be set, 
76          * but llogs might still be zero, for example in obd_filter recovery,
77          * Currenn  */
78         if (obd->obd_llog_ctxt[index] &&
79             (!llogs || (llogs && llogs->llog_ctxt[index]))) {
80                 /* mds_lov_update_mds might call here multiple times. So if the
81                    llog is already set up then don't to do it again. */
82                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n", 
83                        obd->obd_name, index);
84                 ctxt = obd->obd_llog_ctxt[index];
85                 LASSERT(ctxt->loc_obd == obd);
86                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
87                 LASSERT(ctxt->loc_logops == op);
88                 GOTO(out, rc = 0);
89         }
90
91         OBD_ALLOC(ctxt, sizeof(*ctxt));
92         if (!ctxt)
93                 GOTO(out, rc = -ENOMEM);
94
95         if (llogs)
96                 llogs->llog_ctxt[index] = ctxt;
97
98         if (!obd->obd_llog_ctxt[index])
99                 obd->obd_llog_ctxt[index] = ctxt;
100
101         ctxt->loc_obd = obd;
102         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
103         ctxt->loc_idx = index;
104         ctxt->loc_logops = op;
105         sema_init(&ctxt->loc_sem, 1);
106
107         if (op->lop_setup)
108                 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
109
110         if (rc) {
111                 obd->obd_llog_ctxt[index] = NULL;
112                 class_export_put(ctxt->loc_exp);
113                 OBD_FREE(ctxt, sizeof(*ctxt));
114         }
115
116 out:
117         RETURN(rc);
118 }
119 EXPORT_SYMBOL(llog_setup);
120
121 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
122 {
123         int rc = 0;
124         ENTRY;
125
126         if (!ctxt)
127                 RETURN(0);
128
129         if (CTXTP(ctxt, sync))
130                 rc = CTXTP(ctxt, sync)(ctxt, exp);
131
132         RETURN(rc);
133 }
134 EXPORT_SYMBOL(llog_sync);
135
136 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
137                 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
138                 int numcookies)
139 {
140         int rc;
141         ENTRY;
142
143         if (!ctxt) {
144                 CERROR("No ctxt\n");
145                 RETURN(-ENODEV);
146         }
147         
148         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
149
150         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
151         RETURN(rc);
152 }
153 EXPORT_SYMBOL(llog_add);
154
155 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
156                 int count, struct llog_cookie *cookies, int flags)
157 {
158         int rc;
159         ENTRY;
160
161         if (!ctxt) {
162                 CERROR("No ctxt\n");
163                 RETURN(-ENODEV);
164         }
165         
166         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
167         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
168         RETURN(rc);
169 }
170 EXPORT_SYMBOL(llog_cancel);
171
172 /* callback func for llog_process in llog_obd_origin_setup */
173 static int cat_cancel_cb(struct llog_handle *cathandle,
174                           struct llog_rec_hdr *rec, void *data)
175 {
176         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
177         struct llog_handle *loghandle;
178         struct llog_log_hdr *llh;
179         int rc, index;
180         ENTRY;
181
182         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
183                 CERROR("invalid record in catalog\n");
184                 RETURN(-EINVAL);
185         }
186         CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
187                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
188                rec->lrh_index, cathandle->lgh_id.lgl_oid);
189
190         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
191         if (rc) {
192                 CERROR("Cannot find handle for log "LPX64"\n",
193                        lir->lid_id.lgl_oid);
194                 RETURN(rc);
195         }
196
197         llh = loghandle->lgh_hdr;
198         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
199             (llh->llh_count == 1)) {
200                 rc = llog_destroy(loghandle);
201                 if (rc)
202                         CERROR("failure destroying log in postsetup: %d\n", rc);
203
204                 index = loghandle->u.phd.phd_cookie.lgc_index;
205                 llog_free_handle(loghandle);
206
207                 LASSERT(index);
208                 llog_cat_set_first_idx(cathandle, index);
209                 rc = llog_cancel_rec(cathandle, index);
210                 if (rc == 0)
211                         CWARN("cancel log "LPX64":%x at index %u of catalog "
212                               LPX64"\n", lir->lid_id.lgl_oid,
213                               lir->lid_id.lgl_ogen, rec->lrh_index,
214                               cathandle->lgh_id.lgl_oid);
215         }
216
217         RETURN(rc);
218 }
219
220 /* lop_setup method for filter/osc */
221 // XXX how to set exports
222 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
223                           int index, struct obd_device *disk_obd, int count,
224                           struct llog_logid *logid)
225 {
226         struct llog_ctxt *ctxt;
227         struct llog_handle *handle;
228         struct lvfs_run_ctxt saved;
229         int rc;
230         ENTRY;
231
232         if (count == 0)
233                 RETURN(0);
234
235         LASSERT(count == 1);
236
237         if (!llogs)
238                 ctxt = llog_get_context(obd, index);
239         else
240                 ctxt = llog_get_context_from_llogs(llogs, index);
241         
242         LASSERT(ctxt);
243         llog_gen_init(ctxt);
244
245         if (logid->lgl_oid)
246                 rc = llog_create(ctxt, &handle, logid, NULL);
247         else {
248                 rc = llog_create(ctxt, &handle, NULL, NULL);
249                 if (!rc)
250                         *logid = handle->lgh_id;
251         }
252         if (rc)
253                 GOTO(out, rc);
254
255         ctxt->loc_handle = handle;
256         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
257         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
258         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
259         if (rc)
260                 GOTO(out, rc);
261
262         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
263         if (rc)
264                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
265  out:
266         RETURN(rc);
267 }
268 EXPORT_SYMBOL(llog_obd_origin_setup);
269
270 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
271 {
272         struct llog_handle *cathandle, *n, *loghandle;
273         struct llog_log_hdr *llh;
274         int rc, index;
275         ENTRY;
276
277         if (!ctxt)
278                 RETURN(0);
279
280         cathandle = ctxt->loc_handle;
281         if (cathandle) {
282                 list_for_each_entry_safe(loghandle, n,
283                                          &cathandle->u.chd.chd_head,
284                                          u.phd.phd_entry) {
285                         llh = loghandle->lgh_hdr;
286                         if ((llh->llh_flags &
287                                 LLOG_F_ZAP_WHEN_EMPTY) &&
288                             (llh->llh_count == 1)) {
289                                 rc = llog_destroy(loghandle);
290                                 if (rc)
291                                         CERROR("failure destroying log during "
292                                                "cleanup: %d\n", rc);
293
294                                 index = loghandle->u.phd.phd_cookie.lgc_index;
295                                 llog_free_handle(loghandle);
296
297                                 LASSERT(index);
298                                 llog_cat_set_first_idx(cathandle, index);
299                                 rc = llog_cancel_rec(cathandle, index);
300                                 if (rc == 0)
301                                         CDEBUG(D_HA, "cancel plain log at index"
302                                                " %u of catalog "LPX64"\n",
303                                                index,cathandle->lgh_id.lgl_oid);
304                         }
305                 }
306                 llog_cat_put(ctxt->loc_handle);
307         }
308         RETURN(0);
309 }
310 EXPORT_SYMBOL(llog_obd_origin_cleanup);
311
312 /* add for obdfilter/sz and mds/unlink */
313 int llog_obd_origin_add(struct llog_ctxt *ctxt,
314                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
315                         struct llog_cookie *logcookies, int numcookies)
316 {
317         struct llog_handle *cathandle;
318         int rc;
319         ENTRY;
320
321         cathandle = ctxt->loc_handle;
322         LASSERT(cathandle != NULL);
323         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
324         if (rc != 1)
325                 CERROR("write one catalog record failed: %d\n", rc);
326         RETURN(rc);
327 }
328 EXPORT_SYMBOL(llog_obd_origin_add);
329
330 int llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs,
331                         int count, struct obd_uuid *uuid)
332 {
333         char name[32] = CATLIST;
334         struct llog_catid *idarray;
335         int size = sizeof(*idarray) * count;
336         int rc;
337         ENTRY;
338
339         OBD_ALLOC(idarray, size);
340         if (!idarray) 
341                 RETURN(-ENOMEM);
342
343         rc = llog_get_cat_list(obd, obd, name, count, idarray);
344         if (rc) {
345                 CERROR("rc: %d\n", rc);
346                 GOTO(out, rc);
347         }
348
349         rc = obd_llog_init(obd, llogs, obd, count, idarray, uuid);
350         if (rc) {
351                 CERROR("rc: %d\n", rc);
352                 GOTO(out, rc);
353         }
354
355         rc = llog_put_cat_list(obd, obd, name, count, idarray);
356         if (rc) {
357                 CERROR("rc: %d\n", rc);
358                 GOTO(out, rc);
359         }
360
361  out:
362         OBD_FREE(idarray, size);
363         RETURN(rc);
364 }
365 EXPORT_SYMBOL(llog_cat_initialize);
366
367 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs, 
368                   struct obd_device *disk_obd, int count, 
369                   struct llog_catid *logid, struct obd_uuid *uuid)
370 {
371         int rc;
372         ENTRY;
373         OBD_CHECK_DT_OP(obd, llog_init, 0);
374         OBD_COUNTER_INCREMENT(obd, llog_init);
375
376         rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid, 
377                                  uuid);
378         RETURN(rc);
379 }
380 EXPORT_SYMBOL(obd_llog_init);
381
382 int obd_llog_finish(struct obd_device *obd, int count)
383 {
384         int rc;
385         ENTRY;
386         OBD_CHECK_DT_OP(obd, llog_finish, 0);
387         OBD_COUNTER_INCREMENT(obd, llog_finish);
388
389         rc = OBP(obd, llog_finish)(obd, count);
390         RETURN(rc);
391 }
392 EXPORT_SYMBOL(obd_llog_finish);
393