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