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