Whamcloud - gitweb
add9ae486e5e56094f801919812cc89552fb21a1
[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
26 /* helper functions for calling the llog obd methods */
27
28 int obd_llog_setup(struct obd_device *obd, struct obd_llogs *llogs, 
29                    int index, struct obd_device *disk_obd, int count, 
30                    struct llog_logid *logid, struct llog_operations *op)
31 {
32         int rc = 0;
33         struct llog_ctxt *ctxt;
34         ENTRY;
35
36         LASSERT(llogs);
37
38         if (index < 0 || index >= LLOG_MAX_CTXTS)
39                 RETURN(-EFAULT);
40
41         OBD_ALLOC(ctxt, sizeof(*ctxt));
42         if (!ctxt)
43                 RETURN(-ENOMEM);
44
45         llogs->llog_ctxt[index] = ctxt;
46         obd->obd_llog_ctxt[index] = ctxt;
47         ctxt->loc_logops = op;
48         ctxt->loc_obd = obd;
49         ctxt->loc_llogs = llogs;
50         ctxt->loc_idx = index;
51         ctxt->loc_alone = 0;
52         ctxt->loc_exp = class_export_get(disk_obd->obd_self_export);
53         sema_init(&ctxt->loc_sem, 1);
54
55         if (op->lop_close == llog_lvfs_ops.lop_close) {
56                 ctxt->loc_fsops = disk_obd->obd_fsops;
57                 ctxt->loc_lvfs_ctxt = &disk_obd->obd_lvfs_ctxt;
58                 if (!strcmp(disk_obd->obd_type->typ_name, "mds")) {
59                         struct mds_obd *mds = &disk_obd->u.mds;
60                         ctxt->loc_objects_dir = mds->mds_objects_dir;
61                         ctxt->loc_logs_dir = mds->mds_logs_dir;
62                 }
63         }
64
65         if (op->lop_setup)
66                 rc = op->lop_setup(obd, llogs, index, disk_obd, count, logid);
67         if (ctxt && rc) {
68                 obd->obd_llog_ctxt[index] = NULL;
69                 OBD_FREE(ctxt, sizeof(*ctxt));
70         }
71
72         RETURN(rc);
73 }
74 EXPORT_SYMBOL(obd_llog_setup);
75
76 int obd_llog_cleanup(struct llog_ctxt *ctxt)
77 {
78         int rc = 0;
79         ENTRY;
80
81         LASSERT(ctxt);
82
83         if (CTXTP(ctxt, cleanup))  
84                 rc = CTXTP(ctxt, cleanup)(ctxt);
85                 
86         ctxt->loc_obd->obd_llog_ctxt[ctxt->loc_idx] = NULL;
87         ctxt->loc_llogs->llog_ctxt[ctxt->loc_idx] = NULL;
88         class_export_put(ctxt->loc_exp);
89         ctxt->loc_exp = NULL;
90         OBD_FREE(ctxt, sizeof(*ctxt));
91         RETURN(rc);
92 }
93 EXPORT_SYMBOL(obd_llog_cleanup);
94
95 /* callback func for llog_process in llog_obd_origin_setup */
96 static int cat_cancel_cb(struct llog_handle *cathandle,
97                           struct llog_rec_hdr *rec, void *data)
98 {
99         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
100         struct llog_handle *loghandle;
101         struct llog_log_hdr *llh;
102         int rc, index;
103         ENTRY;
104
105         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
106                 CERROR("invalid record in catalog\n");
107                 RETURN(-EINVAL);
108         }
109         CWARN("processing log "LPX64":%x at index %u of catalog "LPX64"\n",
110                lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
111                rec->lrh_index, cathandle->lgh_id.lgl_oid);
112
113         rc = llog_cat_id2handle(cathandle, &loghandle, &lir->lid_id);
114         if (rc) {
115                 CERROR("Cannot find handle for log "LPX64"\n",
116                        lir->lid_id.lgl_oid);
117                 RETURN(rc);
118         }
119
120         llh = loghandle->lgh_hdr;
121         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
122             (llh->llh_count == 1)) {
123                 rc = llog_destroy(loghandle);
124                 if (rc)
125                         CERROR("failure destroying log in postsetup: %d\n", rc);
126                 LASSERT(rc == 0);
127
128                 index = loghandle->u.phd.phd_cookie.lgc_index;
129                 llog_free_handle(loghandle);
130
131                 LASSERT(index);
132                 llog_cat_set_first_idx(cathandle, index);
133                 rc = llog_cancel_rec(cathandle, index);
134                 if (rc == 0)
135                         CWARN("cancel log "LPX64":%x at index %u of catalog "
136                               LPX64"\n", lir->lid_id.lgl_oid,
137                               lir->lid_id.lgl_ogen, rec->lrh_index,
138                               cathandle->lgh_id.lgl_oid);
139         }
140
141         RETURN(rc);
142 }
143
144 /* lop_setup method for filter/osc */
145 // XXX how to set exports
146 int llog_obd_origin_setup(struct obd_device *obd, struct obd_llogs *llogs,
147                           int index, struct obd_device *disk_obd, int count,
148                           struct llog_logid *logid)
149 {
150         struct llog_ctxt *ctxt;
151         struct llog_handle *handle;
152         struct lvfs_run_ctxt saved;
153         int rc;
154         ENTRY;
155
156         if (count == 0)
157                 RETURN(0);
158
159         LASSERT(count == 1);
160
161         ctxt = llog_get_context(llogs, index);
162         LASSERT(ctxt);
163         llog_gen_init(ctxt);
164
165         if (logid->lgl_oid)
166                 rc = llog_create(ctxt, &handle, logid, NULL);
167         else {
168                 rc = llog_create(ctxt, &handle, NULL, NULL);
169                 if (!rc)
170                         *logid = handle->lgh_id;
171         }
172         if (rc)
173                 RETURN(rc);
174
175         ctxt->loc_handle = handle;
176         push_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
177         rc = llog_init_handle(handle, LLOG_F_IS_CAT, NULL);
178         pop_ctxt(&saved, &disk_obd->obd_lvfs_ctxt, NULL);
179         if (rc)
180                 RETURN(rc);
181
182         rc = llog_process(handle, (llog_cb_t)cat_cancel_cb, NULL, NULL);
183         if (rc)
184                 CERROR("llog_process with cat_cancel_cb failed: %d\n", rc);
185
186         if (ctxt && rc) {
187                 llogs->llog_ctxt[index] = NULL;
188                 OBD_FREE(ctxt, sizeof(*ctxt));
189         }
190         RETURN(rc);
191 }
192 EXPORT_SYMBOL(llog_obd_origin_setup);
193
194 int llog_obd_origin_cleanup(struct llog_ctxt *ctxt)
195 {
196         struct llog_handle *cathandle, *n, *loghandle;
197         struct llog_log_hdr *llh;
198         int rc, index;
199         ENTRY;
200
201         if (!ctxt)
202                 return 0;
203
204         cathandle = ctxt->loc_handle;
205         if (cathandle) {
206                 list_for_each_entry_safe(loghandle, n,
207                                          &cathandle->u.chd.chd_head,
208                                          u.phd.phd_entry) {
209                         llh = loghandle->lgh_hdr;
210                         if ((llh->llh_flags &
211                                 LLOG_F_ZAP_WHEN_EMPTY) &&
212                             (llh->llh_count == 1)) {
213                                 rc = llog_destroy(loghandle);
214                                 if (rc)
215                                         CERROR("failure destroying log during "
216                                                "cleanup: %d\n", rc);
217                                 LASSERT(rc == 0);
218
219                                 index = loghandle->u.phd.phd_cookie.lgc_index;
220                                 llog_free_handle(loghandle);
221
222                                 LASSERT(index);
223                                 llog_cat_set_first_idx(cathandle, index);
224                                 rc = llog_cancel_rec(cathandle, index);
225                                 if (rc == 0)
226                                         CDEBUG(D_HA, "cancel plain log at index"
227                                                " %u of catalog "LPX64"\n",
228                                                index,cathandle->lgh_id.lgl_oid);
229                         }
230                 }
231                 llog_cat_put(ctxt->loc_handle);
232         }
233         return 0;
234 }
235 EXPORT_SYMBOL(llog_obd_origin_cleanup);
236
237 /* add for obdfilter/sz and mds/unlink */
238 int llog_obd_origin_add(struct llog_ctxt *ctxt,
239                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
240                         struct llog_cookie *logcookies, int numcookies)
241 {
242         struct llog_handle *cathandle;
243         int rc;
244         ENTRY;
245
246         cathandle = ctxt->loc_handle;
247         LASSERT(cathandle != NULL);
248         rc = llog_cat_add_rec(cathandle, rec, logcookies, NULL);
249         if (rc != 1)
250                 CERROR("write one catalog record failed: %d\n", rc);
251         RETURN(rc);
252 }
253 EXPORT_SYMBOL(llog_obd_origin_add);
254
255 int obd_llog_cat_initialize(struct obd_device *obd, struct obd_llogs *llogs, 
256                             int count, char *name)
257 {
258         struct llog_catid *idarray;
259         int size = sizeof(*idarray) * count;
260         int rc;
261         ENTRY;
262
263         OBD_ALLOC(idarray, size);
264         if (!idarray)
265                 RETURN(-ENOMEM);
266
267         rc = llog_get_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
268                                name, count, idarray);
269         if (rc) {
270                 CERROR("rc: %d\n", rc);
271                 GOTO(out, rc);
272         }
273
274         rc = obd_llog_init(obd, llogs, obd, count, idarray);
275         if (rc) {
276                 CERROR("rc: %d\n", rc);
277                 GOTO(out, rc);
278         }
279
280         rc = llog_put_cat_list(&obd->obd_lvfs_ctxt, obd->obd_fsops,
281                                name, count, idarray);
282         if (rc) {
283                 CERROR("rc: %d\n", rc);
284                 GOTO(out, rc);
285         }
286
287  out:
288         OBD_FREE(idarray, size);
289         RETURN(rc);
290 }
291 EXPORT_SYMBOL(obd_llog_cat_initialize);
292
293 int obd_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
294                   struct obd_device *disk_obd, int count,
295                   struct llog_catid *logid)
296 {
297         int rc;
298         ENTRY;
299         OBD_CHECK_OP(obd, llog_init, 0);
300         OBD_COUNTER_INCREMENT(obd, llog_init);
301
302         rc = OBP(obd, llog_init)(obd, llogs, disk_obd, count, logid);
303         RETURN(rc);
304 }
305 EXPORT_SYMBOL(obd_llog_init);
306
307 int obd_llog_finish(struct obd_device *obd, struct obd_llogs *llogs, int count)
308 {
309         int rc;
310         ENTRY;
311         OBD_CHECK_OP(obd, llog_finish, 0);
312         OBD_COUNTER_INCREMENT(obd, llog_finish);
313
314         rc = OBP(obd, llog_finish)(obd, llogs, count);
315         RETURN(rc);
316 }
317 EXPORT_SYMBOL(obd_llog_finish);