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