Whamcloud - gitweb
Branch HEAD
[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  *  Copyright (C) 2005 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #define DEBUG_SUBSYSTEM S_LOG
26
27 #ifndef EXPORT_SYMTAB
28 #define EXPORT_SYMTAB
29 #endif
30
31 #ifndef __KERNEL__
32 #include <liblustre.h>
33 #endif
34
35 #include <obd_class.h>
36 #include <lustre_log.h>
37 #include <libcfs/list.h>
38 #include "llog_internal.h"
39
40 /* helper functions for calling the llog obd methods */
41 static struct llog_ctxt* llog_new_ctxt(struct obd_device *obd)
42 {
43         struct llog_ctxt *ctxt;
44
45         OBD_ALLOC_PTR(ctxt);
46         if (!ctxt)
47                 return NULL;
48
49         ctxt->loc_obd = obd;
50         atomic_set(&ctxt->loc_refcount, 1);
51
52         return ctxt;
53 }
54
55 static void llog_ctxt_destroy(struct llog_ctxt *ctxt)
56 {
57         if (ctxt->loc_exp)
58                 class_export_put(ctxt->loc_exp);
59         OBD_FREE_PTR(ctxt);
60         return;
61 }
62
63 int __llog_ctxt_put(struct llog_ctxt *ctxt)
64 {
65         struct obd_llog_group *olg = ctxt->loc_olg;
66         struct obd_device *obd;
67         int rc = 0;
68
69         spin_lock(&olg->olg_lock);
70         if (!atomic_dec_and_test(&ctxt->loc_refcount)) {
71                 spin_unlock(&olg->olg_lock);
72                 return rc;
73         }
74         olg->olg_ctxts[ctxt->loc_idx] = NULL;
75         spin_unlock(&olg->olg_lock);
76
77         obd = ctxt->loc_obd;
78         spin_lock(&obd->obd_dev_lock);
79         spin_unlock(&obd->obd_dev_lock); /* sync with llog ctxt user thread */
80         LASSERT(obd->obd_stopping == 1 || obd->obd_set_up == 0);
81         /* cleanup the llog ctxt here */
82         if (CTXTP(ctxt, cleanup))
83                 rc = CTXTP(ctxt, cleanup)(ctxt);
84
85         llog_ctxt_destroy(ctxt);
86         wake_up(&olg->olg_waitq);
87         return rc;
88 }
89 EXPORT_SYMBOL(__llog_ctxt_put);
90
91 int llog_cleanup(struct llog_ctxt *ctxt)
92 {
93         struct l_wait_info lwi = LWI_INTR(LWI_ON_SIGNAL_NOOP, NULL);
94         struct obd_llog_group *olg;
95         int rc, idx;
96         ENTRY;
97
98         if (!ctxt) {
99                 CERROR("No ctxt\n");
100                 RETURN(-ENODEV);
101         }
102
103         olg = ctxt->loc_olg;
104         idx = ctxt->loc_idx;
105
106         /* banlance the ctxt get when calling llog_cleanup */
107         LASSERT(atomic_read(&ctxt->loc_refcount) > 1);
108         llog_ctxt_put(ctxt);
109
110         /* try to free the ctxt */
111         rc = __llog_ctxt_put(ctxt);
112
113         l_wait_event(olg->olg_waitq,
114                      llog_group_ctxt_null(olg, idx), &lwi);
115
116         RETURN(rc);
117 }
118 EXPORT_SYMBOL(llog_cleanup);
119
120 int llog_setup(struct obd_device *obd,  struct obd_llog_group *olg, int index,
121                struct obd_device *disk_obd, int count, struct llog_logid *logid,
122                struct llog_operations *op)
123 {
124         int rc = 0;
125         struct llog_ctxt *ctxt;
126         ENTRY;
127
128         if (index < 0 || index >= LLOG_MAX_CTXTS)
129                 RETURN(-EFAULT);
130
131         LASSERT(olg != NULL);
132         ctxt = llog_group_get_ctxt(olg, index);
133
134         /* in some recovery cases, obd_llog_ctxt might already be set,
135          * but llogs might still be zero, for example in obd_filter recovery */
136         if (ctxt) {
137                 /* mds_lov_update_mds might call here multiple times. So if the
138                    llog is already set up then don't to do it again. */
139                 CDEBUG(D_CONFIG, "obd %s ctxt %d already set up\n",
140                        obd->obd_name, index);
141                 LASSERT(ctxt->loc_olg == olg);
142                 LASSERT(ctxt->loc_obd == obd);
143                 LASSERT(ctxt->loc_exp == disk_obd->obd_self_export);
144                 LASSERT(ctxt->loc_logops == op);
145                 llog_ctxt_put(ctxt);
146                 GOTO(out, rc = 0);
147         }
148         ctxt = llog_new_ctxt(obd);
149         if (!ctxt)
150                 GOTO(out, rc = -ENOMEM);
151
152         rc = llog_group_set_ctxt(olg, ctxt, index);
153         if (rc) {
154                 llog_ctxt_destroy(ctxt);
155                 if (rc == -EEXIST)
156                         rc = 0;
157                 GOTO(out, rc);
158         }
159         ctxt->loc_obd = obd;
160         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
161         ctxt->loc_olg = olg;
162         ctxt->loc_idx = index;
163         ctxt->loc_logops = op;
164         sema_init(&ctxt->loc_sem, 1);
165
166         if (op->lop_setup)
167                 rc = op->lop_setup(obd, olg, index, disk_obd, count, logid);
168
169         if (rc) {
170                 llog_ctxt_destroy(ctxt);
171         }
172 out:
173         RETURN(rc);
174 }
175 EXPORT_SYMBOL(llog_setup);
176
177 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
178 {
179         int rc = 0;
180         ENTRY;
181
182         if (!ctxt)
183                 RETURN(0);
184
185         if (CTXTP(ctxt, sync))
186                 rc = CTXTP(ctxt, sync)(ctxt, exp);
187
188         RETURN(rc);
189 }
190 EXPORT_SYMBOL(llog_sync);
191
192 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
193                 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
194                 int numcookies)
195 {
196         __u32 cap;
197         int rc;
198         ENTRY;
199
200         if (!ctxt) {
201                 CERROR("No ctxt\n");
202                 RETURN(-ENODEV);
203         }
204         
205         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
206         cap = current->cap_effective;             
207         cap_raise(current->cap_effective, CAP_SYS_RESOURCE);
208         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
209         current->cap_effective = cap; 
210         RETURN(rc);
211 }
212 EXPORT_SYMBOL(llog_add);
213
214 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
215                 int count, struct llog_cookie *cookies, int flags)
216 {
217         int rc;
218         ENTRY;
219
220         if (!ctxt) {
221                 CERROR("No ctxt\n");
222                 RETURN(-ENODEV);
223         }
224         
225         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
226         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
227         RETURN(rc);
228 }
229 EXPORT_SYMBOL(llog_cancel);
230
231 /* callback func for llog_process in llog_obd_origin_setup */
232 static int cat_cancel_cb(struct llog_handle *cathandle,
233                           struct llog_rec_hdr *rec, void *data)
234 {
235         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
236         struct llog_handle *loghandle;
237         struct llog_log_hdr *llh;
238         int rc, index;
239         ENTRY;
240
241         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
242                 CERROR("invalid record in catalog\n");
243                 RETURN(-EINVAL);
244         }
245         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
246                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
247                rec->lrh_index, cathandle->lgh_id.lgl_oid);
248
249         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
250         if (rc) {
251                 CERROR("Cannot find handle for log "LPX64"\n",
252                        lir->lid_id.lgl_oid);
253                 RETURN(rc);
254         }
255
256         llh = loghandle->lgh_hdr;
257         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
258             (llh->llh_count == 1)) {
259                 rc = llog_destroy(loghandle);
260                 if (rc)
261                         CERROR("failure destroying log in postsetup: %d\n", rc);
262
263                 index = loghandle->u.phd.phd_cookie.lgc_index;
264                 llog_free_handle(loghandle);
265
266                 LASSERT(index);
267                 llog_cat_set_first_idx(cathandle, index);
268                 rc = llog_cancel_rec(cathandle, index);
269                 if (rc == 0)
270                         CDEBUG(D_HA, "cancel log "LPX64":%x at index %u of catalog "
271                               LPX64"\n", lir->lid_id.lgl_oid,
272                               lir->lid_id.lgl_ogen, rec->lrh_index,
273                               cathandle->lgh_id.lgl_oid);
274         }
275
276         RETURN(rc);
277 }
278
279 /* lop_setup method for filter/osc */
280 // XXX how to set exports
281 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llog_group *olg,
282                           int index, struct obd_device *disk_obd, int count,
283                           struct llog_logid *logid)
284 {
285         struct llog_ctxt *ctxt;
286         struct llog_handle *handle;
287         struct lvfs_run_ctxt saved;
288         int rc;
289         ENTRY;
290
291         if (count == 0)
292                 RETURN(0);
293
294         LASSERT(count == 1);
295
296         LASSERT(olg != NULL);
297         ctxt = llog_group_get_ctxt(olg, index);
298         
299         LASSERT(ctxt);
300         llog_gen_init(ctxt);
301
302         if (logid->lgl_oid)
303                 rc = llog_create(ctxt, &handle, logid, NULL);
304         else {
305                 rc = llog_create(ctxt, &handle, NULL, NULL);
306                 if (!rc)
307                         *logid = handle->lgh_id;
308         }
309         if (rc)
310                 GOTO(out, rc);
311
312         ctxt->loc_handle = handle;
313         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
314         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
315         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
316         if (rc)
317                 GOTO(out, rc);
318
319         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
320         if (rc)
321                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
322 out:
323         llog_ctxt_put(ctxt);
324         RETURN(rc);
325 }
326 EXPORT_SYMBOL(llog_obd_origin_setup);
327
328 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
329 {
330         struct llog_handle *cathandle, *n, *loghandle;
331         struct llog_log_hdr *llh;
332         int rc, index;
333         ENTRY;
334
335         if (!ctxt)
336                 RETURN(0);
337
338         cathandle = ctxt->loc_handle;
339         if (cathandle) {
340                 list_for_each_entry_safe(loghandle, n,
341                                          &cathandle->u.chd.chd_head,
342                                          u.phd.phd_entry) {
343                         llh = loghandle->lgh_hdr;
344                         if ((llh->llh_flags &
345                                 LLOG_F_ZAP_WHEN_EMPTY) &&
346                             (llh->llh_count == 1)) {
347                                 rc = llog_destroy(loghandle);
348                                 if (rc)
349                                         CERROR("failure destroying log during "
350                                                "cleanup: %d\n", rc);
351
352                                 index = loghandle->u.phd.phd_cookie.lgc_index;
353                                 llog_free_handle(loghandle);
354
355                                 LASSERT(index);
356                                 llog_cat_set_first_idx(cathandle, index);
357                                 rc = llog_cancel_rec(cathandle, index);
358                                 if (rc == 0)
359                                         CDEBUG(D_RPCTRACE, "cancel plain log at"
360                                                "index %u of catalog "LPX64"\n",
361                                                index,cathandle->lgh_id.lgl_oid);
362                         }
363                 }
364                 llog_cat_put(ctxt->loc_handle);
365         }
366         RETURN(0);
367 }
368 EXPORT_SYMBOL(llog_obd_origin_cleanup);
369
370 /* add for obdfilter/sz and mds/unlink */
371 int llog_obd_origin_add(struct llog_ctxt *ctxt,
372                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
373                         struct llog_cookie *logcookies, int numcookies)
374 {
375         struct llog_handle *cathandle;
376         int rc;
377         ENTRY;
378
379         cathandle = ctxt->loc_handle;
380         LASSERT(cathandle != NULL);
381         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
382         if (rc != 1)
383                 CERROR("write one catalog record failed: %d\n", rc);
384         RETURN(rc);
385 }
386 EXPORT_SYMBOL(llog_obd_origin_add);
387
388 int llog_cat_initialize(struct obd_device *obd, struct obd_llog_group *olg,
389                         int count, struct obd_uuid *uuid)
390 {
391         char name[32] = CATLIST;
392         struct llog_catid *idarray = NULL;
393         int size = sizeof(*idarray) * count;
394         int rc;
395         ENTRY;
396
397         if (count) {
398                 OBD_VMALLOC(idarray, size);
399                 if (!idarray)
400                         RETURN(-ENOMEM);
401         }
402
403         rc = llog_get_cat_list(obd, obd, name, count, idarray);
404         if (rc) {
405                 CERROR("rc: %d\n", rc);
406                 GOTO(out, rc);
407         }
408
409         rc = obd_llog_init(obd, olg->olg_group, obd, count, idarray, uuid);
410         if (rc) {
411                 CERROR("rc: %d\n", rc);
412                 GOTO(out, rc);
413         }
414
415         rc = llog_put_cat_list(obd, obd, name, count, idarray);
416         if (rc) {
417                 CERROR("rc: %d\n", rc);
418                 GOTO(out, rc);
419         }
420
421  out:
422         if (idarray)
423                 OBD_VFREE(idarray, size);
424         RETURN(rc);
425 }
426 EXPORT_SYMBOL(llog_cat_initialize);
427
428 int obd_llog_init(struct obd_device *obd, int group, 
429                   struct obd_device *disk_obd, int count, 
430                   struct llog_catid *logid, struct obd_uuid *uuid)
431 {
432         int rc;
433         ENTRY;
434         OBD_CHECK_DT_OP(obd, llog_init, 0);
435         OBD_COUNTER_INCREMENT(obd, llog_init);
436
437         rc = OBP(obd, llog_init)(obd, group, disk_obd, count, logid, uuid);
438         RETURN(rc);
439 }
440 EXPORT_SYMBOL(obd_llog_init);
441
442 int obd_llog_finish(struct obd_device *obd, int count)
443 {
444         int rc;
445         ENTRY;
446         OBD_CHECK_DT_OP(obd, llog_finish, 0);
447         OBD_COUNTER_INCREMENT(obd, llog_finish);
448
449         rc = OBP(obd, llog_finish)(obd, count);
450         RETURN(rc);
451 }
452 EXPORT_SYMBOL(obd_llog_finish);
453