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