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