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