Whamcloud - gitweb
Update b_orphan from HEAD (20040130_1601)
[fs/lustre-release.git] / lustre / obdclass / llog_obd.c
1
2 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
3  * vim:expandtab:shiftwidth=8:tabstop=8:
4  *
5  *   You should have received a copy of the GNU General Public License
6  *   along with Lustre; if not, write to the Free Software
7  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
8  */
9
10 #define DEBUG_SUBSYSTEM S_LOG
11
12 #ifndef EXPORT_SYMTAB
13 #define EXPORT_SYMTAB
14 #endif
15
16 #ifdef __KERNEL__
17 #include <linux/fs.h>
18 #else
19 #include <liblustre.h>
20 #endif
21
22 #include <linux/obd_class.h>
23 #include <linux/lustre_log.h>
24 #include <portals/list.h>
25 #include "llog_internal.h"
26
27 /* helper functions for calling the llog obd methods */
28
29 int llog_setup(struct obd_device *obd, int index, struct obd_device *disk_obd,
30                int count, struct llog_logid *logid, struct llog_operations *op)
31 {
32         int rc = 0;
33         struct llog_ctxt *ctxt;
34         ENTRY;
35
36         if (index < 0 || index >= LLOG_MAX_CTXTS)
37                 RETURN(-EFAULT);
38
39         OBD_ALLOC(ctxt, sizeof(*ctxt));
40         if (!ctxt)
41                 RETURN(-ENOMEM);
42
43         obd->obd_llog_ctxt[index] = ctxt;
44         ctxt->loc_obd = obd;
45         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
46         ctxt->loc_idx = index;
47         ctxt->loc_logops = op;
48         sema_init(&ctxt->loc_sem, 1);
49
50         if (op->lop_setup)
51                 rc = op->lop_setup(obd, index, disk_obd, count, logid);
52         if (ctxt && rc)
53                 OBD_FREE(ctxt, sizeof(*ctxt));
54
55         RETURN(rc);
56 }
57 EXPORT_SYMBOL(llog_setup);
58
59 int llog_cleanup(struct llog_ctxt *ctxt)
60 {
61         int rc = 0;
62         ENTRY;
63
64         LASSERT(ctxt);
65
66         if (CTXTP(ctxt, cleanup))
67                 rc = CTXTP(ctxt, cleanup)(ctxt);
68
69         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
70         class_export_put(ctxt->loc_exp);
71         ctxt->loc_exp = NULL;
72         OBD_FREE(ctxt, sizeof(*ctxt));
73
74         RETURN(rc);
75 }
76 EXPORT_SYMBOL(llog_cleanup);
77
78 int llog_sync(struct llog_ctxt *ctxt, struct obd_export *exp)
79 {
80         int rc = 0;
81         ENTRY;
82
83         if (!ctxt)
84                 RETURN(0);
85
86         if (CTXTP(ctxt, sync))
87                 rc = CTXTP(ctxt, sync)(ctxt, exp);
88
89         RETURN(rc);
90 }
91 EXPORT_SYMBOL(llog_sync);
92
93 int llog_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
94                 struct lov_stripe_md *lsm, struct llog_cookie *logcookies,
95                 int numcookies)
96 {
97         int rc;
98         ENTRY;
99
100         LASSERT(ctxt);
101         CTXT_CHECK_OP(ctxt, add, -EOPNOTSUPP);
102
103         rc = CTXTP(ctxt, add)(ctxt, rec, lsm, logcookies, numcookies);
104         RETURN(rc);
105 }
106 EXPORT_SYMBOL(llog_add);
107
108 int llog_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
109                 int count, struct llog_cookie *cookies, int flags)
110 {
111         int rc;
112         ENTRY;
113
114         LASSERT(ctxt);
115         CTXT_CHECK_OP(ctxt, cancel, -EOPNOTSUPP);
116         rc = CTXTP(ctxt, cancel)(ctxt, lsm, count, cookies, flags);
117         RETURN(rc);
118 }
119 EXPORT_SYMBOL(llog_cancel);
120
121 /* callback func for llog_process in llog_obd_origin_setup */
122 static int cat_cancel_cb(struct llog_handle *cathandle,
123                           struct llog_rec_hdr *rec, void *data)
124 {
125         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
126         struct llog_handle *loghandle;
127         struct llog_log_hdr *llh;
128         int rc, index;
129         ENTRY;
130
131         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
132                 CERROR("invalid record in catalog\n");
133                 RETURN(-EINVAL);
134         }
135         CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
136                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
137                le32_to_cpu(rec->lrh_index), cathandle->lgh_id.lgl_oid);
138
139         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
140         if (rc) {
141                 CERROR("Cannot find handle for log "LPX64"\n",
142                        lir->lid_id.lgl_oid);
143                 RETURN(rc);
144         }
145
146         llh = loghandle->lgh_hdr;
147         if ((le32_to_cpu(llh->llh_flags) & LLOG_F_ZAP_WHEN_EMPTY) &&
148             (le32_to_cpu(llh->llh_count) == 1)) {
149                 rc = llog_destroy(loghandle);
150                 if (rc)
151                         CERROR("failure destroying log in postsetup: %d\n", rc);
152                 LASSERT(rc == 0);
153
154                 index = loghandle->u.phd.phd_cookie.lgc_index;
155                 llog_free_handle(loghandle);
156
157                 LASSERT(index);
158                 llog_cat_set_first_idx(cathandle, index);
159                 rc = llog_cancel_rec(cathandle, index);
160                 if (rc == 0)
161                         CWARN("cancel log "LPX64":%x at index %u of catalog "
162                               LPX64"\n", lir->lid_id.lgl_oid,
163                               lir->lid_id.lgl_ogen, le32_to_cpu(rec->lrh_index),
164                               cathandle->lgh_id.lgl_oid);
165         }
166
167         RETURN(rc);
168 }
169
170 /* lop_setup method for filter/osc */
171 // XXX how to set exports
172 int llog_obd_origin_setup(struct obd_device *obd, int index,
173                           struct obd_device *disk_obd, int count,
174                           struct llog_logid *logid)
175 {
176         struct llog_ctxt *ctxt;
177         struct llog_handle *handle;
178         struct obd_run_ctxt saved;
179         int rc;
180         ENTRY;
181
182         if (count == 0)
183                 RETURN(0);
184
185         LASSERT(count == 1);
186
187         ctxt = llog_get_context(obd, index);
188         LASSERT(ctxt);
189         llog_gen_init(ctxt);
190
191         if (logid->lgl_oid)
192                 rc = llog_create(ctxt, &handle, logid, NULL);
193         else {
194                 rc = llog_create(ctxt, &handle, NULL, NULL);
195                 if (!rc)
196                         *logid = handle->lgh_id;
197         }
198         if (rc)
199                 GOTO(out, rc);
200
201         ctxt->loc_handle = handle;
202         push_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
203         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
204         pop_ctxt(&saved, &disk_obd->obd_ctxt, NULL);
205         if (rc)
206                 GOTO(out, rc);
207
208         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
209         if (rc)
210                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
211  out:
212         if (ctxt && rc) {
213                 obd->obd_llog_ctxt[index] = NULL;
214                 OBD_FREE(ctxt, sizeof(*ctxt));
215         }
216         RETURN(rc);
217 }
218 EXPORT_SYMBOL(llog_obd_origin_setup);
219
220 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
221 {
222         struct llog_handle *cathandle, *n, *loghandle;
223         struct llog_log_hdr *llh;
224         int rc, index;
225         ENTRY;
226
227         if (!ctxt)
228                 return 0;
229
230         cathandle = ctxt->loc_handle;
231         if (cathandle) {
232                 list_for_each_entry_safe(loghandle, n,
233                                          &cathandle->u.chd.chd_head,
234                                          u.phd.phd_entry) {
235                         llh = loghandle->lgh_hdr;
236                         if ((le32_to_cpu(llh->llh_flags) &
237                                 LLOG_F_ZAP_WHEN_EMPTY) &&
238                             (le32_to_cpu(llh->llh_count) == 1)) {
239                                 rc = llog_destroy(loghandle);
240                                 if (rc)
241                                         CERROR("failure destroying log during "
242                                                "cleanup: %d\n", rc);
243                                 LASSERT(rc == 0);
244
245                                 index = loghandle->u.phd.phd_cookie.lgc_index;
246                                 llog_free_handle(loghandle);
247
248                                 LASSERT(index);
249                                 llog_cat_set_first_idx(cathandle, index);
250                                 rc = llog_cancel_rec(cathandle, index);
251                                 if (rc == 0)
252                                         CDEBUG(D_HA, "cancel plain log at index"
253                                                " %u of catalog "LPX64"\n",
254                                                index,cathandle->lgh_id.lgl_oid);
255                         }
256                 }
257                 llog_cat_put(ctxt->loc_handle);
258         }
259         return 0;
260 }
261 EXPORT_SYMBOL(llog_obd_origin_cleanup);
262
263 /* add for obdfilter/sz and mds/unlink */
264 int llog_obd_origin_add(struct llog_ctxt *ctxt,
265                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
266                         struct llog_cookie *logcookies, int numcookies)
267 {
268         struct llog_handle *cathandle;
269         int rc;
270         ENTRY;
271
272         cathandle = ctxt->loc_handle;
273         LASSERT(cathandle != NULL);
274         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
275         if (rc != 1)
276                 CERROR("write one catalog record failed: %d\n", rc);
277         RETURN(rc);
278 }
279 EXPORT_SYMBOL(llog_obd_origin_add);
280
281 int llog_cat_initialize(struct obd_device *obd, int count)
282 {
283         struct llog_logid *idarray;
284         int size = sizeof(*idarray) * count;
285         char name[32] = "CATLIST";
286         int rc;
287         ENTRY;
288
289         OBD_ALLOC(idarray, size);
290         if (!idarray)
291                 RETURN(-ENOMEM);
292
293         memset(idarray, 0, size);
294
295         rc = llog_get_cat_list(obd, obd, name, count, idarray);
296         if (rc) {
297                 CERROR("rc: %d\n", rc);
298                 GOTO(out, rc);
299         }
300
301         rc = obd_llog_init(obd, obd, count, idarray);
302         if (rc) {
303                 CERROR("rc: %d\n", rc);
304                 GOTO(out, rc);
305         }
306
307         rc = llog_put_cat_list(obd, obd, name, count, idarray);
308         if (rc) {
309                 CERROR("rc: %d\n", rc);
310                 GOTO(out, rc);
311         }
312
313  out:
314         OBD_FREE(idarray, size);
315         RETURN(rc);
316 }
317 EXPORT_SYMBOL(llog_cat_initialize);
318
319 int obd_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
320                   int count, struct llog_logid *logid)
321 {
322         int rc;
323         ENTRY;
324         OBD_CHECK_OP(obd, llog_init, 0);
325         OBD_COUNTER_INCREMENT(obd, llog_init);
326
327         rc = OBP(obd, llog_init)(obd, disk_obd, count, logid);
328         RETURN(rc);
329 }
330 EXPORT_SYMBOL(obd_llog_init);
331
332 int obd_llog_finish(struct obd_device *obd, int count)
333 {
334         int rc;
335         ENTRY;
336         OBD_CHECK_OP(obd, llog_finish, 0);
337         OBD_COUNTER_INCREMENT(obd, llog_finish);
338
339         rc = OBP(obd, llog_finish)(obd, count);
340         RETURN(rc);
341 }
342 EXPORT_SYMBOL(obd_llog_finish);