Whamcloud - gitweb
b=22658 More tolerant llog processing
[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  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LOG
38
39 #ifndef EXPORT_SYMTAB
40 #define EXPORT_SYMTAB
41 #endif
42
43 #ifndef __KERNEL__
44 #include <liblustre.h>
45 #endif
46
47 #include <obd_class.h>
48 #include <lustre_log.h>
49 #include <libcfs/list.h>
50 #include "llog_internal.h"
51
52 /* helper functions for calling the llog obd methods */
53 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
54 {
55         struct llog_ctxt *ctxt;
56
57         OBD_ALLOC(ctxt, sizeof(*ctxt));
58         if (!ctxt)
59                 return NULL;
60
61         ctxt->loc_obd = obd;
62         atomic_set(&ctxt->loc_refcount, 1);
63
64         return ctxt;
65 }
66
67 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
68 {
69         if (ctxt->loc_exp)
70                 class_export_put(ctxt->loc_exp);
71         if (ctxt->loc_imp) {
72                 class_import_put(ctxt->loc_imp);
73                 ctxt->loc_imp = NULL;
74         }
75         LASSERT(ctxt->loc_llcd == NULL);
76         OBD_FREE(ctxt, sizeof(*ctxt));
77         return;
78 }
79
80 int __llog_ctxt_put(struct llog_ctxt *ctxt)
81 {
82         struct obd_device *obd;
83         int rc = 0;
84
85         obd = ctxt->loc_obd;
86         spin_lock(&obd->obd_dev_lock);
87         if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
88                 spin_unlock(&obd->obd_dev_lock);
89                 return rc;
90         }
91         obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
92         spin_unlock(&obd->obd_dev_lock);
93
94         LASSERTF(obd->obd_starting == 1 || 
95                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
96                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting, 
97                  !!obd->obd_stopping, !!obd->obd_set_up);
98
99         /* cleanup the llog ctxt here */
100         if (CTXTP(ctxt, cleanup))
101                 rc = CTXTP(ctxt, cleanup)(ctxt);
102  
103         llog_ctxt_destroy(ctxt);
104         wake_up(&obd->obd_llog_waitq);
105         return rc;
106 }
107 EXPORT_SYMBOL(__llog_ctxt_put);
108  
109 int llog_cleanup(struct llog_ctxt *ctxt)
110 {
111         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
112         struct obd_device *obd;
113         int rc, idx;
114         ENTRY;
115
116         if (!ctxt) {
117                 CERROR("No ctxt\n");
118                 RETURN(-ENODEV);
119         }
120         obd = ctxt->loc_obd;
121
122         /*banlance the ctxt get when calling llog_cleanup */
123         llog_ctxt_put(ctxt);
124
125         /* sync with other llog ctxt user thread */
126         spin_lock(&obd->obd_dev_lock);
127
128         /* obd->obd_starting is needed for the case of cleanup
129          * in error case while obd is starting up. */
130         LASSERTF(obd->obd_starting == 1 || 
131                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
132                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting, 
133                  !!obd->obd_stopping, !!obd->obd_set_up);
134
135         spin_unlock(&obd->obd_dev_lock);
136
137         idx = ctxt->loc_idx;
138         /*try to free the ctxt */
139         rc = __llog_ctxt_put(ctxt);
140         if (rc)
141                 CERROR("Error %d while cleaning up ctxt %p\n", 
142                        rc, ctxt);
143
144         l_wait_event(obd->obd_llog_waitq, llog_ctxt_null(obd, idx), &lwi);
145
146         RETURN(rc);
147 }
148 EXPORT_SYMBOL(llog_cleanup);
149
150 int llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
151                int count, struct llog_logid *logid, struct llog_operations *op)
152 {
153         int rc = 0;
154         struct llog_ctxt *ctxt, *old_ctxt;
155         ENTRY;
156
157         if (index < 0 || index >= LLOG_MAX_CTXTS)
158                 RETURN(-EFAULT);
159
160         ctxt = llog_new_ctxt(obd);
161         if (!ctxt)
162                 GOTO(out, rc = -ENOMEM);
163
164         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
165         ctxt->loc_idx = index;
166         ctxt->loc_logops = op;
167         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
168         sema_init(&ctxt->loc_sem, 1);
169
170         /* sync with other llog ctxt user thread */
171         spin_lock(&obd->obd_dev_lock);
172         old_ctxt = obd->obd_llog_ctxt[index];
173         if (old_ctxt) {
174                 /* mds_lov_update_mds might call here multiple times. So if the
175                    llog is already set up then don't to do it again. */
176                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
177                        obd->obd_name, index);
178                 LASSERT(old_ctxt->loc_obd == obd);
179                 LASSERT(old_ctxt->loc_exp == disk_obd->obd_self_export);
180                 LASSERT(old_ctxt->loc_logops == op);
181                 spin_unlock(&obd->obd_dev_lock);
182
183                 llog_ctxt_destroy(ctxt);
184                 ctxt = old_ctxt;
185                 GOTO(out, rc = 0);
186         }
187
188         obd->obd_llog_ctxt[index] = ctxt;
189         spin_unlock(&obd->obd_dev_lock);
190
191         if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP)) {
192                 rc = -EOPNOTSUPP;
193         } else {
194                 if (op->lop_setup)
195                         rc = op->lop_setup(obd, index, disk_obd, count, logid);
196         }
197
198         if (rc) {
199                 CERROR("obd %s ctxt %d lop_setup=%p failed %d\n",
200                        obd->obd_name, index, op->lop_setup, rc);
201                 llog_ctxt_put(ctxt);
202         } else {
203                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
204                        obd->obd_name, index);
205                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
206         }
207 out:
208         RETURN(rc);
209 }
210 EXPORT_SYMBOL(llog_setup);
211
212 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
213 {
214         int rc = 0;
215         ENTRY;
216
217         if (!ctxt)
218                 RETURN(0);
219
220         if (CTXTP(ctxt, sync))
221                 rc = CTXTP(ctxt, sync)(ctxt, exp);
222
223         RETURN(rc);
224 }
225 EXPORT_SYMBOL(llog_sync);
226
227 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
228              struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
229              int numcookies)
230 {
231         int raised, rc;
232         ENTRY;
233
234         if (!ctxt) {
235                 CERROR("No ctxt\n");
236                 RETURN(-ENODEV);
237         }
238
239         if (ctxt->loc_flags & LLOG_CTXT_FLAG_UNINITIALIZED)
240                 RETURN(-ENXIO);
241
242         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
243         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
244         if (!raised)
245                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
246         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
247         if (!raised)
248                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
249         RETURN(rc);
250 }
251 EXPORT_SYMBOL(llog_add);
252
253 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
254                 int count, struct llog_cookie *cookies, int flags)
255 {
256         int rc;
257         ENTRY;
258
259         if (!ctxt) {
260                 CERROR("No ctxt\n");
261                 RETURN(-ENODEV);
262         }
263         
264         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
265         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
266         RETURN(rc);
267 }
268 EXPORT_SYMBOL(llog_cancel);
269
270 /* callback func for llog_process in llog_obd_origin_setup */
271 static int cat_cancel_cb(struct llog_handle *cathandle,
272                           struct llog_rec_hdr *rec, void *data)
273 {
274         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
275         struct llog_handle *loghandle;
276         struct llog_log_hdr *llh;
277         int rc, index;
278         ENTRY;
279
280         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
281                 CERROR("invalid record in catalog\n");
282                 RETURN(-EINVAL);
283         }
284         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
285                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
286                rec->lrh_index, cathandle->lgh_id.lgl_oid);
287
288         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
289         if (rc) {
290                 CERROR("Cannot find handle for log "LPX64"\n",
291                        lir->lid_id.lgl_oid);
292                 if (rc == -ENOENT) {
293                         index = rec->lrh_index;
294                         goto cat_cleanup;
295                 }
296                 RETURN(rc);
297         }
298
299         llh = loghandle->lgh_hdr;
300         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
301             (llh->llh_count == 1)) {
302                 rc = llog_destroy(loghandle);
303                 if (rc)
304                         CERROR("failure destroying log in postsetup: %d\n", rc);
305
306                 index = loghandle->u.phd.phd_cookie.lgc_index;
307                 llog_free_handle(loghandle);
308
309 cat_cleanup:
310                 LASSERT(index);
311                 llog_cat_set_first_idx(cathandle, index);
312                 rc = llog_cancel_rec(cathandle, index);
313                 if (rc == 0)
314                         CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of "
315                                "catalog "LPX64"\n", lir->lid_id.lgl_oid,
316                                lir->lid_id.lgl_ogen, rec->lrh_index,
317                                cathandle->lgh_id.lgl_oid);
318         }
319
320         RETURN(rc);
321 }
322
323 /* lop_setup method for filter/osc */
324 // XXX how to set exports
325 int llog_obd_origin_setup(struct obd_device *obd, int index,
326                           struct obd_device *disk_obd, int count,
327                           struct llog_logid *logid)
328 {
329         struct llog_ctxt *ctxt;
330         struct llog_handle *handle;
331         struct lvfs_run_ctxt *saved = NULL;
332         int rc;
333         ENTRY;
334
335         if (count == 0)
336                 RETURN(0);
337
338         OBD_SLAB_ALLOC_PTR(saved, obd_lvfs_ctxt_cache);
339         if (saved == NULL)
340                 RETURN(-ENOMEM);
341
342         LASSERT(count == 1);
343
344         ctxt = llog_get_context(obd, index);
345         LASSERT(ctxt);
346         llog_gen_init(ctxt);
347
348         if (logid->lgl_oid)
349                 rc = llog_create(ctxt, &handle, logid, NULL);
350         else {
351                 rc = llog_create(ctxt, &handle, NULL, NULL);
352                 if (!rc)
353                         *logid = handle->lgh_id;
354         }
355         if (rc)
356                 GOTO(out, rc);
357
358         ctxt->loc_handle = handle;
359         push_ctxt(saved, &disk_obd->obd_lvfs_ctxt, NULL);
360         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
361         pop_ctxt(saved, &disk_obd->obd_lvfs_ctxt, NULL);
362         if (rc)
363                 GOTO(out, rc);
364
365         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
366         if (rc)
367                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
368 out:
369         llog_ctxt_put(ctxt);
370         OBD_SLAB_FREE_PTR(saved, obd_lvfs_ctxt_cache);
371         RETURN(rc);
372 }
373 EXPORT_SYMBOL(llog_obd_origin_setup);
374
375 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
376 {
377         struct llog_handle *cathandle, *n, *loghandle;
378         struct llog_log_hdr *llh;
379         int rc, index;
380         ENTRY;
381
382         if (!ctxt)
383                 RETURN(0);
384
385         cathandle = ctxt->loc_handle;
386         if (cathandle) {
387                 list_for_each_entry_safe(loghandle, n,
388                                          &cathandle->u.chd.chd_head,
389                                          u.phd.phd_entry) {
390                         llh = loghandle->lgh_hdr;
391                         if ((llh->llh_flags &
392                                 LLOG_F_ZAP_WHEN_EMPTY) &&
393                             (llh->llh_count == 1)) {
394                                 rc = llog_destroy(loghandle);
395                                 if (rc)
396                                         CERROR("failure destroying log during "
397                                                "cleanup: %d\n", rc);
398
399                                 index = loghandle->u.phd.phd_cookie.lgc_index;
400                                 llog_free_handle(loghandle);
401
402                                 LASSERT(index);
403                                 llog_cat_set_first_idx(cathandle, index);
404                                 rc = llog_cancel_rec(cathandle, index);
405                                 if (rc == 0)
406                                         CDEBUG(D_RPCTRACE, "cancel plain log at"
407                                                "index %u of catalog "LPX64"\n",
408                                                index,cathandle->lgh_id.lgl_oid);
409                         }
410                 }
411                 llog_cat_put(ctxt->loc_handle);
412         }
413         RETURN(0);
414 }
415 EXPORT_SYMBOL(llog_obd_origin_cleanup);
416
417 /* add for obdfilter/sz and mds/unlink */
418 int llog_obd_origin_add(struct llog_ctxt *ctxt,
419                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
420                         struct llog_cookie *logcookies, int numcookies)
421 {
422         struct llog_handle *cathandle;
423         int rc;
424         ENTRY;
425
426         cathandle = ctxt->loc_handle;
427         LASSERT(cathandle != NULL);
428         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
429         if (rc != 1)
430                 CERROR("write one catalog record failed: %d\n", rc);
431         RETURN(rc);
432 }
433 EXPORT_SYMBOL(llog_obd_origin_add);
434
435 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
436                   int *index)
437 {
438         int rc;
439         ENTRY;
440         OBD_CHECK_OP(obd, llog_init, 0);
441         OBD_COUNTER_INCREMENT(obd, llog_init);
442
443         rc = OBP(obd, llog_init)(obd, disk_obd, index);
444         RETURN(rc);
445 }
446 EXPORT_SYMBOL(obd_llog_init);
447
448 int obd_llog_finish(struct obd_device *obd, int count)
449 {
450         int rc;
451         ENTRY;
452         OBD_CHECK_OP(obd, llog_finish, 0);
453         OBD_COUNTER_INCREMENT(obd, llog_finish);
454
455         rc = OBP(obd, llog_finish)(obd, count);
456         RETURN(rc);
457 }
458 EXPORT_SYMBOL(obd_llog_finish);