Whamcloud - gitweb
b=24037 Changes of 2.6.32 kernel.
[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 (c) 2003, 2010, Oracle and/or its affiliates. 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_PTR(ctxt);
58         if (!ctxt)
59                 return NULL;
60
61         ctxt->loc_obd = obd;
62         cfs_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                 ctxt->loc_exp = NULL;
72         }
73         if (ctxt->loc_imp) {
74                 class_import_put(ctxt->loc_imp);
75                 ctxt->loc_imp = NULL;
76         }
77         LASSERT(ctxt->loc_llcd == NULL);
78         OBD_FREE_PTR(ctxt);
79 }
80
81 int __llog_ctxt_put(struct llog_ctxt *ctxt)
82 {
83         struct obd_llog_group *olg = ctxt->loc_olg;
84         struct obd_device *obd;
85         int rc = 0;
86
87         cfs_spin_lock(&olg->olg_lock);
88         if (!cfs_atomic_dec_and_test(&ctxt->loc_refcount)) {
89                 cfs_spin_unlock(&olg->olg_lock);
90                 return rc;
91         }
92         olg->olg_ctxts[ctxt->loc_idx] = NULL;
93         cfs_spin_unlock(&olg->olg_lock);
94
95         if (ctxt->loc_lcm)
96                 lcm_put(ctxt->loc_lcm);
97
98         obd = ctxt->loc_obd;
99         cfs_spin_lock(&obd->obd_dev_lock);
100         /* sync with llog ctxt user thread */
101         cfs_spin_unlock(&obd->obd_dev_lock);
102
103         /* obd->obd_starting is needed for the case of cleanup
104          * in error case while obd is starting up. */
105         LASSERTF(obd->obd_starting == 1 ||
106                  obd->obd_stopping == 1 || obd->obd_set_up == 0,
107                  "wrong obd state: %d/%d/%d\n", !!obd->obd_starting,
108                  !!obd->obd_stopping, !!obd->obd_set_up);
109
110         /* cleanup the llog ctxt here */
111         if (CTXTP(ctxt, cleanup))
112                 rc = CTXTP(ctxt, cleanup)(ctxt);
113
114         llog_ctxt_destroy(ctxt);
115         cfs_waitq_signal(&olg->olg_waitq);
116         return rc;
117 }
118 EXPORT_SYMBOL(__llog_ctxt_put);
119
120 int llog_cleanup(struct llog_ctxt *ctxt)
121 {
122         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
123         struct obd_llog_group *olg;
124         int rc, idx;
125         ENTRY;
126
127         LASSERT(ctxt != NULL);
128         LASSERT(ctxt != LP_POISON);
129
130         olg = ctxt->loc_olg;
131         LASSERT(olg != NULL);
132         LASSERT(olg != LP_POISON);
133
134         idx = ctxt->loc_idx;
135
136         /* 
137          * Banlance the ctxt get when calling llog_cleanup()
138          */
139         LASSERT(cfs_atomic_read(&ctxt->loc_refcount) < 0x5a5a5a);
140         LASSERT(cfs_atomic_read(&ctxt->loc_refcount) > 1);
141         llog_ctxt_put(ctxt);
142
143         /* 
144          * Try to free the ctxt. 
145          */
146         rc = __llog_ctxt_put(ctxt);
147         if (rc)
148                 CERROR("Error %d while cleaning up ctxt %p\n",
149                        rc, ctxt);
150
151         l_wait_event(olg->olg_waitq,
152                      llog_group_ctxt_null(olg, idx), &lwi);
153
154         RETURN(rc);
155 }
156 EXPORT_SYMBOL(llog_cleanup);
157
158 int llog_setup_named(struct obd_device *obd,  struct obd_llog_group *olg,
159                      int index, struct obd_device *disk_obd, int count,
160                      struct llog_logid *logid, const char *logname,
161                      struct llog_operations *op)
162 {
163         struct llog_ctxt *ctxt;
164         int rc = 0;
165         ENTRY;
166
167         if (index < 0 || index >= LLOG_MAX_CTXTS)
168                 RETURN(-EINVAL);
169
170         LASSERT(olg != NULL);
171
172         ctxt = llog_new_ctxt(obd);
173         if (!ctxt)
174                 RETURN(-ENOMEM);
175
176         ctxt->loc_obd = obd;
177         ctxt->loc_olg = olg;
178         ctxt->loc_idx = index;
179         ctxt->loc_logops = op;
180         cfs_sema_init(&ctxt->loc_sem, 1);
181         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
182         ctxt->loc_flags = LLOG_CTXT_FLAG_UNINITIALIZED;
183
184         rc = llog_group_set_ctxt(olg, ctxt, index);
185         if (rc) {
186                 llog_ctxt_destroy(ctxt);
187                 if (rc == -EEXIST) {
188                         ctxt = llog_group_get_ctxt(olg, index);
189                         if (ctxt) {
190                                 /*
191                                  * mds_lov_update_desc() might call here multiple
192                                  * times. So if the llog is already set up then
193                                  * don't to do it again. 
194                                  */
195                                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
196                                        obd->obd_name, index);
197                                 LASSERT(ctxt->loc_olg == olg);
198                                 LASSERT(ctxt->loc_obd == obd);
199                                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
200                                 LASSERT(ctxt->loc_logops == op);
201                                 llog_ctxt_put(ctxt);
202                         }
203                         rc = 0;
204                 }
205                 RETURN(rc);
206         }
207
208         if (OBD_FAIL_CHECK(OBD_FAIL_OBD_LLOG_SETUP)) {
209                 rc = -ENOTSUPP;
210         } else {
211                 if (op->lop_setup)
212                         rc = op->lop_setup(obd, olg, index, disk_obd, count,
213                                            logid, logname);
214         }
215
216         if (rc) {
217                 CERROR("obd %s ctxt %d lop_setup=%p failed %d\n",
218                        obd->obd_name, index, op->lop_setup, rc);
219                 llog_ctxt_put(ctxt);
220         } else {
221                 CDEBUG(D_CONFIG, "obd %s ctxt %d is initialized\n",
222                        obd->obd_name, index);
223                 ctxt->loc_flags &= ~LLOG_CTXT_FLAG_UNINITIALIZED;
224         }
225
226         RETURN(rc);
227 }
228 EXPORT_SYMBOL(llog_setup_named);
229
230 int llog_setup(struct obd_device *obd,  struct obd_llog_group *olg,
231                int index, struct obd_device *disk_obd, int count,
232                struct llog_logid *logid, struct llog_operations *op)
233 {
234         return llog_setup_named(obd,olg,index,disk_obd,count,logid,NULL,op);
235 }
236 EXPORT_SYMBOL(llog_setup);
237
238 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
239 {
240         int rc = 0;
241         ENTRY;
242
243         if (!ctxt)
244                 RETURN(0);
245
246         if (CTXTP(ctxt, sync))
247                 rc = CTXTP(ctxt, sync)(ctxt, exp);
248
249         RETURN(rc);
250 }
251 EXPORT_SYMBOL(llog_sync);
252
253 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
254              struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
255              int numcookies)
256 {
257         int raised, rc;
258         ENTRY;
259
260         if (!ctxt) {
261                 CERROR("No ctxt\n");
262                 RETURN(-ENODEV);
263         }
264
265         if (ctxt->loc_flags & LLOG_CTXT_FLAG_UNINITIALIZED)
266                 RETURN(-ENXIO);
267
268
269         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
270         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
271         if (!raised)
272                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
273         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
274         if (!raised)
275                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
276         RETURN(rc);
277 }
278 EXPORT_SYMBOL(llog_add);
279
280 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
281                 int count, struct llog_cookie *cookies, int flags)
282 {
283         int rc;
284         ENTRY;
285
286         if (!ctxt) {
287                 CERROR("No ctxt\n");
288                 RETURN(-ENODEV);
289         }
290
291         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
292         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
293         RETURN(rc);
294 }
295 EXPORT_SYMBOL(llog_cancel);
296
297 /* callback func for llog_process in llog_obd_origin_setup */
298 static int cat_cancel_cb(struct llog_handle *cathandle,
299                           struct llog_rec_hdr *rec, void *data)
300 {
301         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
302         struct llog_handle *loghandle;
303         struct llog_log_hdr *llh;
304         int rc, index;
305         ENTRY;
306
307         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
308                 CERROR("invalid record in catalog\n");
309                 RETURN(-EINVAL);
310         }
311         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
312                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
313                rec->lrh_index, cathandle->lgh_id.lgl_oid);
314
315         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
316         if (rc) {
317                 CERROR("Cannot find handle for log "LPX64"\n",
318                        lir->lid_id.lgl_oid);
319                 if (rc == -ENOENT) {
320                         index = rec->lrh_index;
321                         goto cat_cleanup;
322                 }
323                 RETURN(rc);
324         }
325
326         llh = loghandle->lgh_hdr;
327         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
328             (llh->llh_count == 1)) {
329                 rc = llog_destroy(loghandle);
330                 if (rc)
331                         CERROR("failure destroying log in postsetup: %d\n", rc);
332
333                 index = loghandle->u.phd.phd_cookie.lgc_index;
334                 llog_free_handle(loghandle);
335
336 cat_cleanup:
337                 LASSERT(index);
338                 llog_cat_set_first_idx(cathandle, index);
339                 rc = llog_cancel_rec(cathandle, index);
340                 if (rc == 0)
341                         CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
342                               LPX64"\n", lir->lid_id.lgl_oid,
343                               lir->lid_id.lgl_ogen, rec->lrh_index,
344                               cathandle->lgh_id.lgl_oid);
345         }
346
347         RETURN(rc);
348 }
349
350 /* lop_setup method for filter/osc */
351 // XXX how to set exports
352 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llog_group *olg,
353                           int index, struct obd_device *disk_obd, int count,
354                           struct llog_logid *logid, const char *name)
355 {
356         struct llog_ctxt *ctxt;
357         struct llog_handle *handle;
358         struct lvfs_run_ctxt saved;
359         int rc;
360         ENTRY;
361
362         if (count == 0)
363                 RETURN(0);
364
365         LASSERT(count == 1);
366
367         LASSERT(olg != NULL);
368         ctxt = llog_group_get_ctxt(olg, index);
369         if (!ctxt)
370                 RETURN(-ENODEV);
371
372         if (logid && logid->lgl_oid) {
373                 rc = llog_create(ctxt, &handle, logid, NULL);
374         } else {
375                 rc = llog_create(ctxt, &handle, NULL, (char *)name);
376                 if (!rc && logid)
377                         *logid = handle->lgh_id;
378         }
379         if (rc)
380                 GOTO(out, rc);
381
382         ctxt->loc_handle = handle;
383         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
384         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
385         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
386         if (rc)
387                 GOTO(out, rc);
388
389         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
390         if (rc)
391                 CERROR("llog_process() with cat_cancel_cb failed: %d\n", rc);
392         GOTO(out, rc);
393 out:
394         llog_ctxt_put(ctxt);
395         return rc;
396 }
397 EXPORT_SYMBOL(llog_obd_origin_setup);
398
399 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
400 {
401         struct llog_handle *cathandle, *n, *loghandle;
402         struct llog_log_hdr *llh;
403         int rc, index;
404         ENTRY;
405
406         if (!ctxt)
407                 RETURN(0);
408
409         cathandle = ctxt->loc_handle;
410         if (cathandle) {
411                 cfs_list_for_each_entry_safe(loghandle, n,
412                                              &cathandle->u.chd.chd_head,
413                                              u.phd.phd_entry) {
414                         llh = loghandle->lgh_hdr;
415                         if ((llh->llh_flags &
416                                 LLOG_F_ZAP_WHEN_EMPTY) &&
417                             (llh->llh_count == 1)) {
418                                 rc = llog_destroy(loghandle);
419                                 if (rc)
420                                         CERROR("failure destroying log during "
421                                                "cleanup: %d\n", rc);
422
423                                 index = loghandle->u.phd.phd_cookie.lgc_index;
424                                 llog_free_handle(loghandle);
425
426                                 LASSERT(index);
427                                 llog_cat_set_first_idx(cathandle, index);
428                                 rc = llog_cancel_rec(cathandle, index);
429                                 if (rc == 0)
430                                         CDEBUG(D_RPCTRACE, "cancel plain log at"
431                                                "index %u of catalog "LPX64"\n",
432                                                index,cathandle->lgh_id.lgl_oid);
433                         }
434                 }
435                 llog_cat_put(ctxt->loc_handle);
436         }
437         RETURN(0);
438 }
439 EXPORT_SYMBOL(llog_obd_origin_cleanup);
440
441 /* add for obdfilter/sz and mds/unlink */
442 int llog_obd_origin_add(struct llog_ctxt *ctxt,
443                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
444                         struct llog_cookie *logcookies, int numcookies)
445 {
446         struct llog_handle *cathandle;
447         int rc;
448         ENTRY;
449
450         cathandle = ctxt->loc_handle;
451         LASSERT(cathandle != NULL);
452         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
453         if (rc != 0 && rc != 1)
454                 CERROR("write one catalog record failed: %d\n", rc);
455         RETURN(rc);
456 }
457 EXPORT_SYMBOL(llog_obd_origin_add);
458
459 int obd_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
460                   struct obd_device *disk_obd, int *index)
461 {
462         int rc;
463         ENTRY;
464         OBD_CHECK_DT_OP(obd, llog_init, 0);
465         OBD_COUNTER_INCREMENT(obd, llog_init);
466
467         rc = OBP(obd, llog_init)(obd, olg, disk_obd, index);
468         RETURN(rc);
469 }
470 EXPORT_SYMBOL(obd_llog_init);
471
472 int obd_llog_finish(struct obd_device *obd, int count)
473 {
474         int rc;
475         ENTRY;
476         OBD_CHECK_DT_OP(obd, llog_finish, 0);
477         OBD_COUNTER_INCREMENT(obd, llog_finish);
478
479         rc = OBP(obd, llog_finish)(obd, count);
480         RETURN(rc);
481 }
482 EXPORT_SYMBOL(obd_llog_finish);