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