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