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