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