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